Search code examples
pythonoptimizationampl

how to run an optimization problem in different steps?


I have an optimization problem in AMPL. am wondering how I can run my optimization with my own algorithm in different steps? should I use AMPL or python or another software?

Here is what I want to do:

I want to search in feasible reign layer by layer. For example if my problem is in dimension 3, I want to search in 3 layers such as:

first layer :  x1+x2+x3=1

second layer:  x1+x2+x3=2

third layer:    x1+x2+x3=3

in each layer I have some new constraints that will be active when the search is in that layer. Suppose C1, C2 ,C3 are constraints for layer 1,2 and 3 respectively. I want the problem to be ran as below:

First ran in the first layer and C1 must be active :

          `x1+x2+x3=1`   and `C1`     are active.  (the constraints C2 ,C3 and 2 other layers are non-active)

Then ran in the second layer and C2 must be active:

          `x1+x2+x3=2`   and `C2`     are active.  (the constraints C1 ,C3 and 2 other layers are non-active)

third ran in the third layer and C3 must be active :

          `x1+x2+x3=3`   and `C3`     are active.  (the constraints C1 ,C2 and 2 other layers are non-active)

Solution

  • You can use a script to do this in AMPL. For instance:

    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;
    }
    

    You could also use drop and restore statements to switch constraints on and off, depending on just how much you want to automate it.