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 geneticoptimize($domain, $origin, $people, $flights, $destination, $popsize = 50, $step=1, $mutprob=0.2, $elite=0.2, $maxiter = 100) { | |
function mutate($vec, $domain, $step) { | |
$i = rand(0, (count($domain) - 1)); | |
$rand = rand(1,10000)/10000; | |
if(($rand < 0.5) && ($vec[$i] > $domain[$i][0])) { | |
$vec[$i] = $vec[$i] - $step; | |
return $vec; | |
} elseif($vec[$i] < $domain[$i][1]) { | |
$vec[$i] = $vec[$i] + $step; | |
return $vec; |
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 annealingoptimize($domain, $origin, $people, $flights, $destination, $T=10000.0, $cool=0.95, $step=1) { | |
$vec = array(); | |
for($s = 0; $s < count($domain); $s++) { | |
$vec[] = rand($domain[$s][0], $domain[$s][1]); | |
} | |
while ( $T > 0.1 ) { | |
$i = rand(0, (count($domain) - 1) ); | |
$dir = rand( -$step, $step); | |
$vecb = $vec; |
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 hillclimb($domain, $origin, $people, $flights, $destination) { | |
$r = array(); | |
for($s = 0; $s < count($domain); $s++) { | |
$r[] = rand($domain[$s][0], $domain[$s][1]); | |
} | |
while(1){ | |
$neighbors = array(); | |
for ($k = 0; $k < count($domain); $k++) { | |
if($r[$k] > $domain[$k][0]) { | |
$neighbors[] = array_replace($r, array($k => $r[$k]-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); |
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
$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
$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
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
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 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; | |
} | |
} |
NewerOlder