How To Write A Binary Variable In GAMS

2,169 views
Skip to first unread message

/\ l_ !

unread,
Aug 24, 2011, 3:06:26 AM8/24/11
to gamsworld
Hello Everybody
This might seem very simple but I'm new to GAMS.
I want to write a binary variable in my model.

Suppose X(i,j) is the binary variable, I want to define it this way:
X(i,j)=1 if Y(i,j)>=1 or Y(j,i)>=1
X(i,j)=0 if Y(i,j)=0 or Y(j,i)=0


How should I write it in GAMS

Thanks a lot.

Krishna Prasad

unread,
Aug 24, 2011, 5:37:46 AM8/24/11
to gams...@googlegroups.com
Hi,
           I think you need to declare it as binary variable first, then use what ever is you conditions, i.e  in GAMS

Binary Variable X(i,j) some descriptions;


then write the conditions....




--
You received this message because you are subscribed to the Google Groups "gamsworld" group.
To post to this group, send email to gams...@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.




--
Sincerely
~Krish


/\ l_ !

unread,
Aug 24, 2011, 7:31:36 AM8/24/11
to gamsworld
Yeah, I know I should write the conditions but what should I write?!!
I mean can anybody write the codes here. I write it again:

Suppose X(i,j) is the binary variable, I want to define it this way:
X(i,j)=1 if Y(i,j)>=1 or Y(j,i)>=1
X(i,j)=0 if Y(i,j)=0 or Y(j,i)=0

On Aug 24, 1:37 pm, Krishna Prasad <kprasad.i...@gmail.com> wrote:
> Hi,
>            I think you need to declare it as binary variable first, then use
> what ever is you conditions, i.e  in GAMS
> *
> Binary Variable X(i,j) some descriptions;*
>
> then write the conditions....

shahin....@gmail.com

unread,
Aug 24, 2011, 4:31:17 PM8/24/11
to gamsworld
Ali, the conditions are a bit strange and contradicting:
what happens if Y(i,j)==1 && Y(i,j)==0
then according to the first constraint you have X(i,j)=1
and according to the next one you have X(i,j)=0


are you sure the set is correct?

Otherwise, depending on what kind of solver you used there, in
general, such constratinst can be epressed differently.
E.g. in cplex you can make use of indicators.

Shahin Gelareh

/\ l_ !

unread,
Aug 25, 2011, 4:40:03 AM8/25/11
to gamsworld
Dear Shahin I don't understand what you mean.
X(i,j) is a binary variable and Y(i,j) is an integer variable. For any
Y(i,j) or Y(j,i) which is greater or equal to 1, our binary variable
X(i,j) must be 1 and for Y(i,j) or Y(j,i) equal to 0 the binary
variable should be equal to 0 too.
You say what happens if Y(i,j)=1, then according to the first
statement X(i,j)=1

On Aug 25, 12:31 am, "shahin.gela...@gmail.com"

shahin....@gmail.com

unread,
Aug 25, 2011, 5:56:11 AM8/25/11
to gamsworld
Ali,
Lets clarify the things here:
if Y(i,j)==1 && Y(i,j)==0 then we have:
1) (Y(i,j)>= 1 or Y(j,i) >=1) is true and therefore X(i,j) must
be 1,
at the same time:
2) (Y(i,j) ==0 or Y(j,i)==0 ) is true and therefore X(i,j) must
be 0

Am I doing something wrong here?

/\ l_ !

unread,
Aug 25, 2011, 6:12:38 AM8/25/11
to gamsworld
How Can Y(i,j) be 1 and 0 at the same time??!!!
Y(i,j) is either 1 or 2 or 3 or 0 or any other integer.

On Aug 25, 1:56 pm, "shahin.gela...@gmail.com"

shahin....@gmail.com

unread,
Aug 25, 2011, 7:29:42 AM8/25/11
to gamsworld
Sorry, that was a typo, look at this:


Lets clarify the things here:
if Y(i,j)==1 && Y(j,i)==0 then we have:
1) (Y(i,j)>= 1 or Y(j,i) >=1) is true and therefore X(i,j) must
be 1,
at the same time:
2) (Y(i,j) ==0 or Y(j,i)==0 ) is true and therefore X(i,j) must
be 0




/\ l_ !

