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?

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

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;)

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

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:

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

becomes

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

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);

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 (184 KB) newfile4.gms (5.07 KB)

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.

* 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;

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.

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

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?

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!’;]

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

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

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.

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