首页 > 解决方案 > 如何在不同的步骤中运行优化问题?

问题描述

我在 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)

标签: pythonoptimizationampl

解决方案


您可以在 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;
}

您还可以使用dropandrestore语句来打开和关闭约束,具体取决于您想要自动化它的程度。


推荐阅读