Loop over set name?

Archive of Gamsworld Google Group
Post Reply
Archiver
User
User
Posts: 7876
Joined: 7 years ago

Loop over set name?

Post by Archiver »


Hi user group,



If possible I would like to condense and improve my code by not having to repeat the same calculation x times, and I wonder if anyone have any tips on how to do this. I need to assign values (LandRisk) based on a projects fractional cost of a defined subgroup (pulled from database and not static). Thera are many groups but below I have given an example of two subgroup. What I would love to do is loop over a counter and just have to reference the calculation once but I am not able to get it to work. Any help is appreciated.



Thanks,

Tina



Set

benset1 (restorproj) /pr1150,pr1193/

benset2 (restorproj) /pr1191,pr1165,pr1112/

;



TotalCost=sum(benset1, SegmentCost(benset1));



loop(benset1,

LandRisk(benset1,uncertscen,restorregion,timeper)=LandRisk2(benset1,uncertscen,restorregion,timeper)*SegmentCost(benset1)/TotalCost;

);



TotalCost=sum(benset2, SegmentCost(benset2));



loop(benset2,

LandRisk(benset2,uncertscen,restorregion,timeper)=LandRisk2(benset2,uncertscen,restorregion,timeper)*SegmentCost(benset2)/TotalCost;

);



*****************

Wish I could do:

Set

benset1 (restorproj) /pr1150,pr1193/

benset2 (restorproj) /pr1191,pr1165/

iteration /1*50/

;



Loop(iteration,

TotalCost=sum(benset||iteration, SegmentCost(benset||iteration));



loop(benset||iteration,

LandRisk(benset||iteration,uncertscen,restorregion,timeper)=LandRisk2(benset||iteration,uncertscen,restorregion,timeper)*SegmentCost(benset||iteration)/TotalCost;

);

);






__________________________________________________________________________

This email message is for the sole use of the intended recipient(s) and
may contain confidential information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies
of the original message.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
Archiver
User
User
Posts: 7876
Joined: 7 years ago

Re: Loop over set name?

Post by Archiver »

Reply-to: gamsworld@googlegroups.com

Christina,

The GAMS $macro facility (http://gams.com/help/index.jsp?topic=%2 ... OLLARmacro) allows you to reuse code. While it is best used for expressions (to make them look like functions), you can use it for more complex statements. Here is a short way of doing what you did:

$macro calc(cc) TotalCost=sum(cc, SegmentCost(cc)); \
loop(cc, LandRisk(cc,uncertscen,restorregion,timeper)=LandRisk2(cc,uncertscen,restorregion,timeper)*SegmentCost(cc)/TotalCost)


calc(benset1);
calc(benset2);

Here is another solution that involves an additional subset cc some some helper sets:

set cc(restorproj), iter /i1*i2/, icc(iter,restorproj) / i1.#benset1, i2.benset2 /;
loop(iter,
cc(restorproj) = icc(iter, restorproj)l
TotalCost=sum(cc, SegmentCost(cc));
loop(cc, LandRisk(cc,uncertscen,restorregion,timeper)=LandRisk2(cc,uncertscen,restorregion,timeper)*SegmentCost(cc)/TotalCost);
);

Hope this helps.
Michael Bussieck - GAMSWorld Coordinator


On Monday, February 22, 2016 at 2:48:53 AM UTC-5, Panis, Christina wrote:

Hi user group,



If possible I would like to condense and improve my code by not having to repeat the same calculation x times, and I wonder if anyone have any tips on how to do this. I need to assign values (LandRisk) based on a projects fractional cost of a defined subgroup (pulled from database and not static). Thera are many groups but below I have given an example of two subgroup. What I would love to do is loop over a counter and just have to reference the calculation once but I am not able to get it to work. Any help is appreciated.



Thanks,

Tina



Set

benset1 (restorproj) /pr1150,pr1193/

benset2 (restorproj) /pr1191,pr1165,pr1112/

;



TotalCost=sum(benset1, SegmentCost(benset1));



loop(benset1,

LandRisk(benset1,uncertscen,restorregion,timeper)=LandRisk2(benset1,uncertscen,restorregion,timeper)*SegmentCost(benset1)/TotalCost;

);



TotalCost=sum(benset2, SegmentCost(benset2));



loop(benset2,

LandRisk(benset2,uncertscen,restorregion,timeper)=LandRisk2(benset2,uncertscen,restorregion,timeper)*SegmentCost(benset2)/TotalCost;

);



*****************

Wish I could do:

Set

benset1 (restorproj) /pr1150,pr1193/

benset2 (restorproj) /pr1191,pr1165/

iteration /1*50/

;



Loop(iteration,

TotalCost=sum(benset||iteration, SegmentCost(benset||iteration));



loop(benset||iteration,

LandRisk(benset||iteration,uncertscen,restorregion,timeper)=LandRisk2(benset||iteration,uncertscen,restorregion,timeper)*SegmentCost(benset||iteration)/TotalCost;

);

);






__________________________________________________________________________

This email message is for the sole use of the intended recipient(s) and
may contain confidential information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies
of the original message.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
Archiver
User
User
Posts: 7876
Joined: 7 years ago

RE: Loop over set name?

Post by Archiver »


Thank you Michael! It was the second version I was hoping for, very helpful!

Tina





From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com] On Behalf Of Michael Bussieck
Sent: Monday, February 22, 2016 3:01 AM
To: gamsworld
Cc: Panis, Christina
Subject: Re: Loop over set name?



