Quantcast
Channel: Yet Another Math Programming Consultant
Viewing all articles
Browse latest Browse all 809

Stable Marriage Problem

$
0
0
The Stable Marriage Problem is a fascinating problem. In this problem, we want to assign ("marry") men to women much like the assignment problem. (I am just following the conventions in the literature here.) There is a twist, however. We want to require that the matchings are stable: there does not exist a combination \((m,w)\) such that \(m\) and \(w\) both prefer each other above their current partner [1].

Let's have a look at a tiny data set from [2].



I am not sure what the function of the last column in these two tables is, but I translated this into:
 

----     61 SET mmen

m1, m2, m3


---- 61 SET wwomen

w1, w2, w3


---- 61 PARAMETER mpref preferences of men for women (ranking,1=best)

w1 w2 w3

m1 123
m2 312
m3 231


---- 61 PARAMETER wprefpreferences of women for men (ranking,1=best)

m1 m2 m3

w1 321
w2 132
w3 213


Here the preferences are represented by numbers. The numbers should be interpreted with a bit of care as they are ordinal. This is related to what in economics is called ordinal utility. Here 1 is best, followed by 2, etc. (My choice here is a bit unfortunate as we shall see later on.)
 
In [3], the following MIP model is proposed:


This is not exactly standard notation (it is explained in [3]).  Another notation I see being used is:

Stable Marriage Problem
\[\begin{align}&\sum_m \color{darkred}x_{m,w} = 1 && \forall w \\&\sum_w \color{darkred}x_{m,w} = 1 && \forall m \\&\sum_{m'\lt_w m} \color{darkred}x_{m',w} -\sum_{w'\gt_m w} \color{darkred}x_{m,w'} \le 0  && \forall m,w\\ & \color{darkred}x_{m,w}\in \{0,1\}  \end{align}\]

