AMPL: How can I run a for loop in AMPL to perform a logical comparison?

67 Views Asked by At

variables:

    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!

Constraints: #Profit #I have tried this and it does work: however I want to make it cleaner with less duplicate code.

    #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];

not pretty but it works. I want to iterate through the list {i in I} to make it cleaner.

I have tried These and they do not work.

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

Does AMPL offer a for loop solution?

I have tried These and they do not work.`

    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.

Does AMPL offer a for loop solution?

1

There are 1 best solutions below

0
Kevin On

So I posted the question and forgot, using cplex;

also went back and tried it again and boom this worked.

 s.t. pRofit{i in I}: x[i] <=  y[i] * Profit[i];