我在AMPL中遇到了一个优化问题。我想知道如何用自己的算法在不同的步骤中运行优化?我应该使用AMPL、python还是其他软件?
以下是我想做的:
我想在可行的统治中一层一层地寻找。例如,如果我的问题在维度3中,我想在3个层中搜索,例如:
first layer : x1+x2+x3=1
second layer: x1+x2+x3=2
third layer: x1+x2+x3=3
在每一层中,我都有一些新的约束,当搜索在该层中时,这些约束将处于活动状态。假设C1
、C2
和C3
分别是层1、2和3的约束。我希望问题运行如下:
首先在第一层运行,并且C1
必须处于活动状态:
`x1+x2+x3=1` and `C1` are active. (the constraints C2 ,C3 and 2 other layers are non-active)
然后在第二层运行,C2
必须处于活动状态:
`x1+x2+x3=2` and `C2` are active. (the constraints C1 ,C3 and 2 other layers are non-active)
第三个在第三层运行,并且C3
必须处于活动状态:
`x1+x2+x3=3` and `C3` are active. (the constraints C1 ,C2 and 2 other layers are non-active)
您可以在AMPL中使用脚本来执行此操作。例如:
reset;
option solver gurobi;
param n_x := 3;
var x{1..n_x};
param bignum := 1e4;
param layer;
set layers := 1..n_x;
s.t. sum_constraint: x[1] + x[2] + x[3] = layer;
s.t. c1a: x[1] >= (if layer = 1 then 10 else 10-bignum);
s.t. c1b: x[1] <= (if layer = 1 then 10 else 10+bignum);
# on layer 1, constrain x[1] = 10, otherwise leave it effectively unconstrained
s.t. c2a: x[2] >= (if layer = 2 then 20 else 20-bignum);
s.t. c2b: x[2] <= (if layer = 2 then 20 else 20+bignum);
s.t. c3a: x[3] >= (if layer = 3 then 30 else 30-bignum);
s.t. c3b: x[3] <= (if layer = 3 then 30 else 30+bignum);
minimize of: x[1]^2+x[2]^2+x[3]^2;
for {i in layers}{
let layer := i;
printf "nLayer = %1.0fn", layer;
solve;
display x;
}
您也可以使用drop
和restore
语句来打开和关闭约束,这取决于您想要自动化的程度