使用 GAMS 中的求和函数对变量子集求和
Using the sum function in GAMS to sum over a subset of variables
我正在 GAMS 中处理最大化问题,我会选择
X=(x_1,x2,...,x_n)
使得 f(X)=c_1*x_1+...c_n*x_n
最大化。 c 是已知的标量,我知道 n
(在我的例子中是 10)。我希望我的约束条件是第一个 (n-1)=9
x 的总和应为 1,而最后一个应小于 10。如何使用总和来做到这一点?
这是我试过的:
SET C / c1 .... c2 /;
ALIAS(Assets,i)
Parameter Valuesforc(i) 'C values'/
*( here are my values typed in for all the C1)
POSITIVE VARIABLES
x(i);
EQUATIONS
Const1 First constraint
Const1 Second constraint
Obj The Object;
* here comes the trouble:
Const1 .. x(10) =l= 10
Const2 .. sum((i-1),x(i)) =e= 1
代码并没有完全完成,但我相信基本设置已经输入。你如何进行求和以找到 x_1+x_1 + .... x_(n-1)
以及你如何引用 x_10
?
试试这个:
Const1 .. x('10') =l= 10;
Const2 .. sum(i$(ord(i)<card(i)),x(i)) =e= 1;
编辑:这里有一些注释来解释 Const2 中发生的事情,尤其是在“$(ord(i) < card(i))”部分。
- “$”开始一个条件,因此它从总和中排除了 i 的某些元素(参见:https://www.gams.com/latest/docs/UG_CondExpr.html#UG_CondExpr_TheDollarCondition)
- 运算符 ord returns 成员在集合中的相对位置(参见:https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheOrdOperator)
- 运算卡returns集合中的元素个数(参见:https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheCardOperator)
所以,总而言之,有一个条件说 i 的所有元素都应该包含在总和中,除了最后一个。
我正在 GAMS 中处理最大化问题,我会选择
X=(x_1,x2,...,x_n)
使得 f(X)=c_1*x_1+...c_n*x_n
最大化。 c 是已知的标量,我知道 n
(在我的例子中是 10)。我希望我的约束条件是第一个 (n-1)=9
x 的总和应为 1,而最后一个应小于 10。如何使用总和来做到这一点?
这是我试过的:
SET C / c1 .... c2 /;
ALIAS(Assets,i)
Parameter Valuesforc(i) 'C values'/
*( here are my values typed in for all the C1)
POSITIVE VARIABLES
x(i);
EQUATIONS
Const1 First constraint
Const1 Second constraint
Obj The Object;
* here comes the trouble:
Const1 .. x(10) =l= 10
Const2 .. sum((i-1),x(i)) =e= 1
代码并没有完全完成,但我相信基本设置已经输入。你如何进行求和以找到 x_1+x_1 + .... x_(n-1)
以及你如何引用 x_10
?
试试这个:
Const1 .. x('10') =l= 10;
Const2 .. sum(i$(ord(i)<card(i)),x(i)) =e= 1;
编辑:这里有一些注释来解释 Const2 中发生的事情,尤其是在“$(ord(i) < card(i))”部分。
- “$”开始一个条件,因此它从总和中排除了 i 的某些元素(参见:https://www.gams.com/latest/docs/UG_CondExpr.html#UG_CondExpr_TheDollarCondition)
- 运算符 ord returns 成员在集合中的相对位置(参见:https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheOrdOperator)
- 运算卡returns集合中的元素个数(参见:https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheCardOperator)
所以,总而言之,有一个条件说 i 的所有元素都应该包含在总和中,除了最后一个。