Equating two sets of variables

Problems with syntax of GAMS
Post Reply
cnbrksnr
User
User
Posts: 10
Joined: 1 year ago

Equating two sets of variables

Post by cnbrksnr » 4 days ago

Hello everyone,

Suppose I have two sets: n1 and n2, where card(n1) > card(n2). And we have two set of variables x(n1) and y(n2). Furthermore, I have a set nn(n1) (i.e., nn is a subset of n1), in which the number of yes's is equal to card(n2).

I want to define the set of constraints x(nn) = y(n2). In other words, a subset of vector x is to be equal to vector y. I couldn't manage to do that. I naively tried:

Code: Select all

Set
nn(n1);
nn(n1)$(n1.pos > 96 and n1.pos <= 2*96) = yes;


Equations
p_coup(n2,n1)
;

p_coup(nn,n1).. x(n1) =e= y(n2)
;

which gives my favorite error: uncontrolled set entered as a constant. Anybody can help me about this?

Thanks!

abhosekar
Moderator
Moderator
Posts: 209
Joined: 1 year ago

Re: Equating two sets of variables

Post by abhosekar » 3 days ago

You are almost in the right direction. What you are missing is a mapping set. When you say x(nn) = y(n2), you are creating a map that y(1) belongs to x(97).. y(2) belongs to x(98) and so on...

I created a small example where n1 is of size 8 and n2 is of size 3. You want to equate x(4-6) with y(1-3).

Code: Select all

set
n1 /1*8/
n2 /1*3/;
variable x(n1), y(n2), obj;


set nn2(n1, n2);
nn2(n1, n2)$(ord(n1) = ord(n2) + 2) = yes;

display nn2;

Equations
p_coup(n2,n1)
objeq
;

p_coup(n2,n1)$nn2(n1, n2).. x(n1) =e= y(n2)
;
objeq.. obj=e=0 ;

model m/all/;
option limrow=100;
solve m using lp min obj;
You can see in the listing file that correct equations are generated. It is good to have favorite errors but the error is obvious because you have y(n2) in an equation that is not indexed by n2. Therefore, GAMS doesn't know what n2 you are talking about in that equation.

HTH

- Atharv

User avatar
dirkse
Moderator
Moderator
Posts: 165
Joined: 5 years ago
Location: Fairfax, VA

Re: Equating two sets of variables

Post by dirkse » 3 days ago

Hello,

Atharv is spot on when he suggests that a mapping set (and an equation defined via this mapping set) is the way to go here.

But his code for computing this mapping set could be more general. Here's an example that works no matter what the set contents are. It uses the .pos attribute of a set - not something I suggest using often but it was added to GAMS for exactly such cases as this.

Code: Select all

sets
  n1 / 'apple', 'pear', 'banana', 'grape' /
  n2 / 'newton', 'hypatia', 'gauss', 'ramanujan', 'lovelace', 'dantzig' /
  nn(n2)
  pair(n1,n2)
  ;
nn(n2) = yes;
nn('gauss') = no;
nn('lovelace') = no;
abort$[card(nn) <> card(n1)] 'subset nn has bogus size', nn;

* N.B.: one might try to define the mapping like this:
*       pair(n1,nn) = [ord(n1) eq ord(nn)];
* but we cannot use ord(nn), because nn is dynamic
* but the .pos attribute is available even for dynamic sets

pair(n1,nn) = [ord(n1) eq nn.pos];
display pair;
HTH,

-Steve

cnbrksnr
User
User
Posts: 10
Joined: 1 year ago

Re: Equating two sets of variables

Post by cnbrksnr » 3 days ago

Thank you Atharv and Steve!

Post Reply