I'm looking for a way to implement a diversified sort. Each cell contains a weight value along with an enum type. I would like to sort it in a way that it will make the weight value dynamic according to the types of elements that were already chosen, giving priority to those 'less chosen' so far. I would like to control the diversity factor, so that when setting it with a high value, it'll produce a fully diverse results array, and when giving a low value it will provide an almost 'regular' sorted array.
This doesn't sound like a very specific use case, so if there are any references to known algorithms, that will also be great.
Update: According to Ophir suggestion, this might be a basic wrapper:
// 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;
}
}