AMPL Modeling Tips #1: Disjunctions

“Two variables x and y cannot be positive at the same time”: how to model this constraint? For the new MP Library-based drivers (e.g., gurobi, highs, copt), as well as for Constraint Programming solvers (ilogcp, gecode, jacop), this goes via AMPL logical operators:

x <= 0 or y <= 0

Small complete examples:

  1. With MP, using the or operator:
var x >= -1000 <= 1000;
var y >= -1000 <= 1000;
maximize total: 5 * x + 2 * y;
s.t. only_one_positive: x <= 0 or y <= 0;
  1. With MP, using implication:
var x >= -1000 <= 1000;
var y >= -1000 <= 1000;
maximize total: 5 * x + 2 * y;
s.t. only_one_positive: x > 0 ==> y <= 0;
  1. Without MP you would need to linearize the constraint using big-M:
var x >= -1000 <= 1000;
var y >= -1000 <= 1000;
var b binary;
maximize total: 5 * x + 2 * y;
s.t. big_m_1: x <= b * 1000;
s.t. big_m_2: y <= (1-b) * 1000;

Solving any of the three models above using our new Gurobi 10 driver produces the following:

ampl: option solver gurobi; solve; display x, y, total;
x-Gurobi 10.0.0: optimal solution; objective 5000
x = 1000
y = 0
total = 5000

Solving any of the three models above using our HiGHS driver produces the following:

ampl: option solver highs; solve; display x, y, total;
HiGHS 1.2.2: optimal solution; objective 5000
1 branching nodes
x = 1000
y = 0
total = 5000

More examples and documentation are in the MP Modeling Guide.

[On LinkedIn] [On Twitter]

3 Likes

This new feature is awesome!! Looking forward to the next chapter :slight_smile:

2 Likes