我正在寻找实现多样化排序的方法。每个单元格都包含一个权重值和一个枚举类型。我希望根据已选择的元素类型动态调整权重值,优先考虑到目前为止“较少选择”的类型。我想控制多样性因素,这样当设置一个高值时,它会产生完全多样化的结果数组,而当给出一个低值时,它将提供一个几乎“常规”排序的数组。
由于这不像是一个非常具体的用例,所以如果有任何已知算法的参考,那也将是很好的。
更新: 根据Ophir的建议,这可能是一个基本的包装器:
由于这不像是一个非常具体的用例,所以如果有任何已知算法的参考,那也将是很好的。
更新: 根据Ophir的建议,这可能是一个基本的包装器:
// these will be the three arrays, one per type
$contentTypeA, $contentTypeB, $contentTypeC;
// sort each by value
sort($contentTypeA);
sort($contentTypeB);
sort($contentTypeC);
// while i didn't get the amount I want or there aren't any more options to chose from
while ($amountChosen < 100 && (count($contentTypeA) + count($contentTypeB) + count($contentTypeC) > 0)) {
$diversifiedContent[] = selectBest($bestA, $bestB, $bestC, &$contentTypeA, &$contentTypeB, &$contentTypeC);
$amountChosen++;
}
$diversifiedContent = array_slice($diversifiedContent, 0, 520);
return $diversifiedContent;
}
function selectBest($bestA, $bestB, $bestC, &$contentTypeA, &$contentTypeB, &$contentTypeC) {
static $typeSelected;
$diversifyFactor = 0.5;
if (?) {
$typeSelected['A']++;
array_shift($contentTypeA);
return $bestA;
}
else if (?) {
$typeSelected['B']++;
array_shift($contentTypeB);
return $bestA;
}
else if (?) {
$typeSelected['C']++;
array_shift($contentTypeC);
return $bestA;
}
}