Finding type of an integer variable

Problems with syntax of GAMS
Post Reply
Abhi140
User
User
Posts: 29
Joined: 3 years ago

Finding type of an integer variable

Post by Abhi140 »

Hi Everyone,

I need to find whether an integer variable is an odd or even number. The problem is as follows.

For a set i /1:5/;
Integer variable x(i);
Variable a(i), OF;

if x(i) is odd, then a(i) => 10;
if x(i) is even, then a(i) <= 10;

x.lo(i) = 0;
x.up(i) = 10;

I need to minimize OF = sum(i,a(i));

I tried this but I am unable to figure it out. Since we can't use variables in conditional (logical) statements using $. I don't know how to do it. COuld anyone please help me with this?

Code: Select all

Set i  'buses' /1:5/;

Parameters
b                 value    /10/;

display b;

Integer Variables x;
Variable a, OF;

Equation cons1, cons2;
cons1(i).. a(i)$(mod(x(i),2)=0) =g= b;   
cons2(i).. a(i)$(mod(x(i),2)=1) =l= b; 

Equation obj;
obj.. OF =g= sum((i), a(i));
             
x.up(i) = 10;
x.lo(i) = 0;

Model OddEven / all /;

OPTION MINLP = BARON;

solve OddEven using MINLP minimizing OF;

display OF.l, x.l, a.l;
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Finding type of an integer variable

Post by bussieck »

The trick is to represent x as 2*y + z where y is another integer variable and z a binary. x can even be continuous but will automatically assume integer values. z=1 tells you that x is odd, z=0 tell you that x is even. You can use the binary variable z in some logic constraints to enforce your other constraint. I used a bigM formulation. You find many other formulation, just search the forum for logical constraint.

Here is the full model (I had to correct a few syntactical issues):

Code: Select all

Set i  'buses' /1*5/;

Parameters
b                 value    /10/;

display b;

Integer Variables y;
binary variable z;
Variable x, a, OF;

Equation defoddeven, cons1, cons2;
defoddeven(i).. x(i) =e= 2*y(i) + z(i);
cons1(i).. a(i) =g= b - (1-z(i))*100;   
cons2(i).. a(i) =l= b + z(i)*100; 

Equation obj;
obj.. OF =e= sum((i), a(i));
             
x.up(i) = 10;
x.lo(i) = 0;
a.lo(i) = -10;

Model OddEven / all /;

solve OddEven using MIP minimizing OF;

display OF.l, x.l, a.l;
-Michael
Abhi140
User
User
Posts: 29
Joined: 3 years ago

Re: Finding type of an integer variable

Post by Abhi140 »

Thank you Bussieck for your response.
Post Reply