Summing differing elements of a binary variable

Problems with modeling
Post Reply
hendesign
User
User
Posts: 3
Joined: 4 years ago

Summing differing elements of a binary variable

Post by hendesign »

Good afternoon.

I would like to sum how many different values of the element 'j' exist in the binary variable y(i,j,k,l) when y(i,j,k,l) = 1. For example, consider:

y(H1,C1,S1,S2) = 1
y(H1,C1,S2,S6) = 1
y(H1,C2,S1,S2) = 1
y(H1,C4,S9,S10) = 1

In the above example, there is C1, C2, C4 i.e. 3 different values for j. I want the binary variable to be limited so that the number of different j values is 2 or less. How would I go about implementing this? My initial attempt was:

Code: Select all

genHotLim(i,k,l)      .. sum((j), y(i,j,k,l)) =l= 2;
This works for a given k or l; the number of j's is =<2. But it does not limit the different number of j's in different k's and l's, such as in the example above.

For anyone savvy with heat exchanger networks, y is the binary variable for heat transfer occurring between hot stream 'i' in heat source block 'k', with cold stream 'j' in heat sink block 'l'. I want matches to be between a maximum of two streams.

I have attached my code for reference.

Thanks for the help.
HEN.gms
(33.94 KiB) Downloaded 197 times
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Summing differing elements of a binary variable

Post by bussieck »

You probably need another binary variable just indexed by j, say z(j) and you need to force z(j) to go to one if a y(i,j,k,l) goes to one, e.g. e1(i,k,l).. z(j) =g= y(i,j,k,l); Now you just limit the z's by e2.. sum(j, z(j)) =l= 2;

-Michael
hendesign
User
User
Posts: 3
Joined: 4 years ago

Re: Summing differing elements of a binary variable

Post by hendesign »

Thank you for your response! It was very close to the solution I was looking for and instantly set me down the right track. I implemented it like so:

Code: Select all

zdef(i,j,k,l)       .. z(i,j) =g= y(i,j,k,l) ;
HotLimit(i)         .. sum((j), z(i,j)) =l= 2 ;
ColdLimit(j)        .. sum((i), z(i,j)) =l= 2 ;
Post Reply