GLPK/Conditional Constraints
< GLPK
Constraints depending on a parameter
Sometimes constraints shall only be active if a condition is met which can be expressed by a parameter.
The following is not legal GMPL:
if (flag) {
s.t. c : x <= 5;
} else {
s.t. c : x <= 3;
}
But we can model it in GMPL as follows:
/*
* This model demonstrates how to let the existence of simple constraints
* depend on a parameter.
*/
/* This flag controls if constraint c0 or contraint c1 is active */
param flag := 1;
var x;
maximize obj: x;
s.t. c0{i in {1} : flag == 0} : x <= 3;
s.t. c1{i in {1} : flag == 1} : x <= 5;
solve;
display x;
end;
We can do the same for indexed conditions:
/*
* This model demonstrates how to let the existence of indexed constraints
* depend on a parameter.
*/
/* This flag controls if constraint c0 or contraint c1 is active */
param flag := 0;
set I := {1..3};
var x{I}, <= 10;
maximize obj: sum{i in I} x[i];
s.t. c0{i in I : i < 3 && flag == 0} : x[i] <= 3;
s.t. c1{i in I : i > 1 && flag == 1} : x[i] <= 5;
solve;
display x;
end;
Constraints depending on a binary variable
If the relevance of constraints shall depend on a binary variable, we can use a BigM approach:
/*
* This model demonstrates how to let the relevance of indexed constraints
* depend on a binary variable.
*/
/* Big M, chosen as small as possible */
param M := 7;
set I := {1..3};
var x{I}, <= 10;
/* Binary variable controlling which constraint is active */
var y, binary;
maximize obj: sum{i in I} x[i];
s.t. c0{i in I : i < 2} : x[i] <= 3 + M * y;
s.t. c1{i in I : i > 1} : x[i] <= 5 + M * (1 - y);
solve;
display x, y;
end;