Python 如何在不同的步骤中运行优化问题?

Python 如何在不同的步骤中运行优化问题?,python,optimization,ampl,Python,Optimization,Ampl,我在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和

我在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)
          `x1+x2+x3=3`   and `C3`     are active.  (the constraints C1 ,C2 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)
第三层中的第三个ran和
C3
必须处于活动状态:

          `x1+x2+x3=1`   and `C1`     are active.  (the constraints C2 ,C3 and 2 other layers are non-active)
          `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.0f\n", layer; 
    solve;
    display x;
}
复位;
选项求解器古罗比;
参数n_x:=3;
var x{1..n_x};
参数bignum:=1e4;
参数层;
设置层:=1..n_x;
s、 t.总和约束:x[1]+x[2]+x[3]=层;
s、 t.c1a:x[1]>=(如果layer=1,则为10,否则为10 bignum);
s、 t.c1b:x[1]=(如果层=2,则为20,否则为20 bignum);
s、 t.c2b:x[2]=(如果层=3,则30,否则30个bignum);

s、 t.c3b:x[3]您可以使用脚本在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.0f\n", layer; 
    solve;
    display x;
}
复位;
选项求解器古罗比;
参数n_x:=3;
var x{1..n_x};
参数bignum:=1e4;
参数层;
设置层:=1..n_x;
s、 t.总和约束:x[1]+x[2]+x[3]=层;
s、 t.c1a:x[1]>=(如果layer=1,则为10,否则为10 bignum);
s、 t.c1b:x[1]=(如果层=2,则为20,否则为20 bignum);
s、 t.c2b:x[2]=(如果层=3,则30,否则30个bignum);

s、 t.c3b:x[3]您能解释一下删除和恢复吗?我在每一层都有更多的约束。我真的不明白如何使用ampl书中的插入脚本。谢谢你,所以muchI无法理解你的代码。很抱歉其中约束条件C1,。。是活动的还是非活动的?为什么x[1]=10?在c1a:x[1]>=的约束中,您的意思是c1a:C1>=?根据我的问题?为什么要对目标求平方?在这个例子中,我假设我们只想在层=1时实现“c1:x[1]=10”,这可以通过将c1拆分为两个约束来实现,如上所示。另一种方法是使用
drop c1当您想要关闭该约束时,并
恢复c1重新打开。您能解释一下删除和恢复吗?我在每一层都有更多的约束。我真的不明白如何使用ampl书中的插入脚本。谢谢你,所以muchI无法理解你的代码。很抱歉其中约束条件C1,。。是活动的还是非活动的?为什么x[1]=10?在c1a:x[1]>=的约束中,您的意思是c1a:C1>=?根据我的问题?为什么要对目标求平方?在这个例子中,我假设我们只想在层=1时实现“c1:x[1]=10”,这可以通过将c1拆分为两个约束来实现,如上所示。另一种方法是使用
drop c1当您想要关闭该约束时,并
恢复c1将其重新打开。