【问题标题】:One-Way Stable Mariage Matching单向稳定婚姻匹配
【发布时间】:2014-02-04 20:21:15
【问题描述】:

我必须匹配列表管理员及其首选学校。每位经理都会写下他们最喜欢的 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);
    }  

}

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

为什么?!

【问题讨论】:

    标签: php sorting match matching


    【解决方案1】:

    您拥有的是transportation problem

    很遗憾,我在 PHP 中找不到完整的实现,但是 here's one description of the algorithm

    本质上,运输问题被定义为:

    拥有

    • m 来源,每个来源产生 M[] 单位,并且
    • n 目的地每个都需要 N[] 单位,并且
    • 从源i 到目标j 提供一个单位的成本由矩阵A[i][j] 定义。

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

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

    【讨论】:

    • 谢谢!我会看看你提供的链接。
    猜你喜欢
    • 1970-01-01
    • 2012-07-16
    • 2012-06-25
    • 2013-08-31
    • 1970-01-01
    • 2012-01-11
    • 2014-03-13
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多