Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ryokuchaneko/5999810 to your computer and use it in GitHub Desktop.
Save Ryokuchaneko/5999810 to your computer and use it in GitHub Desktop.
擬似アニーリングによる最適化関数。ここでは、旅程のコストの最適値を求めているが、コスト関数を入れ替えれば他の問題にも適用できます。
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