【发布时间】: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