unread,
Aug 25, 2011, 8:55:57 AM8/25/11
to gamsworld
Thank you, I made a mistake in defining it
it must be this way:
X(i,j)=1 if Y(i,j)>=1 or Y(j,i)>=1
X(i,j)=0 if Y(i,j)=Y(j,i)=0
Now how should I write this code in GAMS?
I think it's a tough work.

On Aug 25, 3:29 pm, "shahin.gela...@gmail.com"

shahin....@gmail.com

unread,
Aug 25, 2011, 9:51:52 AM8/25/11
to gamsworld
well this is a modelling question not much to do with gams:

X(i,j)=1 if Y(i,j)>=1 or Y(j,i)>=1
converts to
X(i,j) <= Y(i,j) + Y(j,i)

this lets your X moves to its upper bound if needed once either of Ys
is >= 1 and make it zero once both are zero.
Does it solve your problem?

/\ l_ !

unread,
Aug 25, 2011, 10:55:43 AM8/25/11
to gamsworld
Lets Suppose Y(i,j)=1 and Y(j,i)=1 then according to the constraint
you've written we'll have X(i,j)<=2, but it doesn't mean that X(i,j)=1
it can be X(i,j)=0 too. Because both answers fit in the constraint
X(i,j)<=2.

On Aug 25, 5:51 pm, "shahin.gela...@gmail.com"

shahin....@gmail.com

unread,
Aug 25, 2011, 11:07:27 AM8/25/11
to gamsworld
Well this is the objective function which guides your X to take 1 "if
necessary!".
You don't have to worry about it.
I dont know any linear form that fulfills your wish.
Are you doing feasibility problem or optimization with an objective?

/\ l_ !

unread,
Aug 25, 2011, 11:42:33 AM8/25/11
to gamsworld
This is an optimization model. My model has Y(i,j) as an output. Then
I want to define this binary variable X to use it in another
constraint so as to have some certain Y(i,j)s.

On Aug 25, 7:07 pm, "shahin.gela...@gmail.com"

Alexander

unread,
Aug 25, 2011, 11:49:08 AM8/25/11
to gamsworld
Ali,

If the objective function does not push X in one direction you need
more
than one constraint.

To keep things simpler forget about the indices and have the example
if (Y1=1 or Y2=1) then X=1 else X=0.

You can write the following (this may not be the unique or best way
for
your problem, but works)
1) Y1 in {0,1}, Y2 in {0,1}, X in [0,1]

2) X<= Y1 + Y2
which ensures that X=0 if both Y1 and Y2 are 0.
if either Y1=1 or Y2=1 the constraint becomes redundant X<=1
if both Y1=1 and Y2=1 the constraint becomes redundant X<=2

3) X>= Y1
which ensures that X=1 if Y1=1
if Y1=0 the constraint becomes redundant X>=0

4) X>= Y2
which ensures that X=1 if Y2=1
if Y2=0 the constraint becomes redundant X>=0

In general there are well-established techniques to write this type of
constraints. Have a look at the following links to see how we used
such
techniques in problems involving reaction networks
http://dx.doi.org/10.1186/1752-0509-5-107
http://dx.doi.org/10.1371/journal.pcbi.1000591
http://dx.doi.org/10.1016/j.combustflame.2008.03.004
We cite some of the old work dealing with these techniques.

/\ l_ !

unread,
Aug 25, 2011, 12:29:43 PM8/25/11
to gamsworld
Thank You Alexander
But I must mention that Y is an integer variable and can be
0,1,2,3,... so a lot of constraints above cant be applied to my
model.
Also thanks to you shahin.

On Aug 25, 7:49 pm, Alexander <alexander.mit...@gmail.com> wrote:
> Ali,
>
> If the objective function does not push X in one direction you need
> more
> than one constraint.
>
> To keep things simpler forget about the indices and have the example
> if (Y1=1 or Y2=1) then X=1 else X=0.
>
> You can write the following (this may not be the unique or best way
> for
> your problem, but works)
> 1)  Y1 in {0,1}, Y2 in {0,1}, X in [0,1]
>
> 2) X<=  Y1 + Y2
> which ensures that X=0 if both Y1 and Y2 are 0.
> if either Y1=1 or Y2=1 the constraint becomes redundant  X<=1
> if both Y1=1 and Y2=1 the constraint becomes redundant   X<=2
>
> 3) X>=  Y1
> which ensures that X=1 if Y1=1
> if Y1=0 the constraint becomes redundant X>=0
>
> 4) X>=  Y2
> which ensures that X=1 if Y2=1
> if Y2=0 the constraint becomes redundant  X>=0
>
> In general there are well-established techniques to write this type of
> constraints. Have a look at the following links to see how we used
> such
> techniques in problems involving reaction networkshttp://dx.doi.org/10.1186/1752-0509-5-107http://dx.doi.org/10.1371/journal.pcbi.1000591http://dx.doi.org/10.1016/j.combustflame.2008.03.004

