## SOLVE statement in a loop

### SOLVE statement in a loop

Dear friends,

I am writing a program in GAMS, in which I need to change a parameter to a certain range, and for each value of this parameter i am evaluating results.

For this sensitivity analysis is it possible to use solve statement in a loop, so that for each input I can get results.

--

With Regards,

Parul Mathuria

Research Scholar

--

To post to this group, send email to gamsworld@googlegroups.com.

To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

### Re: SOLVE statement in a loop

Well yes this is indeed quite simple.

Define a set which will be the loop index -- for instance

SET ITER /i01 * i10/;" for 10 iterations.

Define the values of your parameter in some data structure indexed over "ITER"

PARAMETER MYDATA(..., ITER)

(where ... represent whatever indices you need in your model)

and define a parameter structure which will be able to "host" the results :

PARAMETER MYRESULTS(..., ITER)

Then you just need to use something like:

loop (iter,

modeldata(...) = mydata(..., iter) ;

solve mymodel using ... minimising ... ;

myresults(..., iter) = X.l(...) ;

);

where "modeldata" is the parameter you want to change in your model, and "X" is the name of the variable you want to keep track of.

Hope this helps

cheers

Le mercredi 10 octobre 2012 11:13:28 UTC+2, parul mathuria a Ã©crit :

Dear friends,

I am writing a program in GAMS, in which I need to change a parameter to a certain range, and for each value of this parameter i am evaluating results.

For this sensitivity analysis is it possible to use solve statement in a loop, so that for each input I can get results.

--

With Regards,

Parul Mathuria

Research Scholar

--

To view this discussion on the web visit https://groups.google.com/d/msg/gamswor ... 53XwosttMJ.

To post to this group, send email to gamsworld@googlegroups.com.

To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

### Re: SOLVE statement in a loop

thanks a lot for your help. it works for my model.

On Thu, Oct 11, 2012 at 12:53 PM, Dax wrote:

Well yes this is indeed quite simple.

Define a set which will be the loop index -- for instance

SET ITER /i01 * i10/;" for 10 iterations.

Define the values of your parameter in some data structure indexed over "ITER"

PARAMETER MYDATA(..., ITER)

(where ... represent whatever indices you need in your model)

and define a parameter structure which will be able to "host" the results :

PARAMETER MYRESULTS(..., ITER)

Then you just need to use something like:

loop (iter,

modeldata(...) = mydata(..., iter) ;

solve mymodel using ... minimising ... ;

myresults(..., iter) = X.l(...) ;

);

where "modeldata" is the parameter you want to change in your model, and "X" is the name of the variable you want to keep track of.

Hope this helps

cheers

Le mercredi 10 octobre 2012 11:13:28 UTC+2, parul mathuria a Ã©crit :

Dear friends,

I am writing a program in GAMS, in which I need to change a parameter to a certain range, and for each value of this parameter i am evaluating results.

For this sensitivity analysis is it possible to use solve statement in a loop, so that for each input I can get results.

--

With Regards,

Parul Mathuria

Research Scholar

--

To view this discussion on the web visit https://groups.google.com/d/msg/gamswor ... 53XwosttMJ.

To post to this group, send email to gamsworld@googlegroups.com.

To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

--

With Regards,

Parul Mathuria

Research Scholar

--

To post to this group, send email to gamsworld@googlegroups.com.

To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

### Re: SOLVE statement in a loop

Dear all,

I need to do exactly the same (to check how the solution of a system changes with changes in a parameter), I did exactly what Dax suggested, but it turns out that, though my code runs without problems, the reported results for all the iterations are the corresponding to the system evaluated just with the first value (in the index) assigned to my parameter. I don´t really find out what the problem is. Does any of you have an idea about what could be going wrong with my code??

In advance, thank you all very much for your help.

Regards,

Kathia

I need to do exactly the same (to check how the solution of a system changes with changes in a parameter), I did exactly what Dax suggested, but it turns out that, though my code runs without problems, the reported results for all the iterations are the corresponding to the system evaluated just with the first value (in the index) assigned to my parameter. I don´t really find out what the problem is. Does any of you have an idea about what could be going wrong with my code??

In advance, thank you all very much for your help.

Regards,

Kathia

- achristensen
- User
**Posts:**14**Joined:**7 months ago

### Re: SOLVE statement in a loop

I suspect that you might not be redefining your original parameter within the loop

something like:

PARAMETER sample(iter);

*create some fake parameter to sample from

sample(iter) = uniform(0,1);

loop(iter,

my_original_parameter = sample(iter);

SOLVE my model....

);

does that help?

best,

adam

something like:

PARAMETER sample(iter);

*create some fake parameter to sample from

sample(iter) = uniform(0,1);

loop(iter,

my_original_parameter = sample(iter);

SOLVE my model....

);

does that help?

best,

adam