This file contains 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
$critics=array('Lisa Rose'=> array('Lady in the Water' =>2.5, 'Snakes on a Plane' =>3.5, | |
'Just My Luck'=> 3.0, 'Superman Returns'=> 3.5, 'You, Me and Dupree'=> 2.5, | |
'The Night Listener'=> 3.0), | |
'Gene Seymour' => array('Lady in the Water'=> 3.0, 'Snakes on a Plane'=> 3.5, | |
'Just My Luck'=> 1.5, 'Superman Returns'=> 5.0, 'The Night Listener'=> 3.0, | |
'You, Me and Dupree'=> 3.5), | |
'Michael Phillips'=> array('Lady in the Water'=> 2.5, 'Snakes on a Plane'=> 3.0, | |
'Superman Returns'=> 3.5, 'The Night Listener'=> 4.0), | |
'Claudia Puig'=> array('Snakes on a Plane'=> 3.5, 'Just My Luck'=> 3.0, | |
'The Night Listener'=> 4.5, 'Superman Returns'=> 4.0, |
This file contains 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
function sim_distance($person1, $person2){ | |
$si = array(); | |
foreach ($person1 as $k => $v) { | |
if (array_key_exists($k, $person2)) { | |
$si[$k] = 1; | |
} | |
} | |
if (count($si) == 0) { | |
return 0; | |
} |
This file contains 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
function getRecommendations($array, $person, $type) { | |
$totals = array(); | |
$simSums = array(); | |
$rankings = array(); | |
foreach ($array as $other => $v) { | |
if ($other == $person ){ | |
continue; | |
} elseif($type == 'pearson') { | |
$simulate = sim_pearson($array[$person], $array[$other]); | |
if($simulate <= 0) { |
This file contains 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
function transformPrefs($critics) { | |
$result = array(); | |
foreach ($critics as $key => $person) { | |
foreach ($person as $item => $value) { | |
if(!isset($result[$item])){ | |
$resutl[$item] = array(); | |
} | |
$result[$item][$key] = $value; | |
} | |
} |
This file contains 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
function calculateSimilarItems($critics, $n) { | |
$result = array(); | |
$itemPrefs = transformPrefs($critics); | |
$c = 0; | |
foreach($itemPrefs as $item => $v) { | |
$c++; | |
if( $c%100 == 0){ | |
echo sprintf("%d / %d", $c, count($itemPrefs)); | |
} | |
$scores = topMatches($itemPrefs, $item, $n, 'distance'); |
This file contains 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
function loadMovieLens($path) { | |
$movies = array(); | |
$fp = fopen($path . "/u.item", "r"); | |
while($line = fgets($fp)){ | |
$line_explode = explode("|", $line); | |
$movies[$line_explode[0]] = $line_explode[1]; | |
} | |
fclose($fp); | |
$prefs = array(); | |
$fp = fopen($path . "/u.data", "r"); |
This file contains 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
$people = array(array('Symour', 'BOS'), array('Franny', 'DAL'), array('Zooey', 'CAK'), array('Walt', 'MIA'), array('Buddy', 'ORD'), array('Les', 'OMA')); | |
$destination = 'LGA'; | |
$flights = array(); | |
$fp = fopen("schedule.txt", "r"); | |
while($line = fgets($fp)) { | |
$line = rtrim($line); | |
$explode_line = explode(",",$line); | |
$origin = $explode_line[0]; | |
$dest = $explode_line[1]; | |
$depart = $explode_line[2]; |
This file contains 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
$s = array(1,4,3,2,7,3,6,3,2,4,5,3); | |
function printschedule($r, $people, $flights, $destination) { | |
$d = count($r)/2; | |
for($i = 0; $i < $d; $i++){ | |
$name = $people[$i][0]; | |
$origin = $people[$i][1]; | |
$out = $flights[$origin][$destination][$r[($i*2)]]; | |
$ret = $flights[$destination][$origin][$r[($i*2+1)]]; | |
echo sprintf("%10s%10s %5s-%5s $%3s %5s-%5s $%3s" . PHP_EOL, $name, $origin, $out[0], $out[1], $out[2], $ret[0], $ret[1], $ret[2]); |
This file contains 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
function schedulecost($sol, $origin, $people, $flights, $destination) { | |
$totalprice = 0; | |
$latestarrival = 0; | |
$earliestdep = 24*60; | |
$d = count($sol)/2; | |
for ($i = 0; $i < $d; $i++) { | |
$origin = $people[$i][1]; | |
$outbound = $flights[$origin][$destination][$sol[$i*2]]; | |
$returnf = $flights[$destination][$origin][$sol[$i*2+1]]; |
This file contains 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
function randomoptimize($domain, $origin, $people, $flights, $destination) { | |
$best = 999999999; | |
$bestr = 'null'; | |
for($i = 0; $i < 10000; $i++) { | |
$r = array(); | |
for($s = 0; $s < count($domain); $s++) { | |
$r[] = rand($domain[$s][0], $domain[$s][1]); | |
} | |
$cost = schedulecost($r, $origin, $people, $flights, $destination); |
OlderNewer