如何在AMPL中使用循环实现逻辑比较以简化约束代码?
关于AMPL利润约束批量定义的疑问
变量定义
set I:={1..4}; # prod 1-4 var x{I} >=0 integer; # Set this to a non-negative number! var y{I} binary; # Fixed cost is no matter what!
约束条件:利润约束
我尝试了以下写法,可行:
但我希望代码更简洁,减少重复。
#s.t. p1: x[1] <= y[1]*Profit[1]; #s.t. p2: x[2] <= y[2]*Profit[2]; #s.t. p3: x[3] <= y[3]*Profit[3]; #s.t. p4: x[4] <= y[4]*Profit[4];
这种写法可行但不够简洁。我希望通过遍历集合{i in I}来优化代码。
我尝试了以下写法,但均不可行:
s.t. pRofit: for{i in I}{ x[i]} <= for{i in I}{ y[i] * Profit[i]}; s.t. pRofit: sum{i in I}{ x[i]} <= sum{i in I}{ y[i] * Profit[i]};
请问AMPL是否提供循环解决方案?
我还尝试了以下写法,同样不可行:
s.t. pRofit: for{i in I}{ x[i]} <= for{i in I}{ y[i] * Profit[i]}; s.t. pRofit: sum{i in I}{ x[i]} <= sum{i in I}{ y[i] * Profit[i]}; * even tried the suggested stackO solutions: s.t. c1{i in S}: x[i] = y[i]*Profit[i]; by: GB supports the mod strike.
请问AMPL是否支持用循环来实现该需求?
内容的提问来源于stack exchange,提问作者Kevin
相关产品推荐
相关产品推荐