Christina,

The GAMS $macro facility (http://gams.com/help/index.jsp?topic=%2 ... OLLARmacro) allows you to reuse code. While it is best used for expressions (to make them look like functions), you can use it for more complex statements. Here is a short way of doing what you did:

$macro calc(cc) TotalCost=sum(cc, SegmentCost(cc)); \
loop(cc, LandRisk(cc,uncertscen,restorregion,timeper)=LandRisk2(cc,uncertscen,restorregion,timeper)*SegmentCost(cc)/TotalCost)


calc(benset1);
calc(benset2);

Here is another solution that involves an additional subset cc some some helper sets:

set cc(restorproj), iter /i1*i2/, icc(iter,restorproj) / i1.#benset1, i2.benset2 /;
loop(iter,
cc(restorproj) = icc(iter, restorproj)l
TotalCost=sum(cc, SegmentCost(cc));
loop(cc, LandRisk(cc,uncertscen,restorregion,timeper)=LandRisk2(cc,uncertscen,restorregion,timeper)*SegmentCost(cc)/TotalCost);
);

Hope this helps.
Michael Bussieck - GAMSWorld Coordinator


On Monday, February 22, 2016 at 2:48:53 AM UTC-5, Panis, Christina wrote:

Hi user group,



If possible I would like to condense and improve my code by not having to repeat the same calculation x times, and I wonder if anyone have any tips on how to do this. I need to assign values (LandRisk) based on a projects fractional cost of a defined subgroup (pulled from database and not static). Thera are many groups but below I have given an example of two subgroup. What I would love to do is loop over a counter and just have to reference the calculation once but I am not able to get it to work. Any help is appreciated.



Thanks,

Tina



Set

benset1 (restorproj) /pr1150,pr1193/

benset2 (restorproj) /pr1191,pr1165,pr1112/

;



TotalCost=sum(benset1, SegmentCost(benset1));



loop(benset1,

LandRisk(benset1,uncertscen,restorregion,timeper)=LandRisk2(benset1,uncertscen,restorregion,timeper)*SegmentCost(benset1)/TotalCost;

);



TotalCost=sum(benset2, SegmentCost(benset2));



loop(benset2,

LandRisk(benset2,uncertscen,restorregion,timeper)=LandRisk2(benset2,uncertscen,restorregion,timeper)*SegmentCost(benset2)/TotalCost;

);



*****************

Wish I could do:

Set

benset1 (restorproj) /pr1150,pr1193/

benset2 (restorproj) /pr1191,pr1165/

iteration /1*50/

;



Loop(iteration,

TotalCost=sum(benset||iteration, SegmentCost(benset||iteration));



loop(benset||iteration,

LandRisk(benset||iteration,uncertscen,restorregion,timeper)=LandRisk2(benset||iteration,uncertscen,restorregion,timeper)*SegmentCost(benset||iteration)/TotalCost;

);

);






__________________________________________________________________________

This email message is for the sole use of the intended recipient(s) and
may contain confidential information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies
of the original message.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
Post Reply