Page **1** of **1**

### Loop for objective function

Posted: **Tue Nov 13, 2018 8:05 pm**

by **mrv_ce**

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.

### Re: Loop for objective function

Posted: **Wed Nov 14, 2018 8:00 am**

by **Renger**

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

### Re: Loop for objective function

Posted: **Wed Nov 14, 2018 3:36 pm**

by **mrv_ce**

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.

### Re: Loop for objective function

Posted: **Thu Nov 15, 2018 8:32 am**

by **Renger**

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

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

### Re: Loop for objective function

Posted: **Tue Nov 27, 2018 6:21 pm**

by **mrv_ce**

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.

### Re: Loop for objective function

Posted: **Tue Nov 27, 2018 7:06 pm**

by **Renger**

2xyes