Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ryokuchaneko/5999777 to your computer and use it in GitHub Desktop.
Save Ryokuchaneko/5999777 to your computer and use it in GitHub Desktop.
ランダムサーチによる最小コストの旅程を求める関数。コスト関数を入れ替えれば旅程以外の問題でも最小、最大をランダムサーチによって求めることができる。
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);
if ($cost < $best) {
$best = $cost;
$bestr = $r;
}
}
return $bestr;
}
$domain = array();
for ($i = 0; $i < count($people)*2; $i++) {
$domain[] = array(0, 9);
}
$result = randomoptimize($domain, $origin, $people, $flights, $destination);
$schedule = printschedule($result, $people, $flights, $destination);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment