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.
Loop for objective function
Re: Loop for objective function
Hi
Just remove the index from the objective and save the results in a parameter
Cheers
Renger
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;)
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Loop for objective function
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.
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.
Re: Loop for objective function
Hi
You should rewrite the set j from your model and treat the variables and parameters independent of j. Here an example:
In your case:
becomes
and if p(j,g) is a parameter, it is now set in the loop as
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
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;
);
Code: Select all
sum(q,(w(q)**n)*(abs(p(j,q)-c('1',q)))**n))**in
Code: Select all
sum(q,(w(q)**n)*(abs(pj(q)-c('1',q)))**n))**in
Code: Select all
pj(q) = p(j,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
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Loop for objective function
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.
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.
Re: Loop for objective function
2xyes
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
-
- User
- Posts: 30
- Joined: 4 years ago
Re: Loop for objective function
GAMS tells you that the dimension
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.
I hope this helps you on your way.
Cheers
Renger
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
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;
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist
-
- User
- Posts: 30
- Joined: 4 years ago
Re: Loop for objective function
Hi Renger,Renger wrote: ↑4 years ago GAMS tells you that the dimensionYou defined z over t (i.e. z(t)).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
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.
I hope this helps you on your way.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;
Cheers
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.
Re: Loop for objective function
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
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
Enjoy modeling even more: Read my blog on modeling at The lazy economist