Fatih Pense's Blog

GAMS Conditionals and Subsets (Operations Research)

Thursday, April 6th, 2023

Question

Solve the following blood banking location allocation model using GAMS.

N is the number of demand points

M is the number of supply points

V is the maximum number of supply vehicles available

𝐷 = {𝐻1, … , 𝐻𝑁} is a set of N demand points

𝑆 = {𝐻𝑁+1, … , 𝐻𝑁+𝑀} is a set of M supply points

𝐻 = 𝐷 ∪ 𝑆 is the set of all points in the problem

Parameters

H =8, N=5, M=3, V=4

s.t.

Note that the MIP is used instead of the LP for mixed integer programming in GAMS

Solution

There are two important issues converting these equations to GAMS code.

First, you can see a lot of indices use different starting and ending points. However they mean the same thing(H point, demand or supply doesn’t matter). If you define these as different Sets in GAMS, they will be different domains. You have to duplicate a lot of code and even then I’m not sure if there is a good answer.

We have two solutions for this issue: 1- use conditionals for summations and also for some equations. 2- use subsets. ( Here is the subset documentation ) We will show both solutions.

Second issue is the constraint equation(4) that is defined for S and “complement of S”. Do we want all combinations of complementary subsets of all points(H)?

  • This would be a very hard problem to solve setting i to each subset(powerset element) of the set H, and j to complement of that subset. We have 2^8 different subsets and complements…
  • I think the question only asks us to exclude when i and j are same values.

