Loop for objective function

Problems with syntax of GAMS
Luqman Hakeem
User
User
Posts: 30
Joined: 4 years ago

Re: Loop for objective function

Post by Luqman Hakeem »

Renger wrote: 3 years 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
It is basically a constraint over a binary variable 'h' as;
retro(i) .. sum(tc, h(i,tc)) =l= 1;
It should run only once (means 'sum of h < 1'). Summation is working but this summation appears in every loop for "t" which is not required.
Luqman Hakeem
User
User
Posts: 30
Joined: 4 years ago

Re: Loop for objective function

Post by Luqman Hakeem »

Renger wrote: 3 years 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
Hi Renger!

In the attached file "HGDPwFS", 'h(i,tc)' is a binary variable for fuel switching retrofitting. Retrofitting should only be done only once in the time horizon. But it appears for every loop on "t".
HGDPwFS.gms
(9.48 KiB) Downloaded 323 times
input2.xlsx
(351.99 KiB) Downloaded 352 times
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop for objective function

Post by Renger »

Hi

I think you could do without t in H(i,t). Then, if in year x, H(i) becomes 1, you fix it to that level and it will stay like this in all t afterwards.

Code: Select all

if(h(i) = 1; h.FX(i)  = 1;);
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Luqman Hakeem
User
User
Posts: 30
Joined: 4 years ago

Re: Loop for objective function

Post by Luqman Hakeem »

Renger wrote: 3 years ago Hi

I think you could do without t in H(i,t). Then, if in year x, H(i) becomes 1, you fix it to that level and it will stay like this in all t afterwards.

Code: Select all

if(h(i) = 1; h.FX(i)  = 1;);
Cheers
Renger
Hi Renger,
Your idea is good as always. But it is showing following errors.
input2.xlsx
(351.86 KiB) Downloaded 338 times
141 Symbol declared but no values have been assigned. Check for missing
data definition, assignment, data loading or implicit assignment
via a solve statement.
A wild shot: You may have spurious commas in the explanatory
text of a declaration. Check symbol reference list.
143 A suffix is missing
149 Uncontrolled set entered as constant
257 Solve statement not checked because of previous errors
HGDPwFSPangy.gms
(9.58 KiB) Downloaded 296 times
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop for objective function

Post by Renger »

Hi Hakeem
I am not sure if this is what you are looking for
HGDPwFSPangy.gms
(9.64 KiB) Downloaded 379 times
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
dasa
User
User
Posts: 13
Joined: 4 years ago

Re: Loop for objective function

Post by dasa »

Renger wrote: 5 years 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
Dear Renger,

Following the question, i have some related problem. in my case, it happen in variable as my below

Code: Select all

set t /1*5/
    m /1*2/;
parameter a(m) /1 2,2 3/;
parameter b(m) /1 5,2 4.5/;
variables P(m,t),z;
parameter
am
bm
Pm;
loop(m,
am=a(m);
bm=b(m);
Pm(t)=P(m,t);
);
equation o1, c1, c2;
o1.. z=e=am*sum(t,Pm(t))+bm;
c1(t).. Pm(t) =l=5;
c2(t).. Pm(t) =g= 0;
model gg/all/
loop(m,
solve gg using lp minimizaing z;
);
I don't know, what is wrong with it. please kindly help me. Thanks
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop for objective function

Post by Renger »

Hi
It is not clear from your post what you intend to do.
You assign the value of a variable to another variable before you solved the model (Pm(t) = P(m,t)) in a loop over m which also doesn't make sense. Pm(t) would get the value of the last m.

CHeers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
dasa
User
User
Posts: 13
Joined: 4 years ago

Re: Loop for objective function

Post by dasa »

Renger wrote: 3 years ago Hi
It is not clear from your post what you intend to do.
You assign the value of a variable to another variable before you solved the model (Pm(t) = P(m,t)) in a loop over m which also doesn't make sense. Pm(t) would get the value of the last m.

CHeers
Renger
Hello,
Thanks for your feedback. what i want to do is to solve this optimization problem for every m. However, i don't know how to do it.
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop for objective function

Post by Renger »

Code: Select all

set t /1*5/
    m /1*2/;
parameter a(m) /1 2,2 3/;
parameter b(m) /1 5,2 4.5/;
variables Pm(t),z;

parameter am, bm, resultsm, resultsmt;

equation o1, c1, c2;
o1.. z =e= am*sum(t,Pm(t))+bm;
c1(t).. Pm(t) =l= 5;
c2(t).. Pm(t) =g= 0; 


model gg /all/;
loop(m,
    am = a(m);
    bm = b(m);    
   solve gg using lp minimizaing z;
    resultsmt(m,t, "PM") = PM.L(t);
   resultsm(m, "z") = Z.L;
);
display resultsm, resultsmt;
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
dasa
User
User
Posts: 13
Joined: 4 years ago

Re: Loop for objective function

Post by dasa »

Renger wrote: 3 years ago

Code: Select all

set t /1*5/
    m /1*2/;
parameter a(m) /1 2,2 3/;
parameter b(m) /1 5,2 4.5/;
variables Pm(t),z;

parameter am, bm, resultsm, resultsmt;

equation o1, c1, c2;
o1.. z =e= am*sum(t,Pm(t))+bm;
c1(t).. Pm(t) =l= 5;
c2(t).. Pm(t) =g= 0; 


model gg /all/;
loop(m,
    am = a(m);
    bm = b(m);    
   solve gg using lp minimizaing z;
    resultsmt(m,t, "PM") = PM.L(t);
   resultsm(m, "z") = Z.L;
);
display resultsm, resultsmt;
Cheers
Renger
Impressive!!! thanks you so much.
Post Reply