## Solving stochastic gams model multiple times in gams Topic is solved

### Solving stochastic gams model multiple times in gams

I create a gams code including randomly generated scenarios ; I want to trigger my gams code to solve, for example 100 times to obtain different solutions for eweryrun (When i click run button , in each time , i can obtain different solution since it includes random generator for scenarios) but ı could not do it automotically for example to write a code or to find trigger option for solving a hundred times in order to obtain different solutions and create histogram by using solutions . How can ı trigger could you please help me ?

### Re: Solving stochastic gams model multiple times in gams

Hi

Take a closer look at the documentation on loops.

Cheers

Renger

Take a closer look at the documentation on loops.

Code: Select all

```
set i /1*100/;
loop(i,
* set a random value
* solve your model
* save the results:
results(i, "myresult x") = X.L;
* end of loop
);
```

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: Solving stochastic gams model multiple times in gams

Hello,

I try to apply your method to obtain loop but gams gives an error massage that is "declaration can not be allowed in loop as I understund from massage I connot put random value generation inside the loop could you please help me about this case , ı add my code below;

execseed= 1+gmillisec(jnow)

sets

i index of small item/1*3/

j index of pattern/1*4/

scen senaryo /1*100/;

Variables z total cost;

integer variables

x number of replication

r raw material quantity;

positive variables

In(scen)

Ls(scen);

binary variable

y;

scalars

c production cost/100/

s setup cost/60/

h holding cost/320/

b lost sales cost/800/

L threshold/1000/

M big m/30/;

parameter d(scen);

scalar mu /10/;

scalar poisson;

scalar su;

loop(scen,

poisson = -1;

su = 0;

repeat

su = su + [-log(uniform(0.01,1))];

poisson = poisson + 1;

until (su >= mu);

d(scen) = poisson;

);

parameter ro(scen);

ro(scen)= normal(0.95,0.0001) ;

parameter

p(scen) "probability" / #scen [1/card(scen)] / ;

parameters

w(i) length of small items

/1 500

2 400

3 300/

av(i) avaibility of small items

/1 15

2 25

3 15/

cr(i)

/1 75

2 60

3 45/;

table a(i,j) how many small items i in pattern j

1 2 3 4

1 1 2 0 0

2 1 0 1 3

3 1 0 2 0 ;

equations

obj

cons1

cons2

cons3

cons4

;

obj.. z =e= sum(i,cr(i)*r(i))+sum(j,c*x(j)) + sum(j,s*y(j))+sum(scen,p(scen)*(h*In(scen)+b*Ls(scen)));

cons1(j)..x(j)=l=M*y(j);

cons2(scen)..In(scen)-Ls(scen)=e=ro(scen)*sum(j,x(j))-d(scen);

cons3(i)..sum(j,a(i,j)*x(j))=e=r(i);

cons4(i)..r(i)=l=av(i);

Model mymode/all/;

solve mymodel min z using mip;

display x.l, r.l, y.l, In.l,Ls.l,z.l,d, p;

I try to apply your method to obtain loop but gams gives an error massage that is "declaration can not be allowed in loop as I understund from massage I connot put random value generation inside the loop could you please help me about this case , ı add my code below;

execseed= 1+gmillisec(jnow)

sets

i index of small item/1*3/

j index of pattern/1*4/

scen senaryo /1*100/;

Variables z total cost;

integer variables

x number of replication

r raw material quantity;

positive variables

In(scen)

Ls(scen);

binary variable

y;

scalars

c production cost/100/

s setup cost/60/

h holding cost/320/

b lost sales cost/800/

L threshold/1000/

M big m/30/;

parameter d(scen);

scalar mu /10/;

scalar poisson;

scalar su;

loop(scen,

poisson = -1;

su = 0;

repeat

su = su + [-log(uniform(0.01,1))];

poisson = poisson + 1;

until (su >= mu);

d(scen) = poisson;

);

parameter ro(scen);

ro(scen)= normal(0.95,0.0001) ;

parameter

p(scen) "probability" / #scen [1/card(scen)] / ;

parameters

w(i) length of small items

/1 500

2 400

3 300/

av(i) avaibility of small items

/1 15

2 25

3 15/

cr(i)

/1 75

2 60

3 45/;

table a(i,j) how many small items i in pattern j

1 2 3 4

1 1 2 0 0

2 1 0 1 3

3 1 0 2 0 ;

equations

obj

cons1

cons2

cons3

cons4

;

obj.. z =e= sum(i,cr(i)*r(i))+sum(j,c*x(j)) + sum(j,s*y(j))+sum(scen,p(scen)*(h*In(scen)+b*Ls(scen)));

cons1(j)..x(j)=l=M*y(j);

cons2(scen)..In(scen)-Ls(scen)=e=ro(scen)*sum(j,x(j))-d(scen);

cons3(i)..sum(j,a(i,j)*x(j))=e=r(i);

cons4(i)..r(i)=l=av(i);

Model mymode/all/;

solve mymodel min z using mip;

display x.l, r.l, y.l, In.l,Ls.l,z.l,d, p;

### Re: Solving stochastic gams model multiple times in gams

Hi

The error means that you have declared a parameter, variable or set within the loop. Assignments (... = ... are allowed but declaration are not. YOu have to put the declarations before the loop starts.

Renger

PS. Your code doesn't reproduce this error. Next time, please add the code that reproduces the error. ALso put the code between code blocks (use the </> symbol in the post editor), so that the formatting is preserved. Otherwise, people have to reformat your code.

The error means that you have declared a parameter, variable or set within the loop. Assignments (... = ... are allowed but declaration are not. YOu have to put the declarations before the loop starts.

Renger

PS. Your code doesn't reproduce this error. Next time, please add the code that reproduces the error. ALso put the code between code blocks (use the </> symbol in the post editor), so that the formatting is preserved. Otherwise, people have to reformat your code.

____________________________________

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: Solving stochastic gams model multiple times in gams

Hello

Thank you very much , I followed your instruction and now it is ok. There is no error ı could obtain results.

Best regards

Thank you very much , I followed your instruction and now it is ok. There is no error ı could obtain results.

Best regards