单向稳定婚姻匹配

时间:2014-02-04 20:21:15

标签: php sorting match matching

我必须匹配列表管理员和他们喜欢的学校。每位经理都会写下他们最喜欢的5所学校(按顺序)。有24名经理和12所学校;每所学校都有两位经理。

这是我使用的排序逻辑,但它似乎不起作用:

Randomly assign 2 managers to each school. 
Calculate the initial total preference, P.
Create an array of all managers
While managers is not empty:
    Select the first manager, m
    Determine its current assigned university, u'
    Determine its top preferred university, u
    Determine the least preferred manager for u, m'
    Try to switch these two managers ( m->u, m'->u')
    Calculate the new preference, P'
    If( P' > P )
        Accept the switch
        Push the manager, m' to the manager array list
    EndIf
EndWhile

我写的PHP代码是:

// Assume $this->managers has the list of all managers, and $this->universities has the list of universities
// Managers are already assigned to the universities
$managers = $this->managers;
shuffle($managers);
while( !empty($managers) ) 
{
    // Select the Manager
    $m       = array_shift($managers);
    // Find school they are part of
    $current = $this->getCurrentUniversity($m);
    // Find their top school
    $top     = $this->getTopUniversity($m, $current);
    // Find least preferred person for $top
    $least   = $this->getLeastManager($top);
    $try     = $this->switchManagers($m, $current, $top, $least);
    // If this makes it better, then accept it!
    $tryPref = $this->preference($try);
    if( $tryPref > $preference ) {
        $this->universities = $try;
        $preference         = $tryPref;
        array_push($managers, $least);
    }  

}

不用说,它不起作用。它确实使匹配相对更好,但它不是最好的排序。更重要的是,如果重新运行程序,每次我得到一个新的结果。因此匹配不会收敛,并且没有唯一的答案。

为什么?!

1 个答案:

答案 0 :(得分:0)

你拥有的是transportation problem

不幸的是,我无法在PHP中找到完整的实现,只有here's one description of the algorithm

实质上,运输问题定义为:

具有

  • m来源,每个产生M[]个单位,
  • n个目的地,每个目标需要N[]个单位,
  • 从源i向目标j提供一个单位的费用由矩阵A[i][j]定义。

找到这样的交通计划(从每个来源到每个目的地提供的单位数量),总成本最低。

在您的情况下,经理提供供应(每个1个),学校提供需求(每个2个),并且将经理A分配给学校B的成本与A的偏好列表中B的位置相反。