Loop for objective function

Problems with syntax of GAMS
mrv_ce
User
User
Posts: 19
Joined: 1 year ago

Loop for objective function

Post by mrv_ce » 1 year ago

Hello everybody,

In my work, I need to write a loop for my objective function.

In my objective function, I use one set which is coded with j. And I need the model results for every j values. I write an loop but I obtained "ERROR 149: Uncontrolled set entered as constant."
My code like this below;

Set j/1*52/

obj.. z=e= L(j,'1')

other constraints.. ( some of them still runs for every j)

MODEL test_data /ALL/ ;
loop(j,
SOLVE test_data USING NLP MINIMIZING z;
DISPLAY z.l, a.l, w.l, aa.l;)

How can I write a loop and display all results of model for each value of j?

Please help me...

Thanks a lot.

Best regards.

User avatar
Renger
Posts: 585
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 year ago

Hi

Just remove the index from the objective and save the results in a parameter

Code: Select all

Set j/1*52/

obj.. z=e= L('1')

other constraints.. ( some of them still runs for every j)
parameter results(j,*);
MODEL test_data /ALL/ ; 
loop(j, 
SOLVE test_data USING NLP MINIMIZING z;
results(j,"Objective") = z.L;
DISPLAY z.l, a.l, w.l, aa.l;)
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

mrv_ce
User
User
Posts: 19
Joined: 1 year ago

Re: Loop for objective function

Post by mrv_ce » 1 year ago

Hi,

Actually L(j,'1') is distance function. And I coded like below

sum(q,(w(q)**n)*(abs(p(j,q)-c('1',q)))**n))**in

q and j are sets. n and in are scalars. I need the results for every j.

I could not understand how can I remove j from the objective function and save it to a parameter?

My objective function is below:
objective.. z =e= sum(q,(w(q)**n)*(abs(p(j,q)-c(m,q)))**n))**in

Thanks for your help.

Best regards.

User avatar
Renger
Posts: 585
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 year ago

Hi

You should rewrite the set j from your model and treat the variables and parameters independent of j. Here an example:

Code: Select all

set j /1*2/;

parameter
    p(j)   Parameter defined over j,
    pj     Scalar to be used in the loop to replace p;

p(j) = ord(j);

variable OBJ,X;

equation objective, eq1;

objective..
OBJ =E= x;

eq1..
   X=E= pj;

model test /all/;

loop(j, 
	pj = p(j);
	solve test maximizing OBJ using nlp;
);
In your case:

Code: Select all

sum(q,(w(q)**n)*(abs(p(j,q)-c('1',q)))**n))**in
becomes

Code: Select all

sum(q,(w(q)**n)*(abs(pj(q)-c('1',q)))**n))**in
and if p(j,g) is a parameter, it is now set in the loop as

Code: Select all

pj(q) = p(j,g);
If p(j,g) is a variable, you define it as PJ(g) and save the result in a parameter results("p", j,g) = PJ.L(g);

Your distance function then becomes L("1").

Hope this helps.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

mrv_ce
User
User
Posts: 19
Joined: 1 year ago

Re: Loop for objective function

Post by mrv_ce » 1 year ago

Hi,

In the example, x is defined. Is this equal to " sum(q,(w(q)**n)*(abs(pj(q)-c('1',q)))**n))**in " equation or x is something new?

And one more question,

parameter
p(j) Parameter defined over j,
pj Scalar to be used in the loop to replace p;

These are defined and we also defined pj(q).

pj(q) and pj are the same?

Thanks for your support.

Best regards.

User avatar
Renger
Posts: 585
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 year ago

2xyes
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Luqman Hakeem
User
User
Posts: 22
Joined: 4 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 3 months ago

Renger wrote:
1 year ago
2xyes
Hi Renger,

I am facing almost similar problem. I have objective function "zf(t)" with 't' index for time. I don't know how to write it correctly in GAMS. Can you help me out?
Files are attached for your kind review.
Thanks in advance.
input.xlsx
(183.92 KiB) Downloaded 30 times
newfile4.gms
(5.07 KiB) Downloaded 30 times

User avatar
Renger
Posts: 585
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 3 months ago

GAMS tells you that the dimension

Code: Select all

 115  costfinal(t) .. zf(t) =e= z/(AED(t)*10*4380);
****                             $148
148  Dimension different - The symbol is referenced with more/less
        indices as declared
You defined z over t (i.e. z(t)).
For optimization you can't have multiple functions to optimize. If you want to solve this optimization problem for every t, you have to write your variables without t and loop over t.

Code: Select all

* model equations and variables without t

Equations
     costfinal        cost in KWh
etc.

...
 zf =e= z/(AED*10*4380);

* Define parameters used in the model equations without t (example GrsCm(u) instead of  GrsC(u,t) )
parameter GrsCm(u)
...
loop(t,
* Assign your parameters from the data you import
GrsCm(u) =  GrsC(u,t);
... 

solve model 
* Report results
results(t,"Objective") = zf.l;
I hope this helps you on your way.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Luqman Hakeem
User
User
Posts: 22
Joined: 4 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 3 months ago

Renger wrote:
3 months ago
GAMS tells you that the dimension

Code: Select all

 115  costfinal(t) .. zf(t) =e= z/(AED(t)*10*4380);
****                             $148
148  Dimension different - The symbol is referenced with more/less
        indices as declared
You defined z over t (i.e. z(t)).
For optimization you can't have multiple functions to optimize. If you want to solve this optimization problem for every t, you have to write your variables without t and loop over t.

Code: Select all

* model equations and variables without t

Equations
     costfinal        cost in KWh
etc.

...
 zf =e= z/(AED*10*4380);

* Define parameters used in the model equations without t (example GrsCm(u) instead of  GrsC(u,t) )
parameter GrsCm(u)
...
loop(t,
* Assign your parameters from the data you import
GrsCm(u) =  GrsC(u,t);
... 

solve model 
* Report results
results(t,"Objective") = zf.l;
I hope this helps you on your way.
Cheers
Renger
Hi Renger,

Thank you for help.
I am a little bit confused. Because parameters like 'GrsC(u)', 'CapC(p,q)' etc. have different values at different 't'. Or probably i have not been able to understand your reply completely.

User avatar
Renger
Posts: 585
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 3 months ago

As far as I can see, your model has no lagged time variables or parameters, so the idea is that you will run for each t an optimization in a loop.
Because of the loop, you can't have an index in your equations (that is technically not possible).
THerefore you make them all without the time index.
For the parameters you can either use your parameter with the index t or you define them like I did and assign in the loop the value for the actual t

cgrum(u) = cgru(u,t);

In your equations you write cgrum(u) which will have the proper value assigned for each t in the loop.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Post Reply