";
print_r($form_list);
echo "";
}
// Extract List of Applicants to Array
if (isset($form_list["applicants"]) && strlen($form_list["applicants"]) > 0) {
$applicants = array_map('trim', explode(PHP_EOL, $form_list["applicants"])); // List of applicants from user
$num_submissions = count($applicants);
// Limit User Inputs to Max of their applicant entries
$k = min($k, $num_submissions); // K = # of students applicants
$rand_finalist_limit = (int) ($num_submissions * (rand(70, 100) / 100)); //(create a random percentage (between 70% - 100%) to generate number of accepted applicants )
$a = min($a, $rand_finalist_limit); // A = # of choosen applicants
// B = # of Possible banned pairings based on permutation of n! / (n-r)!
// Also generate number of groupings based on percentage.
// Example 500 possible pairings * 90% will generate a ban list of 450 pairings.
$lower_perm = $k - $r;
$factk = fact($k);
$factl = fact($lower_perm);
$q = (int)($a/$r); // Q = # of required pairings
$b = (int)(($factk / $factl) - $q ); //Total possible pairings - required pairings = possible max banned list
} else {
try {
/* Variable List - Default Values */
$applicants_text = explode(PHP_EOL, file_get_contents("student_list.txt"));
shuffle($applicants_text);
$applicants = array_slice($applicants_text, 0, $k);
if ($GLOBALS['show_debug']) {
echo "".print_r($applicants, true).""; } } catch(Exception $e) { echo "Could Not Find File student_list.txt: " . $e->getMessage(); exit(); } } // Extract or Generate List of Banned Pairings if (isset($form_list["ban_relations"]) && strlen($form_list["ban_relations"]) > 0) { $ban_relations_list = explode(PHP_EOL, $form_list["ban_relations"]); // List of applicants from user if ($GLOBALS['show_debug']) { echo "
";
print_r($ban_relations_list);
echo "";
echo "";
print_r($applicants);
echo "";
}
$ban_relations = array(); //List of User Inputs
foreach ($ban_relations_list as $pairing) {
$applicant = array_map('trim', explode(",", $pairing));
if ($GLOBALS['show_debug']) {
echo "";
print_r($applicant);
echo "";
}
$temp_arr = array();
for ($v=0; $v < count($applicant); $v++) {
$key_app = array_search($applicant[$v], $applicants);
if ($key_app == false) {
echo "
Banned Pairing: ".$pairing." has an applicant - ".$applicant[$v]." that is not found in the original list of applicants.
Resubmit Your List with banned users that exist in the applicant
'.print_r($trim_ban_relations, true).''; } // create list of ranked applicants & sets their initial ranked values to zero. $ranked_list = array(); for ($i = 0; $i < count($applicants); $i++) { $ranked_list[$applicants[$i]] = 0; } /* If the Number of students per pairing was > 2, Then you would only need to make a function that breaks the pairings into groupings of 2, before processing them. Ex: Banned pairing: student_1, student_2, student_3 would become : student_1, student_3 student_1, student_2 student_3, student_2 */ //Create Ranked List and Sort List of Applicants By # of Bad Relations $ranked_list = rank_users($trim_ban_relations); if ($GLOBALS['show_debug']) { echo '
Create Ranked List and Sort List of Applicants By # of Bad Pairings each applicant is listed in
'; echo "";
print_r($ranked_list);
echo "";
}
// Show Results
renderApplicants($k, $r, $a, $q, $b, $applicants);
renderCombinations($k, $ban_relations);
renderBannedList($b, $ban_relations, $trim_ban_relations);
pair_delist($ranked_list, $q_list, $q, $trim_ban_relations, $applicants);
}
/* Functions */
// Create rank_list from banned applicant relationships
function rank_users($arr_banned) {
$ranked_list = array();
foreach($arr_banned as $banned) {
//check and increment first and last element of each random pair found in ban list.
if (empty($ranked_list[$banned[array_key_first($banned)]])) {
$ranked_list[$banned[array_key_first($banned)]] = 1;
} else {
$ranked_list[$banned[array_key_first($banned)]]++;
}
if (empty($ranked_list[$banned[array_key_last($banned)]])) {
$ranked_list[$banned[array_key_last($banned)]] = 1;
} else {
$ranked_list[$banned[array_key_last($banned)]]++;
}
}
asort($ranked_list);
return $ranked_list;
}
// Splits ranked list into groupings based on # appearances on the ban list.
function rank_grouping($arr) {
$rank_sort = array();
foreach ($arr as $applicant => $value) {
$rank_sort[$value][] = $applicant;
}
return $rank_sort;
}
function pairingCheck($temparr,$active_ban_relations)
{
// If false, then pair is from ban list
// If true then a match is not found and approved
if ($GLOBALS['show_debug']) {
echo 'Checks Potential Pairing against Banned List of Pairings
found a similarity between ((".$temparr[0]." == ".$ban_pair[$bf_key].") && (".$temparr[1]." == ".$ban_pair[$bl_key].")) || ((".$temparr[0]." == ".$ban_pair[$bl_key].") && (".$temparr[1]." == ".$ban_pair[$bf_key]."))
"; } return false; } } return true; } function pair_delist($ranked_list, $q_list, $q, $active_ban_relations,$applicants){ if ($GLOBALS['show_debug']) { echo ''.print_r($q_list).''; } // Group List By Ranks $rank_group = rank_grouping($ranked_list); $f_key = array_key_first($rank_group); $l_key = array_key_last($rank_group); if (count($q_list) >= $q) { // If count of qualified list >= quota size $list = ""; foreach ($q_list as $accepted_pairing) { $list .= implode(', ', $accepted_pairing)."
Checking Pairing: '.print_r($temparr).'
Testing against ban pair: '.print_r($ban).'
Checking Pairing: '.print_r($temparr).''; echo '
Ban List Item: '.print_r($ban).''; } unset($active_ban_relations[$bankey]); } } if ($GLOBALS['show_debug']) { echo "
'.print_r($squashed_list, true).''; echo "
'.print_r($applicants, true).''; echo "
'.print_r($total_remaining_apps, true).''; echo "
'.print_r($selected_apps, true).''; } for ($i = 0; $i < (count($selected_apps)/2); $i+=2) { // Take all the lowest value keys applicants and combine them with the highest value keys applicants . IE: add them to the completed approved list. //When array doesnt intersect with the ban list, then add it as qualified. $temparr = array($selected_apps[$i],$selected_apps[$i+1]); $q_list[] = $temparr; } } pair_delist($ranked_list, $q_list, $q, $active_ban_relations,$applicants); } else { // completed approved list is less than the student pair quota. if (empty($q_list) || count($q_list) < $q) { for ($i = 0; $i < min(count($rank_group[$f_key]),count($rank_group[$l_key])); $i++) { // Take all the lowest value keys applicants and combine them with the highest value keys applicants . IE: add them to the completed approved list. // Set both applicants to rank -1 on the active rank list. //When array doesnt intersect with the ban list, then add it as approved. $temparr = array($rank_group[$f_key][$i],$rank_group[$l_key][$i]); if ($GLOBALS['show_debug']) { echo '
Checking Pairing: '.print_r($temparr, true).'
Testing against ban pair: '.print_r($ban, true).'
Checking Pairing: '.print_r($temparr, true).''; echo '
Ban List Item: '.print_r($ban, true).''; } unset($active_ban_relations[$bankey]); } } if ($GLOBALS['show_debug']) { echo "
Student List ('.$k.' Applicants | '.$r.' Students Per Room | '.$a.' Applicants Can Be Chosen | '.$q.' Acceptable Pairings)
This is using all possible combinations to create a list of bad pairings to pull from.
'; } function renderBannedList($b, $ban_relations, $trim_ban_relations) { echo 'This shuffles the All-Combinations applicants list, and selects the first '.min($b,count($ban_relations)).' pairs to create a dean ban list.
'; if (!$GLOBALS['show_debug'] || ($GLOBALS['show_debug']) == false) { echo '