Using Loop on Objective Function when the Looping Index is also Domain of a Table Topic is solved

Problems with syntax of GAMS
Post Reply
zar
User
User
Posts: 17
Joined: 4 years ago

Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by zar »

Hello Everyone,

I am stuck in a problem where the Looping Index is also used as a Domain Variable in a Table. Here Please refer to the code below:

Code: Select all

SET m 'operating modes' / A, B, C/

p 'liquid fractions'
/        GO      
         HN      
         LD      
         HD     
         BR       / ;
        
         Table ratio(m,p) ' ratios for different operating modes'

              GO         HN        LD          HD      BR
A       0.1117   0.0946  0.2004   0.0688  0.5245
B       0.1172   0.0279  0.2621   0.0687  0.524
C       0.1173   0.0279  0.2103   0.1289  0.5156;

VARIABLES
unit(u)
Qfeed(p)
z;

EQUATIONS
Q
Objective;

PARAMETERS
Qfeedm(p,m)
unitm(p,m)
zm(m);

Q(p,m).. QFeed(p) =E= unit(u)*ratio(p,m);
Objective.. z(m) =E= ratio(p,m)*500;

Model test /ALL/

LOOP(m,
*Here I want to LOOP over Parameter ratio, which is in Table form --- I DON'T KNOW HOW?

SOLVE test using LP maximizing z
Qfeedm(p,m) = Qfeed.L(p)
unit(u,m) = unit.L(u)
zm(m) = z.L;

As the "Table ratio(p,m)" also depends on the "Operating Mode", I don't know how to add it in the loop.

Regards,
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by Renger »

Hi
Just have a look at this [ur=hCttps://forum.gamsworld.org/viewtopic.php?f=2&p=26821#p26821l]discussion [/url]and see the example I provided (I added the solution to the problem in the answer with "You owe me a beer").
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
zar
User
User
Posts: 17
Joined: 4 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by zar »

Hi Ranger,

I actually saw that post. In fact, I learnt to apply Loop on objective function from your post. However, the case is slightly different in my scenario. Here the looping index (m) is also used as a dependent variable in the Table ratio(m,p). Now, how should I define the Loop over the index 'm' when 'm' also acts as a dependent variable in 'ratio(p,m)' ? Unlike your suggested post, 'm' is not exactly like an independent variable as 'time period'.
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by Renger »

Hi
If I understand correctly you want to solve your model for every operation mode m.
This means looping over m and making sure that "m" doesn't appear in your code.
For this, you redefine your variables and parameters used in the model by dropping the index m and assigning the parameters from the original table.
Example:

Code: Select all


parameter ratioModel(p), unitModel(p), QfeedModel(p)
...
variable 
   Z, Q(m), ...
   
equation Q(p), Objective, ...
...

Q(p).. QFeed(p) =E= unit(u)*ratioModel(p);

Objective..
       Z =E= ratioModel(p) * 500;
loop(m, 
     ratioModel(p) = ratio(m,p);
     unitModel(p=  unitm(m,p=;
     ....
*     solve model
*      save results in parameter indexed over m
);  

I hope this is what you are looking for.

Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
zar
User
User
Posts: 17
Joined: 4 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by zar »

Hi Ranger,

Thank you for your feedback.

Are you suggesting to drop looping index(m) from the table as well? In the existing case, the table is 2-dimensional having looping index (m) on x-axis and liquid fractions (p) on y-axis. If I remove 'm' from the table then there will be a domain violation.

Regards,
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by Renger »

Hi
In your model you don't want to have the m-index, so I made a table just over p (ratioModel(p)) and in the loop over the index m I assigned the variables of the original table to the parameter ratioModel:

Code: Select all

loop(m, 
     ratioModel(p) = ratio(m,p);
The equations now have ratioModel(p) and not any longer ratio(m,p).

Code: Select all

Q(p).. QFeed(p) =E= unit(u)*ratioModel(p);
I only showed you how to adjust some of the code. The rest is up to you.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
zar
User
User
Posts: 17
Joined: 4 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by zar »

Hi Renger,
I appreciate your replies.

Now, I defined my model parameters as below:

Code: Select all

PARAMETERS
M 'Operating Mode'
/A, B, C/ 

ratiomodel(p)
/
GO.A    0.1117
GO.B    0.1172
GO.C    0.1173
HN.A    0.0946
HN.B    0.0279
HN.C    0.0279
/;
In the Loop, I defined the code as:

Code: Select all

LOOP(m,
         ratiomodel(p) = ratio(p,m);
When I execute, GAMS give Error 148 (Dimensions different) about the parameter "ratiomodel(p)".
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by Renger »

No, you should use ratio(p) in your model and assign the values from ratioModel(p,m) to this parameter in the loop.

Code: Select all

PARAMETERS
M 'Operating Mode'
/A, B, C/ 

ratio(p,m)
/
GO.A    0.1117
GO.B    0.1172
GO.C    0.1173
HN.A    0.0946
HN.B    0.0279
HN.C    0.0279
/,

ratiomodel(p)   Ratio used in the model;
In the Loop, I defined the code as:
CODE: SELECT ALL

LOOP(m,
         ratiomodel(p) = ratio(p,m);
and use ratiomodel(p) in the equations.

Cheers
Renger
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
zar
User
User
Posts: 17
Joined: 4 years ago

Re: Using Loop on Objective Function when the Looping Index is also Domain of a Table

Post by zar »

Hi Renger,

Thank you for your elaborate response. My problem is finally solved.

I owe you a Coca Cola :)

ZAR
Post Reply