Logical Constraints for Counting

Problems with modeling
Post Reply
alonzo
User
User
Posts: 4
Joined: 6 years ago

Logical Constraints for Counting

Post by alonzo »

Hello all,

I would like to know how to write logical constraints for counting in GAMS.

I know CPLEX provides logical constraints .

First example, I have three variables and at least two of three variables must be greater than 20.

Then following CPLEX constraint describes above idea.

(x[0] >= 20) + (x[1] >= 20) + (x[2] >= 20) >= 2;

Do we have similar concept of constraints in GAMS?

Thank you for all your help in advance.
User avatar
bussieck
Moderator
Moderator
Posts: 1037
Joined: 7 years ago

Re: Logical Constraints for Counting

Post by bussieck »

No, there is no automatic reformulation of such constraints in GAMS. You need to do it yourself. Automatic reformulation is nice and convenient but if your go beyond toy problems your usually need to exploit more structure of the problem (reuse some intermediate variables, leave out some parts because the optimization direction fulfills constraints automatically, etc) to be able to efficiently solve the model, so therefore we leave this to the user (what's the point of doing it for toy problems if you need to do it yourself for the real deal?). The reformulation "tricks" are really ease and there are plenty of books, papers, web sites that help with that. In this forum I have been promoting the HP Williams book for learning modeling (https://books.google.de/books?id=Cd1HiA--dBcC). Here is also a resource on the web (https://ocw.mit.edu/courses/sloan-schoo ... _lec11.pdf).

I make it easy for myself and assume you x variable has an upper bound of 100, then you can use the following bigM reformulation:

Code: Select all

set i /1*3/;
positive variable x(i);
binary variable b(i);
equation deflo(i), defup(i), deflogic;
deflo(i).. x(i) =g= b(i)*20;
defup(i).. x(i) =l= (1-b(i))*100;
deflogic.. sum(i, b(i)) =g= 2;
Without the upper bound on x you can use indicator constraints (available in some solvers including Cplex, see https://www.gams.com/latest/docs/UG_Lan ... onstraints) or a relatively complex reformulation using SOS1 variables. If you don't have a good upper bound, do not use just a big number like 1e9. This screws up the numerics of the LP solver and other parts of the MIP algorithm.

-Michael
Post Reply