shahin....@gmail.com

unread,
Aug 25, 2011, 2:24:31 PM8/25/11
to gamsworld
Well, perhaps I understood it wrong but I guess Y is integer non
necessarily binary.

Okay, I was extremely busy and brain was not working when I wrote the
last post.
There is indeed a linear way to do that but of course nut of numerical
interest.
introduce g(i,j)as binary variable, f(i,j) as binary and M a big
constant.

Y(i,j) <= M g(i,j)
Y(j,i) <= M g(j,i)
f(i,j) >= g(i,j)
f(i,j) >= g(j,i)

x(i,j) = f(i,j)

I hope I haven't done any mistake here. :) otherwise you will find it.




On Aug 25, 5:49 pm, Alexander <alexander.mit...@gmail.com> wrote:
> Ali,
>
> If the objective function does not push X in one direction you need
> more
> than one constraint.
>
> To keep things simpler forget about the indices and have the example
> if (Y1=1 or Y2=1) then X=1 else X=0.
>
> You can write the following (this may not be the unique or best way
> for
> your problem, but works)
> 1)  Y1 in {0,1}, Y2 in {0,1}, X in [0,1]
>
> 2) X<=  Y1 + Y2
> which ensures that X=0 if both Y1 and Y2 are 0.
> if either Y1=1 or Y2=1 the constraint becomes redundant  X<=1
> if both Y1=1 and Y2=1 the constraint becomes redundant   X<=2
>
> 3) X>=  Y1
> which ensures that X=1 if Y1=1
> if Y1=0 the constraint becomes redundant X>=0
>
> 4) X>=  Y2
> which ensures that X=1 if Y2=1
> if Y2=0 the constraint becomes redundant  X>=0
>
> In general there are well-established techniques to write this type of
> constraints. Have a look at the following links to see how we used
> such
> techniques in problems involving reaction networkshttp://dx.doi.org/10.1186/1752-0509-5-107http://dx.doi.org/10.1371/journal.pcbi.1000591http://dx.doi.org/10.1016/j.combustflame.2008.03.004

/\ l_ !

unread,
Aug 26, 2011, 5:53:00 AM8/26/11
to gamsworld
:)
thank you but there is still a problem here
in case both Y(i,j) and Y(j,i) are 0, in the first two constraints
that you have written g(i,j) and g(j,i) can both be either 1 or 0. for
example considering g(i,j)=1 the first constraint will be 0<=M*1 which
is correct and thus we'll have x(i,j)=1 which is wrong and it must be
0 in case Y(i,j)=Y(j,i)=0

On Aug 25, 10:24 pm, "shahin.gela...@gmail.com"
> > techniques in problems involving reaction networkshttp://dx.doi.org/10.1186/1752-0509-5-107http://dx.doi.org/10.1371/jo...

shahin....@gmail.com

unread,
Aug 26, 2011, 6:17:47 AM8/26/11
to gamsworld
my friend, if it does not make you objective better then leave it to
be any thing as long as it does not cut off part of your feasible
region.
have you ever though why most of MIP are extremely challenging for
solvers? why people designed different customized B&B rules? . Because
the know such behaviors and they just dont want to leave it to a
generic MIP solver to spend time on it blindly.
Only for very few class of problems an ideal formulation is known
which is tight and ... as you wish.

Dont be that obsessive, take that and make use of it. Your feasible
region is contained here.

/\ l_ !

unread,
Aug 27, 2011, 6:00:00 AM8/27/11
to gamsworld
With these constraints X(i,j) would be equal to 1 for all the Y(i,j)s.
So what's the point of defining a binary variable if it is gonna be 1
all the time. Instead of that I could simply define it as a parameter
X(i,j)=1!


On Aug 26, 2:17 pm, "shahin.gela...@gmail.com"
> ...
>
> read more »

shahin....@gmail.com

unread,
Aug 27, 2011, 2:46:52 PM8/27/11
to gamsworld
well, then I would suggest you to consult with a book on linear
programming.
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages