Loop for objective function

Problems with syntax of GAMS
Post Reply
mrv_ce
User
User
Posts: 7
Joined: 3 months ago

Loop for objective function

Post by mrv_ce » 4 weeks ago

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.

User avatar
Renger
Posts: 219
Joined: 2 years ago

Re: Loop for objective function

Post by Renger » 4 weeks ago

Hi

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

mrv_ce
User
User
Posts: 7
Joined: 3 months ago

Re: Loop for objective function

Post by mrv_ce » 4 weeks ago

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.

User avatar
Renger
Posts: 219
Joined: 2 years ago

Re: Loop for objective function

Post by Renger » 4 weeks 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

mrv_ce
User
User
Posts: 7
Joined: 3 months ago

Re: Loop for objective function

Post by mrv_ce » 2 weeks ago

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.

User avatar
Renger
Posts: 219
Joined: 2 years ago

Re: Loop for objective function

Post by Renger » 2 weeks ago

2xyes

Post Reply