Optimiziation in AMPL returns wrong result
I'm new to AMPL "A Mathematical Programming Language". I'm trying to solve some equations but I found out that the answer is not logically correct (or as far as I understand!). So my code is the following (after I found out the problem, I had to simplify it to understand it):
option solver minos; var x; var y; var z; maximize output: x+y+z; subject to x_lim: 0<=x<=1; subject to y_lim: 0<=y<=1; subject to z_lim: 0<=z<=1; subject to disable: if x = 1 then (y+z) = 0; solve; display output,x,y,z;
The output is the following:
output = 1 x = 1 y = 0 z = 0
but if I'm right, the maximum output must be 2 (when x = 0, y = 1, z = 1).
Now, if I switch the variables declaration order :
var y; var x; var z; maximize output: x+y+z; subject to x_lim: 0<=x<=1; subject to y_lim: 0<=y<=1; subject to z_lim: 0<=z<=1; subject to disable: if x = 1 then (y+ z) = 0; solve; display output,x,y,z;
then the output becomes 3 (x=y=z=1) and the constraint (if x = 1 then (y+z) = 0) is not met!
The problem is somehow simple. I'm trying to group as much variables as I can to maximize the output and meet all the constraints as well.
Can you please help me understand it?
Here is the cleaned-up version of your model:
var x binary; var y >= 0, <= 1; var z >= 0, <= 1; maximize output: x + y + z; disable: y + z <= 2*(1-x); solve; display output, x, y, z;
output = 2 x = 0 y = 1 z = 1
I assumed that at least x is a binary variable, otherwise your model makes little sense to me. Also notice the way I expressed variable bounds right at the declaration and not as separate constraints.
The problem with your original model is that AMPL is a modeling language and not a programming language: You have to express if - then differently as in programming languages. See Integer Programming Tricks, under 7.3 Either-or constraints. I know it is counterintuitive and painful. The if - then you stumbled into serve a different purpose and are not meant to be used with variables.
If you are using CPLEX, you can express if-then constraints quite intuitively:
disable: x = 1 ==> y + z = 0;
It should work; unfortunately, I don't have CPLEX installed so I cannot test it.