Search code examples
linear-programmingampl

How to assign binary variable in AMPL in respect to another variable


I have a problem with AMPL modelling. Can you help me how to define a binary variable u that suppose to be equall to 0 when another variable x is also equall to 0 and 1 when x is different than 0?

I was trying to use logical expressions but solver that I am working with (cplex and minos) doesn't allow it.

My idea was:

subject to:
u || x != u && x

Solution

  • Take M a 'big' constant such as x < M holds, and assume x is an integer (or x >= 1 if x is continuous). You can use the two constraints:

     u <= x     (if x=0, then u=0)
     x <= M*u   (if x>0, then u=1)
    

    with u a binary variable.

    If now x is continuous and not necessarily greater than 1, you will have to adapt the constraints above (for example, the first constraint here would not be verified with x=0.3 and u=1).

    The general idea is that you can (in many cases) replace those logical constraints with inequalities, using the fact that if a and b are boolean variables, then the statement "a implies b" can be written as b>=a (if a=1, then b=1).