Loop for objective function

Problems with syntax of GAMS
dasa
User
User
Posts: 13
Joined: 4 years ago

Re: Loop for objective function

Post by dasa »

dasa wrote: 3 years ago
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.
Dear Renger,

Sorry to bother you again. Here, I have added set and constraint (i just add the original one since i don't know how to modify it) that make me trouble. Kindly give me some idea to overcome this model !!!
Thanks.

Code: Select all

set t    /1*5/
    m    /1*3/
alias(m,n);

set conex(m,n) /1.2,1.3,2.3/;
    conex(m,n)$(conex(n,m))=1;
 
;
parameter a(m) /1 2,2 3/;
parameter b(m) /1 5,2 4.5/;
variables Pm(t),z, Pl(t);

parameter am, bm, resultsm, resultsmt;

equation o1, c1, c2, c3, c4;
o1.. z =e= am*sum(t,Pm(t))+ sum((n,t)$conex(m,n),Pl(m,n,t));
c1(t).. Pm(t) =l= 5;
c2(t).. Pm(t) =g= 0;
c3(n,t).. Pl(m,n,t) =l= 2.5;
c4(n,t).. Pl(m,n,t) =g= 0.5;

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;
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop for objective function

Post by Renger »

Hi
The trick is to keep the loop index out of the model equations and assign values to parameters used in the equations within the loop:

Code: Select all

set t    /1*5/
    m    /1*3/
alias(m,n);

set conex(m,n) /1.2,1.3,2.3/;
    conex(m,n)$(conex(n,m))=1;
 
;
parameter a(m) /1 2,2 3/;
parameter b(m) /1 5,2 4.5/;
variables Pm(t),z, Pl(n,t);

parameter am, bm, resultsm, resultsmt, conexm;

equation o1, c1, c2, c3, c4;
*o1.. z =e= am*sum(t,Pm(t))+ sum((n,t)$conex(m,n),Pl(m,n,t));
o1.. z =e= am*sum(t,Pm(t))+ sum((n,t)$conexm(n),Pl(n,t));
c1(t).. Pm(t) =l= 5;
c2(t).. Pm(t) =g= 0;
c3(n,t).. Pl(n,t) =l= 2.5;
c4(n,t).. Pl(n,t) =g= 0.5;

model gg /all/;
loop(m,
    am = a(m);
    bm = b(m);
    conexm(n)$conex(m,n) = 1;
   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 Hi
The trick is to keep the loop index out of the model equations and assign values to parameters used in the equations within the loop:

Code: Select all

set t    /1*5/
    m    /1*3/
alias(m,n);

set conex(m,n) /1.2,1.3,2.3/;
    conex(m,n)$(conex(n,m))=1;
 
;
parameter a(m) /1 2,2 3/;
parameter b(m) /1 5,2 4.5/;
variables Pm(t),z, Pl(n,t);

parameter am, bm, resultsm, resultsmt, conexm;

equation o1, c1, c2, c3, c4;
*o1.. z =e= am*sum(t,Pm(t))+ sum((n,t)$conex(m,n),Pl(m,n,t));
o1.. z =e= am*sum(t,Pm(t))+ sum((n,t)$conexm(n),Pl(n,t));
c1(t).. Pm(t) =l= 5;
c2(t).. Pm(t) =g= 0;
c3(n,t).. Pl(n,t) =l= 2.5;
c4(n,t).. Pl(n,t) =g= 0.5;

model gg /all/;
loop(m,
    am = a(m);
    bm = b(m);
    conexm(n)$conex(m,n) = 1;
   solve gg using lp minimizaing z;
    resultsmt(m,t, "PM") = PM.L(t);
   resultsm(m, "z") = Z.L;
);
display resultsm, resultsmt;
Cheers
Renger
Dear Renger,

I got the point now. Thanks so much for your time.
student2017
User
User
Posts: 1
Joined: 6 years ago

Re: Loop for objective function

Post by student2017 »

Hi Renger,
I have a similar problem and I really appreciate it if you could help me.
I would like to find the objective function value for each JPRIME. My objective function and first constraint have parameters that define over set JPRIME.
I have written a loop to repeat the model for each member of the set JPRIME. But for objective function row and constraint row, I get this error that says "Uncontrolled set entered as constant"
I appreciate your help in advance.
picture.JPG
Thanks,
Behi
Jacob
User
User
Posts: 6
Joined: 2 years ago

Re: Loop for objective function

Post by Jacob »

Hello Renger,
I have a related challenge on the above subject and I am kindly requesting for your expertise.
I would like to find the objective function value (P) and (I_pv) for each V(i).
Below is a copy of the code;
***********************************
Set
i "Point" /1*11/;

Parameters
V(i) "Voltage (V)"
/
1 0
2 4
3 8
4 12
5 16
6 20
7 24
8 28
9 32
10 36
11 38.5
/

Variables
I_pv "Output current of PV"
P "PV output Power"
;

Equations
E64(i),E65(i);

E64(i)..I_pv =e= IL1.L - (Io1.L * ((exp((V(i) + (I_pv * Rs1.L))/(a1.L + 0.0001))) - 1)) - ((V(i) + (I_pv * Rs1.L))/(Rsh1.L + 0.0001));
E65(i)..P =e= I_pv * V(i);

Model PV_Power /E64,E65/;
Solve PV_Power using NLP maximising P;
Post Reply