Loop for objective function

Problems with syntax of GAMS
Luqman Hakeem
User
User
Posts: 21
Joined: 2 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 1 month ago

Renger wrote:
1 month 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
Your idea is brilliant but i don't know how to assign parameters in loop from Excel.

User avatar
Renger
Posts: 550
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 month ago

Hi
You owe me a beer 8-)
newfile4.gms
(6.23 KiB) Downloaded 20 times
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Luqman Hakeem
User
User
Posts: 21
Joined: 2 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 1 month ago

Renger wrote:
1 month ago
Hi
You owe me a beer 8-)
newfile4.gms
Cheers
Renger
Hi Renegr,
Thanks for your efforts.
Yes i do owe you a beer.
At last i want to unload results into Excel but due to loop, i am only able to unload only results for one time period. Is there anyway to do it?

User avatar
Renger
Posts: 550
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 month ago

I showed you how to save results based on t. After the loop you can send these parameters to Excel.
CHeers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Luqman Hakeem
User
User
Posts: 21
Joined: 2 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 1 month ago

Renger wrote:
1 month ago
I showed you how to save results based on t. After the loop you can send these parameters to Excel.
CHeers
Renger
Hi Renger!
Yes you are right. But when i try to send parameters to Excel, it only saves the values for last 't'.
[execute_unload "results.gdx"
execute 'gdxxrw.exe results.gdx var=z.L rng=z! var=X.L rng=X! var=Y.L rng=Y! var=Znew.L rng=Znew! var=zf rng=zf! var=Eexi rng=Eexi! var=Enew rng=Enew! var=Enf rng=Enf! var=c1 rng=c1! var=c2 rng=c2!';]

User avatar
Renger
Posts: 550
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 month ago

Hi

If you want to report a variable within a loop (say MYVAR(i,j)), you define before the loop a parameter myresults(t,i,j,*) and then you assign within the loop the values of the variable to the parameter

Code: Select all

parameter myresults(t,i,j,*);

loop(t,
	solve your model
	myresults(t,i,j,"MYVAR") = MYVAR.L(i,j);
);

display myresults;
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Luqman Hakeem
User
User
Posts: 21
Joined: 2 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 1 month ago

Renger wrote:
1 month ago
Hi

If you want to report a variable within a loop (say MYVAR(i,j)), you define before the loop a parameter myresults(t,i,j,*) and then you assign within the loop the values of the variable to the parameter

Code: Select all

parameter myresults(t,i,j,*);

loop(t,
	solve your model
	myresults(t,i,j,"MYVAR") = MYVAR.L(i,j);
);

display myresults;
Cheers
Renger
Hi Renger,

I need your help in writing a constraint involving sum over "t" but there is also loop over "t".
For example,
Constraint:
C1(i) .. sum(t, h(i,t)) =l= 1

User avatar
Renger
Posts: 550
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 month ago

Hi
I assume that you want to sum up to the "t" of the loop. Then I would add an alias and use ord or .val:

Code: Select all

alias(t,tt);
sum(tt$(ord(tt) LE ord(t), ....);
otherwise just over tt without the $condition.

cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Luqman Hakeem
User
User
Posts: 21
Joined: 2 months ago

Re: Loop for objective function

Post by Luqman Hakeem » 1 month ago

Renger wrote:
1 month ago
Hi
I assume that you want to sum up to the "t" of the loop. Then I would add an alias and use ord or .val:

Code: Select all

alias(t,tt);
sum(tt$(ord(tt) LE ord(t), ....);
otherwise just over tt without the $condition.

cheers
Renger
Thank you Renger. But i also want this constraint not to appear in loop. The way you recommended, this constraint appers for every "t" in loop. I want to get rid of that.

User avatar
Renger
Posts: 550
Joined: 3 years ago

Re: Loop for objective function

Post by Renger » 1 month ago

Same procedure:
replace the parameter or variable with one without the index t, remove the sum and, if it is a parameter, initialize it in the loop with the value for period t
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

Post Reply