Equations with index Topic is solved

Problems with syntax of GAMS
Post Reply
Janisch
User
User
Posts: 38
Joined: 3 years ago

Equations with index

Post by Janisch »

Hello all,

I would like to define an equation with an index. Unfortunately this does not work:

Code: Select all

k                    /"k1","k2","k3",/
EQUATIONS
        ObjFunction(k) Objective functions;
   
        ObjFunction("k1")..
                    SUM((p,s,m,i,j),(x_TR(p,s,m,i,j)*f_TR(s,m,i,j,"k1")*d(m,i,j)+x_TS(p,s,j)*f_TS(s,"k1"))*w(p,"k1"))=e=Z("k1");
        ObjFunction("k2")..
                    SUM((p,s,m,i,j),(x_TR(p,s,m,i,j)*f_TR(s,m,i,j,"k2")*d(m,i,j)+x_TS(p,s,j)*f_TS(s,"k2"))*w(p,"k2"))+120=e=Z("k2");
        ObjFunction("k3")..
                    SUM((p,s,m,i,j),(x_TR(p,s,m,i,j)*f_TR(s,m,i,j,"k3")*d(m,i,j)+x_TS(p,s,j)*f_TS(s,"k3"))*w(p,"k3"))=e=Z("k3");
It should be defined more or less the same way as in the attached example (line 89 and 92).
AUGMECON2.gms
(10.74 KiB) Downloaded 198 times
I hope you can help me :)
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Equations with index

Post by abhosekar »

Technically, it should work. I see that you have an extra comma "," after k3 in set definition. What is the error that you are getting?

One workaround is using sameAs().

In your case, you can do
ObjFunction(k) $(sameAs(k, 'k1'))..

However, I suspect that the error is not related to equation index. You can try the following example (with and without sameAS if you just uncomment one line):

Code: Select all

set i /"i1","i2","i3"/;

variable
obj;

equation
eq1(i);

*eq1(i)$(sameAs(i, 'i1'))..  obj =e= 0;
eq1("i1")..  obj =e= 0;

model m /all/;
solve m minimizing obj using lp;
- Atharv
Janisch
User
User
Posts: 38
Joined: 3 years ago

Re: Equations with index

Post by Janisch »

The comma was a typo....

Thanks for your help. I have not tested your solution yet. Have instead tried to solve it differently.
If I'm not mistaken, the approach should also work or?

Code: Select all

SET k /"k1","k2","k3"

table  term(k,*)  coefficients
         general             f_inv
k1        1                  0
k2        1                  1
k3        1                  0
;

EQUATIONS
        ObjFunction(k);

        ObjFunction(k)..
                    term(k,"general")*(SUM((p,s,m,i,j),(x_TR(p,s,m,i,j)*f_TR(s,m,i,j,k)*d(m,i,j)+x_TS(p,s,j)*f_TS(s,k))*w(p,k)))+term(k,"f_inv")*(SUM((p,s,m,i,j),((x_TR(p,s,m,i,j)*f_TR(s,m,i,j,"k3")*d(m,i,j)+x_TS(p,s,j)*f_TS(s,"k3"))*w(p,"k3"))*f_inv(p)))=e=Z(k);
I still have a question: What is the difference between "abc" and 'abc'? Or does it not matter how I access a certain element in a set?

Many greetings
Janisch
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Equations with index

Post by abhosekar »

"" or '' both work while accessing a set element.

What you are trying to do is equivalent and should work (except for several typos which you will figure out when you run). You also need 120*term(k, 'f_inv') somewhere in the equation.
This approach will get complicated if there are more terms in your equations in which case, writing equations separately might be better.


- Atharv
Janisch
User
User
Posts: 38
Joined: 3 years ago

Re: Equations with index

Post by Janisch »

Great! Thanks for the tips :)
Fortunately the equations are not so complicated.
Janisch
User
User
Posts: 38
Joined: 3 years ago

Re: Equations with index

Post by Janisch »

How can I explicitly optimize only one variable when calling the Solve command? This expression does not work.

Code: Select all

Model example /all/;
SOLVE example USING mip MINIMIZING Z("k1");
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Equations with index

Post by abhosekar »

This is trickier than one would think. For your case, you can just add another equation stating some variable v =e= z('k1'); Followed by minimizing v.

However, there could be a case when one wants to solve this in a loop for all k. For general purpose, one can define v=e= z(ksub); where ksub is a singleton subset of k defined as

Code: Select all

set k /k1*k5/;
singleton set
ksub(k);

other_equations.....
equations.. v=e= z(ksub);

alias(k ,kk);
loop(kk,
ksub(kk)=yes;
solve model minimizing v using lp;
);
- Atharv
Janisch
User
User
Posts: 38
Joined: 3 years ago

Re: Equations with index

Post by Janisch »

Thank you :) It works great!

Janisch
Post Reply