This needs some explanation. 
  • \(\displaystyle\sum_{m'\lt_w m} \color{darkred}x_{m',w}=1\) indicates an inferior man \(m'\) compared to \(m\) (according to \(w\)) has been assigned to \(w\),
  • \(\displaystyle\sum_{w'\gt_m w} \color{darkred}x_{m,w'}=1\) indicates a superior woman \(w'\) compared to \(w\) (according to \(m\)) has been assigned to \(m\),
  • the complete constraint \[\sum_{m'\lt_w m} \color{darkred}x_{m',w} -\sum_{w'\gt_m w} \color{darkred}x_{m,w'} \le 0 \>\> \forall m,w\] can be interpreted as: if \(w\) marries someone less desirable than \(m\) then \(m\) must be matched to a woman better than \(w\). If this is not the case then both \(m\) and \(w\) are better off marying each other. That would not be "stable."
  • A feasible solution to these equations gives us a stable matching.

It may be interesting to see how I implemented this in GAMS. Again I use the approach where we precalculate data to simplify the constraints. First I calculated the parameters worseMen and betterWomen.


*----------------------------------------------------------------
* derived data
*----------------------------------------------------------------

sets
  worseMen(w,m,mm)     
'worse men than m for w'
  betterWomen(m,w,ww)  
'better women than w for m'
;
worseMen(w,m,mm) = wpref(w,mm)>wpref(w,m);
betterWomen(m,w,ww) = mpref(m,ww)<mpref(m,w);

 

Note that worse corresponds to \(\gt\) and better to \(\lt\). This is due to the way I encoded the ranking. The output looks like:


----     61 SET worseMenworse men than m for w

m1 m2 m3

w1.m2 YES
w1.m3 YES YES
w2.m1 YES YES
w2.m3 YES
w3.m1 YES
w3.m2 YES YES


---- 61 SET betterWomenbetter women than w for m

w1 w2 w3

m1.w2 YES
m1.w3 YES YES
m2.w1 YES YES
m2.w3 YES
m3.w1 YES
m3.w2 YES YES


With this we can write:


* if m marries someone worse than w then w must marry someone
* better than m
* otherwise we can improve both w and m

stability(m,w)..
 
sum(betterWomen(m,w,ww),x(m,ww)) =g= sum(worseMen(w,m,mm),x(mm,w));



After adding a dummy objective function, we have a model that can deliver a single solution. 


Enumerating stable matchings


Let's go back to the example in [2]. It says:


Let's see if we can reproduce this. There are at least three ways to attack this:
  • Solve the problem. Add a constraint that forbids the current solution and repeat until things become infeasible.
  • For large problems with many solutions, we can use a MIP solver with a solution pool.
  • Or we can use a Constraint Programming based solver that allows producing multiple or all solutions.

For this small problem, I decided to use use the first approach. Here is the output:

----    127 PARAMETER solutions  enumerated solutions

INDEX 1 = sol1

w1 w2 w3

m1 1
m2 1
m3 1

INDEX 1 = sol2

w1 w2 w3

m1 1
m2 1
m3 1


So my little experiment can not reproduce the results. It is quite an unequal situation: there are three authors plus say three reviewers, so at least six people have looked at this carefully, versus just me. So let's analyze this a bit further. 

When plugging in solution \(x^3\), we see some problems in equation stability:


---- stability  =G=  stability constraint

stability(m1,w2).. x(m1,w1) - x(m2,w2) - x(m3,w2) =G= 0 ; (LHS = -1, INFES = 1 ****)

stability(m1,w3).. x(m1,w1) + x(m1,w2) - x(m3,w3) =G= 0 ; (LHS = 0)

stability(m2,w1).. - x(m1,w1) + x(m2,w2) + x(m2,w3) =G= 0 ; (LHS = 0)

stability(m2,w3).. - x(m1,w3) + x(m2,w2) - x(m3,w3) =G= 0 ; (LHS = -1, INFES = 1 ****)

stability(m3,w1).. - x(m1,w1) - x(m2,w1) + x(m3,w3) =G= 0 ; (LHS = -1, INFES = 1 ****)

stability(m3,w2).. - x(m2,w2) + x(m3,w1) + x(m3,w3) =G= 0 ; (LHS = 0)

So introducing the match \((m_1,w_2)\) seems beneficial for both \(m_1\) and \(w_2\). We can see this from:


This means \(x^3\) is not a stable assignment.


Alternative formulation


I have used the stability formulation from [3]: \[\sum_{m'\lt_w m} \color{darkred}x_{m',w} -\sum_{w'\gt_m w} \color{darkred}x_{m,w'} \le 0 \>\> \forall m,w\] In [2] an alternative form of this constraint is used: \[\sum_{w'\gt_m w} \color{darkred}x_{m,w'}+\sum_{m'\gt_w m} \color{darkred}x_{m',w} +  \color{darkred}x_{m,w} \ge 1\>\> \forall m,w\] This is the same constraint, as can be seen from the identity: \[\sum_{m'\lt_w m} \color{darkred}x_{m',w} + \color{darkred}x_{m,w} + \sum_{m'\gt_w m} \color{darkred}x_{m',w}=1\]


Conclusion


Even a small \(3 \times 3\) problem can be difficult to analyze manually without a model.
 

References


  1. Stable marriage problem, https://en.wikipedia.org/wiki/Stable_marriage_problem 
  2. Alvin E. Roth, Uriel G. Rothblum and John H. Vande Vate, Stable Matchings, Optimal Assignments, and Linear Programming, Mathematics of Operations Research, Vol. 18, No. 4 (1993), pp. 803-828
  3. John H. Vande Vate, Linear Programming brings Marital Bliss, Operations Research Letters 8 (1989) pp. 147-153. 

Appendix: GAMS code


$ontext

 
Stable Marriage Problem

 
1. John H. Vande Vate
    
LINEAR PROGRAMMING BRINGS MARITAL BLISS
    
Operations Research Letters 8 (1989) pp. 147-153

 
2. Alvin E. Roth, Uriel G. Rothblum and John H. Vande Vate
    
Stable Matchings, Optimal Assignments, and Linear Programming
    
Mathematics of Operations Research
    
Vol. 18, No. 4 (1993), pp. 803-828



$offtext


*----------------------------------------------------------------
* data
*----------------------------------------------------------------


set
  m
'men'    /m1*m3/
  w
'women'  /w1*w3/
;
alias (m,mm),(w,ww);


table mpref(m,w) 'preferences of men for women (ranking,1=best)'
   
w1 w2 w3
m1   1  2  3
m2   3  1  2
m3   2  3  1
;

table wpref(w,m) 'preferences of women for men (ranking,1=best)'
   
m1 m2 m3
w1   3  2  1
w2   1  3  2
w3   2  1  3
;


abort$(card(m)<>card(w)) "Equal-sized sets expected",m,w;

scalar n 'size of problem';
n =
card(w);

*----------------------------------------------------------------
* derived data
*----------------------------------------------------------------

sets
  worseMen(w,m,mm)     
'worse men than m for w'
  betterWomen(m,w,ww)  
'better women than w for m'
;
worseMen(w,m,mm) = wpref(w,mm)>wpref(w,m);
betterWomen(m,w,ww) = mpref(m,ww)<mpref(m,w);

option mpref:0,wpref:0;
display m,w,mpref,wpref,worseMen,betterWomen;


*----------------------------------------------------------------
* MIP model
*----------------------------------------------------------------

binaryvariable x(m,w) 'assignment';

variable z 'dummy objective';

equations
   obj           
'dummy objective'
   assign1(w)    
'assignment constraint'
   assign2(m)    
'assignment constraint'
   stability(m,w)
'stability constraint'
;


obj.. z =e= 0;

assign1(w)..
sum(m, x(m,w)) =e= 1;
assign2(m)..
sum(w, x(m,w)) =e= 1;

* formulation from [1]
* if m marries someone worse than w then w must marry someone better than m
* otherwise we can improve both w and m
stability(m,w)..
 
sum(betterWomen(m,w,ww),x(m,ww)) =g= sum(worseMen(w,m,mm),x(mm,w));

model StableMarriage /all/;
solve StableMarriage using mip minimizing z;

option x:0;
display x.l;

*----------------------------------------------------------------
* generate all feasible solutions
*----------------------------------------------------------------

sets
   sol
'max number of solutions'/sol1*sol5/
   s(sol)
'dynamic subset'
;

equation cut(sol) 'no good constraints';

parameter solutions(sol,m,w) 'enumerated solutions';

cut(s)..
sum((m,w), solutions(s,m,w)*x(m,w)) =l= n-1;


model Enumerate /all/;

s(sol) =
no;
solutions(sol,m,w) = 0;
loop(sol,
   
solve Enumerate using mip minimizing z;
   
break$(Enumerate.ModelStat <> %modelStat.optimal% and
        Enumerate.ModelStat <> %modelStat.integerSolution%);

    solutions(sol,m,w) = round(x.l(m,w));
    s(sol) =
yes;
);

option solutions:0:1:1;
display solutions;


*----------------------------------------------------------------
* check solution from [2]
*----------------------------------------------------------------


parameter sol2(m,w) 'solution 3 from [2]'/
      
m2.w1 1
      
m3.w2 1
      
m1.w3 1
/;
option sol2:0;
display sol2;

x.fx(m,w) = sol2(m,w);

solve StableMarriage using mip minimizing z;



Viewing all articles
Browse latest Browse all 809

Trending Articles