Last active
July 28, 2016 08:53
-
-
Save intarstudents/729b6abdf56be178d8a53216be6f09a6 to your computer and use it in GitHub Desktop.
FreqCalc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
/* | |
$users = [ | |
"pilot-1" => [XXXX, XYXY, ...], | |
"pilot-2" => [POPO, RARA, ...] | |
]; | |
*/ | |
class FreqCalc { | |
private $_raw = array(); | |
public function __construct($users){ | |
$this->parse_users($users); | |
} | |
private function parse_users($users){ | |
$this->_raw = array( | |
"all" => array(), | |
"users" => array(), | |
"sort" => array() | |
); | |
foreach(array_keys($users) as $key){ | |
foreach($users[$key] as $freq){ | |
$this->_raw["all"][$freq] = 1; | |
$this->_raw["users"][$key][$freq] = 1; | |
} | |
} | |
ksort($this->_raw["all"]); | |
// Handle user sorting (ones with less channels to select first) | |
$user_sort = []; | |
foreach(array_keys($this->_raw["users"]) as $key){ | |
ksort($this->_raw["users"][$key]); | |
$user_sort[count($this->_raw["users"][$key])][] = $key; | |
} | |
ksort($user_sort); | |
foreach(array_values($user_sort) as $keys){ | |
shuffle($keys); | |
foreach($keys as $key){ | |
$this->_raw["sort"][] = $key; | |
} | |
} | |
} | |
public function calc(){ | |
$groups = []; | |
$channel_sep = 50; | |
$harmonics = 20; | |
$imd_val = 20; | |
foreach($this->_raw["sort"] as $key){ | |
$assign = false; | |
$group = 0; | |
while(1){ | |
if (!isset($groups[$group])){ | |
$groups[$group] = [ | |
"assigned" => [], | |
"unusable" => [] | |
]; | |
} | |
foreach(array_keys($this->_raw["users"][$key]) as $freq){ | |
if (isset($groups[$group]["unusable"][$freq]) || isset($groups[$group]["assigned"][$freq])){ | |
continue; | |
} | |
$assign = $freq; | |
} | |
$unusable_tmp = $groups[$group]["unusable"]; | |
if ($assign !== false){ | |
foreach(range($assign - $channel_sep, $assign + $channel_sep) as $f){ | |
$unusable_tmp[$f] = 1; | |
} | |
// Harmonics | |
if (1==1){ | |
$hramonics = array( | |
$assign + 190, $assign - 190, | |
$assign + 240, $assign - 240 | |
); | |
foreach($hramonics as $harmonic){ | |
foreach(range($harmonic - $harmonics, $harmonic + $harmonics) as $f){ | |
if (isset($groups[$group]["assigned"][$f])){ | |
$assigned = false; | |
break 2; | |
} | |
$unusable_tmp[$f] = 1; | |
} | |
} | |
} | |
// IMD | |
if (1==1){ | |
if (!empty($groups[$group]["assigned"])){ | |
$imds = []; | |
foreach(array_keys($groups[$group]["assigned"]) as $imd_pair){ | |
$imds[] = 2 * $imd_pair - $assign; | |
$imds[] = 2 * $assign - $imd_pair; | |
} | |
foreach($imds as $imd){ | |
foreach(range($imd - $imd_val, $imd + $imd_val) as $f){ | |
if (isset($groups[$group]["assigned"][$f])){ | |
$assign = false; | |
break 2; | |
} | |
$unusable_tmp[$f] = 1; | |
} | |
} | |
} | |
} | |
} | |
if ($assign !== false){ | |
$groups[$group]["unusable"] = $unusable_tmp; | |
break; | |
} | |
$group++; | |
} | |
$groups[$group]["assigned"][$assign] = $key; | |
} | |
$ret = []; | |
foreach($groups as $group){ | |
$ret[] = $group["assigned"]; | |
} | |
return $ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment