## logical equations & binary variables

Problems with syntax of GAMS
M.Walde
User Posts: 16
Joined: 1 year ago

### Re: logical equations & binary variables

Hi Manassaldi,

thank you a lot for your answer. Apparently I didn't realize, that you already provided the solution I needed.
I'm sorry for that. Thank you a lot for you help!

Cheers

Manassaldi
User Posts: 86
Joined: 3 years ago
Location: Rosario - Argentina

### Re: logical equations & binary variables

Hi, To avoid taking the first element, in my opinion, it is preferable to use this code:

e_T_pi(i)\$(ord(i) gt 1) .. T_pi(i)*(m_rc_pb(i)+m_ts_pb(i)) =e= (m_rc_pb(i)*T_ro + m_ts_pb(i)*T_to(i));

Bye!

gigibotte
User Posts: 4
Joined: 1 year ago

### Re: logical equations & binary variables

Hi,

I have one question for you Manassaldi.
In my objective function I have this member that is z = [...]-TWC(j)*HS(j). Where HS(j)is a variable.

Now, TWC(j) should be a parameter that works like this:

TWC(j) = 0 when HS(j) < 1000

and

TWC(j) = 3.21 when HS(j) >=1000.

Any idea how to implement this in GAMS?

I tried to defined an equation called TWCup(j) that was:

TWCup(j)\$(HS.l(j) >= 1000).. TWC(j) =e= 3.21;

but is not working
Thanks Manassaldi
User Posts: 86
Joined: 3 years ago
Location: Rosario - Argentina

### Re: logical equations & binary variables

Hi, I think you can try a simple big-M reformulation.
I assume that "HS(j)" is continuous and positive.
The new variable TWC_HS(j) correspond to TWC(j)*HS(j)
x(j) is a binary variable:
x(j)=1 if HS(j) >=1000.
x(j)=0 if HS(j) < 1000

eq1(j).. HS(j) =g= 1000*x(j);
eq2(j).. TWC_HS(j) =g= 3.21*HS(j) - 3.21*HS.up(j)*(1-x(j));
eq3(j).. TWC_HS(j) =l= 3.21*HS(j) + 3.21*HS.up(j)*(1-x(j));
eq4(j).. HS(j) =l= (1000-1e-10) + HS.up*x(j);
eq5(j).. TWC_HS(j) =l= 3.21*HS.up(j)*x(j);

So,
if x(j)=1
HS(j) =g= 1000;
TWC_HS(j) =g= 3.21*HS(j);
TWC_HS(j) =l= 3.21*HS(j); (so, TWC_HS(j) = 3.21*HS(j))
HS(j) =l= (1000-1e-10) + HS.up; (always satisfied)
TWC_HS(j) =l= 3.21*HS.up(j); (always satisfied)

if x(j)=0
HS(j) =g= 0; (always satisfied)
TWC_HS(j) =g= 3.21*HS(j) - 3.21*HS.up(j);(always satisfied)
TWC_HS(j) =l= 3.21*HS(j) + 3.21*HS.up(j);(always satisfied)
HS(j) =l= (1000-1e-10); (1e-10 is to avoid the value of 1000)
TWC_HS(j) =l= 0; (so, TWC_HS(j) = 0*HS(j))

There may be mistakes, but I think it should work.
Bye!

mbh
User Posts: 10
Joined: 1 year ago

### Re: logical equations & binary variables

Hi all,
I have a question regarding logical conditions applied to choice variables. I will be happy if someone, specially Manassaldi, can help me on that.

I have two choice variables, a(n) and th, and a parameter p(n). I need to code the following logic in gams.

if p(n)<= th then a(n) =0;
else if th< p(n) <= th * 10^.5 then a(n) = .2;
else if th * 10^.5 < p(n) <= th * 10^1 then a(n) = .4;
else if th * 10^1 < p(n) <= th * 10^1.5 then a(n) = .6;
else if th * 10^1.5 < p(n) <= th * 10^2 then a(n) = .8;
else if th * 10^2 < p(n) then a(n) = 1

I know how to use binary variable when I have only an if then case, but here I need to code many else if cases.
I hope my description of the problem is clear enough and am waiting to hear from you.

Best,
Mary
Last edited by mbh 10 months ago, edited 1 time in total.

Manassaldi
User Posts: 86
Joined: 3 years ago
Location: Rosario - Argentina

### Re: logical equations & binary variables

Hi, if p (n) is a parameter, I think it is better to reformulate the intervals definition:

if th >= p(n) then a(n) =0;
else if th < p(n) and th >= p(n) * 10^-.5 then a(n) = .2;
else if th < p(n)*10^-.5 and th >= p(n) * 10^-1 then a(n) = .4;
else if th < p(n)*10^-1 and th >= p(n) * 10^-1.5 then a(n) = .6;
else if th < p(n)*10^-1.5 and th >= p(n) * 10^-2 then a(n) = .8;
else if th < p(n)*10^-2 then a(n) = 1

Now, using binary variables, you must identify which is the interval where "th" is located:

[0 ; p(n)*10^-2) or
[p(n) * 10^-2 ; p(n)*10^-1.5) or
[p(n) * 10^-1.5 ; p(n)*10^-1) or
[p(n) * 10^-1 ; p(n)*10^-.5) or
[p(n)*10^-0.5 ; p(n) ) or
[p(n) ; Max_value]

Is this analysis ok?
Equations for selecting the intervals are easy to implement. Let me know if you need help.
Bye!

mbh
User Posts: 10
Joined: 1 year ago

### Re: logical equations & binary variables

Hi Manassaldi.
Thank you so much for helping me out. Your analysis was really helpful.

Best,
Mary

mbh
User Posts: 10
Joined: 1 year ago

### Re: logical equations & binary variables

Hi again Manassaldi,
I have a new question for you and will really appreciate it if you can help me as before. I am still struggling with these kind of logical conditions on endogenous variable in my model. The problem is that p(n) includes a binary variable itself. I mean
p(n) = sum(g, t(g) * c(g,n)) where t(g) is a binary variable. With your help, I ended up with a solution to make the intervals using binary variables (uu(n), vv(n), ww(n) , xx(n), yy(n), zz(n), yy(n)). But after that I have a multiplication of binary variables as below:

eq1(n).. th =g= p(n) * uu(n) + p(n) * (10 ** (-1/2)) * vv(n) + p(n) * (10 ** (-2/2)) * ww(n) + p(n) * (10 ** (-3/2)) * xx(n) + p(n) * (10 ** (-4/2)) * yy(n);

eq2(n).. delay(n) =e= 0.2 * vv(n) + 0.4 * ww(n) + 0.6 * xx(n) + 0.8 * yy(n) + 1 * zz(n);

eq3(n).. th =l= max * uu(n) + (p(n) - exp(-10)) * vv(n) + (p(n) * (10 ** (-1/2)) - exp(-10)) * ww(n) + (p(n) * (10 ** (-1)) - exp(-10)) * xx(n)
+ (p(n) * (10 ** (-3/2)) - exp(-10)) * yy(n) + (p(n) * (10 ** (-4/2)) - exp(-10)) * zz(n);

eq4(n).. uu(n) + vv(n) + ww(n) + xx(n) + yy(n) + zz(n) =e= 1;

const13(n).. p(n) =e= sum(g, t(g) * c(g,n) );

Where c(g,n) is a parameter.
I am getting some wired results and my guess is that multiplication of binary variables makes the model much more complicated. Is there any way to avoid the multiplication of binaries, because I already have some other highly nonlinear constraints in the model.

And actually since you seem to have a very good knowledge of GAMS, I have another question for you .

I have a constraint that includes multiplication of two continuous variables (const12 below):

const11(n).. phi(n) =g= 1 - (1 + (d(n) / 229)) ** (-.2) ;

const12(n).. i(n) =g= p(n) * y(n) ;

where d(n), phi(n), y(n), and i(n) are all choice variables. Is there anyway to linearize const12?

Mary

Manassaldi
User Posts: 86
Joined: 3 years ago
Location: Rosario - Argentina

### Re: logical equations & binary variables

Hi, in the previous answer I wrote this code and I saved it as a draft.
Bye!

Code: Select all

``````set
i intervals /i1*i6/
l limits    /l,u/
;
parameter
p(n) /n1 1, n2 3/
intervals(n,i,l)
anvalue(i)
/i1 0
i2  0.2
i3  0.4
i4  0.6
i5  0.8
i6  1/

evalue /1e-6/
;
intervals(n,'i1','l')=0;
intervals(n,'i2','l')=p(n)*(10**(-2));
intervals(n,'i3','l')=p(n)*(10**(-1.5));
intervals(n,'i4','l')=p(n)*(10**(-1));
intervals(n,'i5','l')=p(n)*(10**(-0.5));
intervals(n,'i6','l')=p(n);

intervals(n,'i1','u')=p(n)*(10**(-2))  - evalue;
intervals(n,'i2','u')=p(n)*(10**(-1.5))- evalue;
intervals(n,'i3','u')=p(n)*(10**(-1))  - evalue;
intervals(n,'i4','u')=p(n)*(10**(-.5)) - evalue;
intervals(n,'i5','u')=p(n)                  - evalue;
intervals(n,'i6','u')=p(n)*2;

display intervals;

variable
th
thd(n,i)
a(n)
;
binary variable
bin(n,i)
;
equation
eq1,eq2,eq3,eq4,eq5
;

eq1(n)..   th =e= sum(i,thd(n,i));
eq2(n,i).. thd(n,i) =g= intervals(n,i,'l')*bin(n,i);
eq3(n,i).. thd(n,i) =l= intervals(n,i,'u')*bin(n,i);
eq4(n)..   sum(i,bin(n,i)) =e= 1;
eq5(n)..   a(n) =e= sum(i,bin(n,i)*anvalue(i));

*this is only for testing:
th.lo=0.5;

model test /all/
solve test using mip minimizing th

``````