AMPL约束语法

时间:2015-05-09 16:04:21

标签: constraints linear-programming ampl

我正在开发一个计算优化类的项目,并且在为我的混合整数程序编写特定约束时遇到问题。

该项目基本上是一个贷款组合优化问题,我在努力最大限度地发挥对贫困的影响,同时满足风险,回报和支出的限制。我几乎所有的约束都按预期工作,但它是最后一个给我一个问题。

以下是集合;

#SETS
set REGION;                                 #Set of regions
set COUNTRY;                                #Set of countries
set LOAN_DURATION;                          #Set of loans
set GROUP within {COUNTRY,REGION};          #Set of countries within regions

决策变量

#VARIABLES

var x{COUNTRY,LOAN_DURATION}>=0;            #Amount of money that can be loaned     #to each country c by loan type l
var y{COUNTRY,LOAN_DURATION} binary;        #decision of whether to use loan type #l for each country c 

这是给我一个问题的约束;

subject to WITHIN_REGION{r in REGION, (c,r) in GROUP, l in LOAN_DURATION}:
x[c,l] <= QUOTA[r]*sum{c in COUNTRY, l in LOAN_DURATION} x[c,l];

我要做的是说,对于每个地区,检查借给每个国家的金额,并确保它小于或等于借给该地区的一半配额。当我尝试运行此模型时,我只得到第二行的基本语法错误。

在此之前我有一个类似的约束,它检查以确保每个区域至少获得借出的总金额的区域配额,并且它似乎按预期工作;

subject to REGION_QUOTA{r in REGION}:
sum{(c,r) in GROUP, l in LOAN_DURATION} x[c,l] >= QUOTA[r]*sum{c in COUNTRY, l in LOAN_DURATION} x[c,l];

所以我想知道是否有任何振测专家可以让我帮助我最后一个约束工作。

由于

约什

1 个答案:

答案 0 :(得分:1)

问题是您在具有重叠范围的两个不同索引表达式c(c,r) in GROUP中使用相同的索引c in COUNTRY名称。您可以重命名第二个索引以避免错误:

subject to WITHIN_REGION{r in REGION, (c,r) in GROUP, l in LOAN_DURATION}:
x[c,l] <= QUOTA[r]*sum{c2 in COUNTRY, l in LOAN_DURATION} x[c2,l];

虽然如果你想对区域x[c2,l]中的所有国家r求和,这似乎是这种情况,你应该有类似的东西:

subject to WITHIN_REGION{r in REGION, (c,r) in GROUP, l in LOAN_DURATION}:
x[c,l] <= QUOTA[r]*sum{(c2,r) in GROUP, l in LOAN_DURATION} x[c2,l];