So this should be fine: sum ((i,j,k)$( ord(i) <> ord(j) )... or: sum ((i,j,k)$( not SameAs(i,j) )...

GAMS code using conditionals

Sets
 
H /1*8/
  
 
 
V /1,2,3,4/
;
 
Alias(H,H2,H3)
 
Parameters
c(V) capacity of vehicle k_1
/   1   100
    2   200
    3   150
    4   150/
    
    
f(V) max distance vehicle k_1 may travel
/   1   1000
    2   1000
    3   1000
    4   1000/
    
 
Q(H) requirement of demand at point i_1 and i_2
/   1   100
    2   100
    3   100
    4   100
    5   100
    6   0
    7   0
    8   0/
    
gam(H) probability at point i_2
/   1   0.06
    2   0.06
    3   0.06
    4   0.06
    5   0.06
    6   0
    7   0
    8   0/;
 
    
table QQ[H, H] 'is the distance from 𝐻i_1 to 𝐻j_1'
     1    2    3    4    5    6    7    8
1    0    13   12   15   13   20   25   21
2    13   0    19   37   26   21   40   32
3    12   19   0    22   28   14   37   12
4    15   37   22   0    23   35   10   27
5    13   26   28   23   0    19   12   36
6    20   21   14   34   19   0    7    23
7    25   40   37   10   12   7    0    15
8    21   32   12   27   36   23   15   0;
 
 
 
Variable
    
    z  total costs;
    
 
*x_ijk 0,1 ,  y_ij 0,1
Binary variables
    x(H,H2,V)
    y(H,H2) ;
    
 
Equation
    cost objective function
    const_1(H) constraint 1
    capacity(V) constraint 2 capacity of vehicle k
    distance(V) constraint 3 max distance vehicle k may travel 
    
    const_4 constraint 4 xijk >= 1 V SS
    const_5(H,V) constraint 5 
    const_6(H,H2,V) constraint 6
    const_x(H,H2,V) constraint x_iik = 0
;
    
cost.. z=e= sum ((H,H2,V), QQ[H,H2] * x(H,H2,V))
    + sum ((H,H2)$( ord(H)<6 and ord(H2)>5), gam(H)*QQ[H,H2]*y(H,H2))
     + sum ((H,H2)$( ord(H)<6 and ord(H2)>5), 300*Q(H)*y(H,H2));
    
const_1(H)$ ( ord(H)<6 ) .. sum((H2,V), x(H,H2,V))  =e=   1 ;
 
capacity(V) .. sum((H,H2)$( ord(H)<6), Q(H)* x(H,H2,V)) =l= c(V);
 
distance(V) .. sum((H,H2), QQ[H,H2]* x(H,H2,V)) =l= f(V);
 
const_4.. sum ((H,H2,V)$( ord(H) <> ord(H2) ),   x(H,H2,V)) =g= 1;
 
const_5(H,V)..  sum (H2, x(H,H2,V) ) =e= sum (H2, x(H2,H,V) );
    
const_6(H,H2,V)$ ( ord(H)<6 and ord(H2)>5 ) ..
y(H,H2) =g= sum(H3, x(H,H3,V)) + sum(H3, x(H3,H2,V)) -1;
    
const_x(H,H2,V)$ ( ord(H) = ord(H2) ).. x(H,H2,V) =e= 0; 
     
    
 
Model bloodbank /all/;
Solve bloodbank using MIP minimizing z;
 

GAMS code using subset conditionals

We will use subset M(H) where i or j is from N+1 to N+M We will use subset N(H) where i or j is from 1 to N

To write GAMS model similar to the math notation(for our human brains), we use aliases i and j to represent whole set H.

in_jm(i,j) subset defines: all combinations where i is from 1 to N, and j is from N+1 to N+M

 
Sets
 
H /1*8/
V /1,2,3,4/
;
 
Alias(H, i,j);
 
Alias(V,k);
 
Sets
N(H) /1,2,3,4,5/
 
M(H) /6,7,8/  
 
in_jm(i,j) / (1,2,3,4,5).(6,7,8) /;
 
 
Parameters
c(V) capacity of vehicle k_1
/   1   100
    2   200
    3   150
    4   150/
    
    
f(V) max distance vehicle k_1 may travel
/   1   1000
    2   1000
    3   1000
    4   1000/
    
 
Q(H) requirement of demand at point i_1 and i_2
/   1   100
    2   100
    3   100
    4   100
    5   100
    6   0
    7   0
    8   0/
    
gam(H) probability at point i_2
/   1   0.06
    2   0.06
    3   0.06
    4   0.06
    5   0.06
    6   0
    7   0
    8   0/;
 
    
table QQ[H, H] 'is the distance from 𝐻i_1 to 𝐻j_1'
     1    2    3    4    5    6    7    8
1    0    13   12   15   13   20   25   21
2    13   0    19   37   26   21   40   32
3    12   19   0    22   28   14   37   12
4    15   37   22   0    23   35   10   27
5    13   26   28   23   0    19   12   36
6    20   21   14   34   19   0    7    23
7    25   40   37   10   12   7    0    15
8    21   32   12   27   36   23   15   0;
 
 
 
Variable
    
    z  total costs;
    
 
*x_ijk 0,1 ,  y_ij 0,1
Binary variables
    x(i,j,V)
    y(i,j) ;
    
 
Equation
    cost objective function
    const_1(i) constraint 1
    capacity(V) constraint 2 capacity of vehicle k
    distance(V) constraint 3 max distance vehicle k may travel 
    
    const_4 constraint 4 xijk >= 1 V SS
    const_5(H,V) constraint 5 
    const_6(i,j,V) constraint 6
    const_x(H,V) constraint x_iik = 0
;
    
cost.. z=e= sum ((i,j,k), QQ[i,j] * x(i,j,k))
    + sum ((i,j)$in_jm(i,j), gam(i)*QQ[i,j]*y(i,j))
     + sum ((i,j)$in_jm(i,j), 300*Q(i)*y(i,j));
    
const_1(i)$N(i) .. sum((j,V), x(i,j,V))  =e=   1 ;
 
capacity(V) .. sum((i,j)$N(i), Q(i)* x(i,j,V)) =l= c(V);
 
distance(V) .. sum((i,j), QQ[i,j]* x(i,j,V)) =l= f(V);
 
const_4.. sum ((i,j,V)$( not SameAs(i,j) ),   x(i,j,V)) =g= 1;
 
const_5(H,V)..  sum (j, x(H,j,V) ) =e= sum (i, x(i,H,V) );
    
const_6(i,j,V)$ in_jm(i,j) ..
y(i,j) =g= sum(H, x(i,H,V)) + sum(H, x(j,H,V)) -1;
    
*const_x(H,H2,V)$ ( ord(H) = ord(H2) ).. x(H,H2,V) =e= 0;
*simplify below:
const_x(H,V).. x(H,H,V) =e= 0; 
     
    
 
Model bloodbank /all/;
Solve bloodbank using MIP minimizing z;