Search code examples
algorithmoptimizationampl

How to write "not equal to" in AMPL or set such as a condition for a variable's parameter?


I am trying to set a variable W{m in M, n in N} where M and N are the same set of values. M and N represent a location. Variable W represents a transfer between m and n. Therefore, m cannot equal to n in variable W. For example, when m = 1, n can only equal to 2 and 3 (M=N=c(1,2,3)).

This is a cost-minimizing 2-stage stochastic model. I tried to write m <> n into the sum conditions as well as writing m <> n in the constraint. All returned syntax error.

I tried to add the condition into the objective function:

sum{m in M, n in N | m <> n}W[m,n];

sum{m in M, n in N, m <> n}W[m,n];

sum{m in M, n in N, m!=n}W[m,n];

sum{m in M, n in N | m != n}W[m,n];

I also tried to add it into the variable definition:

var W{m in M, n in N, m <> n};

etc..

I tried many ways but none is working. AMPL returned syntax error if I added the m<>n or m!=n into the model. After I delete the m!=n conditions, the model works but returning the wrong number.


Solution

  • In AMPL you can use ":" to make a condition in the sum or forall method. E.g:

    sum{m in M, n in N : m <> n}W[m,n];
    
    sum{m in M, n in N : m!=n}W[m,n];
    
    sum{m in M, n in N : m != n}W[m,n];
    

    I hope it helps :)