Hi folks,

I'm working with a GAMS model which has hierarchical sets defined and I have a query about the best way to reference a parameter defined over the parent set based on an index from the child set.

To illustrate:

SET C 'Set of countries' / C1 C2 /;

SET R 'Regions in each country' / C1_R1 C1_R2 C2_R1 C2_R2 /;

SET CR 'Set mapping countries to regions' / C1.C1_R1 C1.C1_R2 C2.C2_R1 C2.C2_R2 /;

Parameter POP(C) 'Population of each country' / C1=100000 C2=200000 /;

Parameter R_POP_F(R) 'Fraction of country's population in each region' / C1_R1=0.2 C1_R2=0.7 C2_R1=0.5 C2_R2=0.5 /;

Parameter R_POP(R) 'Absolute population of each region';

R_POP(R)=R_POP_F(R)*POP(C);

To find the absolute population in each region, I need to multiply the fraction of the population in each region, R_POP_F(R) by the corresponding country's population, POP(C). How can I do this in one line, similarly to the above - which obviously doesn't work because the set "C" above is uncontrolled. Is it possible to somehow reference the correct POP(C) value using the sets defined above?

This may not be the best way of going about this specific problem, but I have created this example to illustrate the issue I have which is part of a much larger problem and I want to avoid altering the fundamental structure of it.

Thanks in advance

Jody

asked 29 Feb '12, 10:10

Jody's gravatar image

Jody
32
accept rate: 0%


sets
   C 'Set of countries' / C1, C2 /
   R 'Regions in each country' / C1_R1, C1_R2, C2_R1, C2_R2 /
   CR(C,R) 'Set mapping countries to regions' / C1.C1_R1, C1.C1_R2, C2.C2_R1, C2.C2_R2 /
;

Parameters
   POP(C) 'Population of each country' / C1 100000, C2 200000 /
   R_POP_F(R) "Fraction of country's population in each region"
        / C1_R1 0.2, C1_R2 0.7, C2_R1 0.5, C2_R2 0.5 /
   R_POP(R) 'Absolute population of each region'
;

R_POP(R)=R_POP_F(R)*sum(cr(c,r),POP(C));
display r_pop;
link

answered 29 Feb '12, 15:30

erwin's gravatar image

erwin
40113
accept rate: 10%

Someone else added a comment (unfortunately now deleted) that this is a bad modeling structure, that one needs string operations for this and that GAMS can not handle this case. Obviously I don't agree with this. This is a perfectly valid construct. The naming of the set elements is not relevant so we don't need to rely on string operations.

(29 Feb '12, 16:15) erwin

Many thanks for that - I appreciate it.

In the meantime, I actually came up with the following:

LOOP( (C,R)$(CR(C,R)), R_POP(R)=R_POP_F(R)*POP(C) );

Is either solution more efficient than the other?

Thanks again,

Jody

(29 Feb '12, 19:31) Jody

sorry @erwin, I got the question wrong on first reading; so I removed my earlier comment.

(29 Feb '12, 20:42) Marco Luebbecke ♦

Both approaches are ok. Sometimes loops are faster, sometimes not (depends on the situation). See e.g. http://yetanothermathprogrammingconsultant.blogspot.com/2010/10/speed-of-loops-in-gams.html .

(29 Feb '12, 21:26) erwin
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×51

Asked: 29 Feb '12, 10:10

Seen: 2,397 times

Last updated: 29 Feb '12, 21:29

OR-Exchange! Your site for questions, answers, and announcements about operations research.