Php 单向稳定海况匹配

Php 单向稳定海况匹配,php,sorting,match,matching,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 f

我必须列出经理和他们喜欢的学校。每位管理者将按顺序写下他们最喜欢的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实现,但是

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

拥有

  • 产生
    m[]
    单位的
    m
    源,以及
  • n
    每个需要
    n[]
    单位的目的地,以及
  • 从源
    i
    到目标
    j
    提供一个单元的成本由矩阵
    A[i][j]
    定义
找到这样一个运输计划(从每个来源到每个目的地提供的单位数量),使总成本最小


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

谢谢!我会看看你提供的链接。