Created
July 15, 2013 13:10
-
-
Save Ryokuchaneko/5999810 to your computer and use it in GitHub Desktop.
擬似アニーリングによる最適化関数。ここでは、旅程のコストの最適値を求めているが、コスト関数を入れ替えれば他の問題にも適用できます。
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; | |
$vecb[$i] += $dir; | |
if($vecb[$i] < $domain[$i][0]){ | |
$vecb[$i] = $domain[$i][0]; | |
} elseif ($vecb[$i] > $domain[$i][1]) { | |
$vecb[$i] = $domain[$i][1]; | |
} | |
$ea = schedulecost($vec, $origin, $people, $flights, $destination); | |
$eb = schedulecost($vecb, $origin, $people, $flights, $destination); | |
$p = exp(-abs($eb-$ea)/$T); | |
$rand = rand(1,10000)/10000; | |
if (($eb < $ea) || ($rand < $p)) { | |
var_dump('A'); | |
$vec = $vecb; | |
} | |
$T = $T*$cool; | |
} | |
return $vec; | |
} | |
$result = annealingoptimize($domain, $origin, $people, $flights, $destination, $T=10000.0, $cool=0.95, $step=1); | |
var_dump($result); | |
$cost = schedulecost($result, $origin, $people, $flights, $destination); | |
var_dump($cost); | |
$schedule = printschedule($result, $people, $flights, $destination); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment