Create all combinations of multiple parameters

Problems with modeling
Post Reply
LuisG
User
User
Posts: 8
Joined: 6 years ago

Create all combinations of multiple parameters

Post by LuisG »

I have three parameters each depending on a different set, for example:

Parameter par1(i)
/ 1 100
2 100 / ;

Parameter par2(j)
/ 1 100
2 100 / ;

Parameter par3(k)
/ 1 100
2 100 / ;

Now I would like to have all of the parameter values depending on a single set n. In other words, I would like to create a list of all combinations of the parameters. With the three example parameters above, that set would need 2^3 = 8 elements.

My problem: I can use the loop command over my sets i, j and k to create all of the combinations. However, I do not know how to assign the results to the set n. As far as I can see, I can only assign a parameter value using a set but not with a scalar which could count the iterations of the loop.

Any suggestions?
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Create all combinations of multiple parameters

Post by bussieck »

I have really no idea what you try to do and if you explain why you want to do such thing in the first place we might give more useful advice, but you can advance a single element within a set (nn(n) = nn(n-1);), so you can accomplish what you try to do with the following code. Since I have no idea why you want to do all this I could not do something more useful that multiplying the individual parameters:

Code: Select all

set i /1*2/; alias (i,j,k);
$eval sizeN power(card(i),3)
set n /1*%sizeN%/;
singleton set nn(n);
Parameter par1(i)
/ 1 100
2 100 / ;

Parameter par2(j)
/ 1 100
2 100 / ;

Parameter par3(k)
/ 1 100
2 100 / ;

parameter parn(n), par(i,j,k);

nn('1')=yes;
loop((i,j,k),
  parn(nn) = par1(i)*par2(j)*par3(k);
  par(i,j,k) = nn.val;
  nn(n) = nn(n-1);
);
display parn, par;
-Michael
LuisG
User
User
Posts: 8
Joined: 6 years ago

Re: Create all combinations of multiple parameters

Post by LuisG »

Hello Michael,

thank you so much for your answer, this is exactly what I was looking for. I just did not know how to go through the indexing set. For instance, I was not aware that I could do this: nn('1')=yes;

What I am trying to do with this is rearranging and aggregating some scenarios which are used in a model.

Besides, I did not know that it is possible to create sets with a calculated size using $eval. That is very good to know, thanks a lot for your example!

Best regards,
Luis
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Create all combinations of multiple parameters

Post by dirkse »

Luis,

Looks like you found what you need, but another way to do this that is also helpful is to use a map. In the example below, the mapping set matches each element of n with a different i,j,k tuple. It sometimes help to dump the entire thing to GDX and browse it in the IDE: that really helps to see it.

Code: Select all

set i /1*2/; alias (i,j,k);
$eval sizeN power(card(i),3)
sets
  n /n1 * n%sizeN%/
  map(n,i,j,k) / (#n):((#i).(#j).(#k)) /
  ;
Parameter par1(i)
/ 1 100
2 100 / ;

Parameter par2(j)
/ 1 100
2 100 / ;

Parameter par3(k)
/ 1 100
2 100 / ;

parameter parn(n), par(i,j,k);
par(i,j,k) = par1(i)*par2(j)*par3(k);
parn(n) = sum{map(n,i,j,k), par(i,j,k)};
LuisG
User
User
Posts: 8
Joined: 6 years ago

Re: Create all combinations of multiple parameters

Post by LuisG »

dirkse,

Thank you very much for the additional information! Mapping sets was actually my first idea, but I failed with the implementation and decided that a loop would be easier.
Post Reply