From ce616c12e89dcc1413c268c457cd78066a964ade Mon Sep 17 00:00:00 2001 From: Fukuda Naoto Date: Fri, 22 Feb 2019 22:27:16 +0900 Subject: [PATCH 1/3] Fix indent --- checkmaker.php | 266 +++++++-------- index.php | 139 ++++---- plaid.php | 856 ++++++++++++++++++++++++------------------------- 3 files changed, 633 insertions(+), 628 deletions(-) diff --git a/checkmaker.php b/checkmaker.php index 2829151..716955f 100644 --- a/checkmaker.php +++ b/checkmaker.php @@ -1,165 +1,165 @@ evaluation = null; - $this->chrom = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - } + public function __construct() + { + $this->evaluation = null; + $this->chrom = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + } - // 染色体をランダムで初期化 - public function init_chrom() - { - $this->chrom[0] = rand(0, 7); - for ($i = 1; $i < 13; $i++) { - $this->chrom[$i] = rand(0, 255); - } + // 染色体をランダムで初期化 + public function init_chrom() + { + $this->chrom[0] = rand(0, 7); + for ($i = 1; $i < 13; $i++) { + $this->chrom[$i] = rand(0, 255); } + } - // チェック柄の種類を取得 - public function get_plaid_id() - { - return $this->chrom[0]; - } + // チェック柄の種類を取得 + public function get_plaid_id() + { + return $this->chrom[0]; + } - // 色情報を取得 - public function get_color($color_num) - { - return "rgb({$this->chrom[1 * $color_num]},{$this->chrom[2 * $color_num]},{$this->chrom[3 * $color_num]})"; - } + // 色情報を取得 + public function get_color($color_num) + { + return "rgb({$this->chrom[1 * $color_num]},{$this->chrom[2 * $color_num]},{$this->chrom[3 * $color_num]})"; + } } // Checkmakerクラス class Checkmaker { - public $population_num; // 個体数 - public $mutation_probability; // 突然変異確率 + public $population_num; // 個体数 + public $mutation_probability; // 突然変異確率 - public $population = []; // 集団 + public $population = []; // 集団 - public function __construct($population_num, $mutation_probability) - { - $this->population_num = $population_num; - $this->mutation_probability = $mutation_probability; - for ($i = 0; $i < $this->population_num; $i++) { - $this->population[$i] = new Individual(); - } + public function __construct($population_num, $mutation_probability) + { + $this->population_num = $population_num; + $this->mutation_probability = $mutation_probability; + for ($i = 0; $i < $this->population_num; $i++) { + $this->population[$i] = new Individual(); } + } - // 初期集団生成 - public function init_population() - { - for ($i = 0; $i < $this->population_num; $i++) { - $this->population[$i]->init_chrom(); - } + // 初期集団生成 + public function init_population() + { + for ($i = 0; $i < $this->population_num; $i++) { + $this->population[$i]->init_chrom(); } + } - // 評価値設定 - public function set_evaluations($evaluations) - { - for ($i = 0; $i < $this->population_num; $i++) { - $this->population[$i]->evaluation = $evaluations[$i]; - } + // 評価値設定 + public function set_evaluations($evaluations) + { + for ($i = 0; $i < $this->population_num; $i++) { + $this->population[$i]->evaluation = $evaluations[$i]; } + } - // 次世代生成 - public function generate_next() - { - $evaluation_sum = 0; - $next_population = []; + // 次世代生成 + public function generate_next() + { + $evaluation_sum = 0; + $next_population = []; - foreach ($this->population as $individual) { - $evaluation_sum += $individual->evaluation; + foreach ($this->population as $individual) { + $evaluation_sum += $individual->evaluation; + } + for ($i = 0; $i < $this->population_num; $i++) { + $parent1 = new Individual(); + $parent2 = new Individual(); + $child = new Individual(); + // ルーレット選択(親1) + $roulette_target_num = rand(0, $evaluation_sum); + $roulette_current_num = 0; + foreach ($this->population as $individual) { + $roulette_current_num += $individual->evaluation; + if ($roulette_current_num >= $roulette_target_num) { + for ($j = 0; $j < 13; $j++) { + $parent1->chrom[$j] = $individual->chrom[$j]; + } + break; + } + } + // ルーレット選択(親2) + $roulette_target_num = rand(0, $evaluation_sum); + $roulette_current_num = 0; + foreach ($this->population as $individual) { + $roulette_current_num += $individual->evaluation; + if ($roulette_current_num >= $roulette_target_num) { + for ($j = 0; $j < 13; $j++) { + $parent2->chrom[$j] = $individual->chrom[$j]; + } + break; } - for ($i = 0; $i < $this->population_num; $i++) { - $parent1 = new Individual(); - $parent2 = new Individual(); - $child = new Individual(); - // ルーレット選択(親1) - $roulette_target_num = rand(0, $evaluation_sum); - $roulette_current_num = 0; - foreach ($this->population as $individual) { - $roulette_current_num += $individual->evaluation; - if ($roulette_current_num >= $roulette_target_num) { - for ($j = 0; $j < 13; $j++) { - $parent1->chrom[$j] = $individual->chrom[$j]; - } - break; - } - } - // ルーレット選択(親2) - $roulette_target_num = rand(0, $evaluation_sum); - $roulette_current_num = 0; - foreach ($this->population as $individual) { - $roulette_current_num += $individual->evaluation; - if ($roulette_current_num >= $roulette_target_num) { - for ($j = 0; $j < 13; $j++) { - $parent2->chrom[$j] = $individual->chrom[$j]; - } - break; - } - } - // 一様交叉 - for ($j = 0; $j < 13; $j++) { - $child->chrom[$j] = rand(0, 1) == 0 ? $parent1->chrom[$j] : $parent2->chrom[$j]; - } - // 突然変異 - for ($j = 0; $j < 13; $j++) { - if (rand(0, 100) < $this->mutation_probability * 100) { - $child->chrom[$j] = $j == 0 ? rand(0, 7) : rand(0, 255); - } - } - array_push($next_population, $child); + } + // 一様交叉 + for ($j = 0; $j < 13; $j++) { + $child->chrom[$j] = rand(0, 1) == 0 ? $parent1->chrom[$j] : $parent2->chrom[$j]; + } + // 突然変異 + for ($j = 0; $j < 13; $j++) { + if (rand(0, 100) < $this->mutation_probability * 100) { + $child->chrom[$j] = $j == 0 ? rand(0, 7) : rand(0, 255); } - $this->population = $next_population; + } + array_push($next_population, $child); } + $this->population = $next_population; + } - // 最良の個体を取得 - public function get_best_individual() - { - $this->sort_population(); - $max_evaluation = $this->population[0]->evaluation; - $result_population = []; - foreach ($this->population as $individual) { - if ($individual->evaluation == $max_evaluation) { - array_push($result_population, $individual); - } - } - $result_population_num = count($result_population); - if ($result_population_num > 1) { - $result_individual = $result_population[rand(0, $result_population_num - 1)]; - } elseif ($result_population_num > 0) { - $result_individual = $result_population[0]; - } - return $result_individual; + // 最良の個体を取得 + public function get_best_individual() + { + $this->sort_population(); + $max_evaluation = $this->population[0]->evaluation; + $result_population = []; + foreach ($this->population as $individual) { + if ($individual->evaluation == $max_evaluation) { + array_push($result_population, $individual); + } } + $result_population_num = count($result_population); + if ($result_population_num > 1) { + $result_individual = $result_population[rand(0, $result_population_num - 1)]; + } elseif ($result_population_num > 0) { + $result_individual = $result_population[0]; + } + return $result_individual; + } - // 集団を評価降順にソート - private function sort_population() - { - for ($i = 0; $i < count($this->population) - 1; $i++) { - for ($j = 0; $j < count($this->population) - 1; $j++) { - if ($this->population[$j]->evaluation < $this->population[$j + 1]->evaluation) { - $tmp = $this->population[$j]; - $this->population[$j] = $this->population[$j + 1]; - $this->population[$j + 1] = $this->population[$j]; - } - } + // 集団を評価降順にソート + private function sort_population() + { + for ($i = 0; $i < count($this->population) - 1; $i++) { + for ($j = 0; $j < count($this->population) - 1; $j++) { + if ($this->population[$j]->evaluation < $this->population[$j + 1]->evaluation) { + $tmp = $this->population[$j]; + $this->population[$j] = $this->population[$j + 1]; + $this->population[$j + 1] = $this->population[$j]; } + } } + } - public function print_population() - { - foreach ($this->population as $index => $individual) { - echo $index . ":"; - foreach ($individual->chrom as $gene) { - printf("%3d ", $gene); - } - echo "\n"; - } + public function print_population() + { + foreach ($this->population as $index => $individual) { + echo $index . ":"; + foreach ($individual->chrom as $gene) { + printf("%3d ", $gene); + } + echo "\n"; } -} \ No newline at end of file + } +} diff --git a/index.php b/index.php index dec509e..abdcec3 100644 --- a/index.php +++ b/index.php @@ -2,8 +2,6 @@ require_once "checkmaker.php"; require_once "plaid.php"; -session_start(); - $generation_num = 5; $population_num = 10; $mutation_probability = 0.1; @@ -14,47 +12,47 @@ $plaid_list = []; for ($i = 0; $i < 10; $i++) { - $eva = isset($_GET["plaid$i"]) ? $_GET["plaid$i"] : 0; - array_push($evaluations, $eva); - $evaluation_sum += $eva; + $eva = isset($_GET["plaid$i"]) ? $_GET["plaid$i"] : 0; + array_push($evaluations, $eva); + $evaluation_sum += $eva; } if ($evaluation_sum == 0 && $generation_count != 1) { - echo "
評価を選択してください
"; - exit(); + echo "
評価を選択してください
"; + exit(); } if ($generation_count == 1) { - $is_first = []; - for ($i = 1; $i <= $generation_num; $i++) { - $is_first[$i] = true; - } - $cm = new Checkmaker($population_num, $mutation_probability); - $cm->init_population(); - $is_first[$generation_count] = false; - $_SESSION["is_first"] = $is_first; - $_SESSION["cm"] = serialize($cm); + $is_first = []; + for ($i = 1; $i <= $generation_num; $i++) { + $is_first[$i] = true; + } + $cm = new Checkmaker($population_num, $mutation_probability); + $cm->init_population(); + $is_first[$generation_count] = false; + $_SESSION["is_first"] = $is_first; + $_SESSION["cm"] = serialize($cm); } elseif ($generation_count > $generation_num) { - $cm = unserialize($_SESSION["cm"]); - $cm->set_evaluations($evaluations); - $result_individual = $cm->get_best_individual(); - $plaid_id = $result_individual->get_plaid_id(); - $bg_col = $result_individual->get_color(1); - $col1 = $result_individual->get_color(2); - $col2 = $result_individual->get_color(3); - $col3 = $result_individual->get_color(4); - $result_plaid = new Plaid($plaid_id, $bg_col, $col1, $col2, $col3); + $cm = unserialize($_SESSION["cm"]); + $cm->set_evaluations($evaluations); + $result_individual = $cm->get_best_individual(); + $plaid_id = $result_individual->get_plaid_id(); + $bg_col = $result_individual->get_color(1); + $col1 = $result_individual->get_color(2); + $col2 = $result_individual->get_color(3); + $col3 = $result_individual->get_color(4); + $result_plaid = new Plaid($plaid_id, $bg_col, $col1, $col2, $col3); } else { - $is_first = $_SESSION["is_first"]; - $cm = unserialize($_SESSION["cm"]); - if ($is_first[$generation_count] == true) { - $cm->set_evaluations($evaluations); - $cm->generate_next(); - $is_first[$generation_count] = false; - } - var_dump($is_first); - $_SESSION["is_first"] = $is_first; - $_SESSION["cm"] = serialize($cm); + $is_first = $_SESSION["is_first"]; + $cm = unserialize($_SESSION["cm"]); + if ($is_first[$generation_count] == true) { + $cm->set_evaluations($evaluations); + $cm->generate_next(); + $is_first[$generation_count] = false; + } + var_dump($is_first); + $_SESSION["is_first"] = $is_first; + $_SESSION["cm"] = serialize($cm); } // echo "
";
@@ -62,54 +60,61 @@
 // echo "
"; foreach ($cm->population as $individual) { - $plaid_id = $individual->get_plaid_id(); - $bg_col = $individual->get_color(1); - $col1 = $individual->get_color(2); - $col2 = $individual->get_color(3); - $col3 = $individual->get_color(4); + $plaid_id = $individual->get_plaid_id(); + $bg_col = $individual->get_color(1); + $col1 = $individual->get_color(2); + $col2 = $individual->get_color(3); + $col3 = $individual->get_color(4); - array_push($plaid_list, new Plaid($plaid_id, $bg_col, $col1, $col2, $col3)); + array_push($plaid_list, new Plaid($plaid_id, $bg_col, $col1, $col2, $col3)); } ?> + - - - - チェックメーカー(PHP版) + + + + チェックメーカー(PHP版) + -

チェックメーカー(PHP版)

- -

世代:残り

+

チェックメーカー(PHP版)

+ +

第 + 世代:残り + +

- $plaid): ?> + $plaid): ?>
- +
- -
- - - -
+ +
+ + + +
- -
- - + +
+ +
- +

あなたにおすすめのチェック柄です

- +
もう一度 - + - + + \ No newline at end of file diff --git a/plaid.php b/plaid.php index 15b88f3..b1183a5 100644 --- a/plaid.php +++ b/plaid.php @@ -2,434 +2,434 @@ class Plaid { - public $plaid_id, $bg_color, $color1, $color2, $color3; - - public function __construct($plaid_id, $bg_color, $color1, $color2, $color3) - { - $this->plaid_id = $plaid_id; - $this->bg_color = $bg_color; - $this->color1 = $color1; - $this->color2 = $color2; - $this->color3 = $color3; + public $plaid_id, $bg_color, $color1, $color2, $color3; + + public function __construct($plaid_id, $bg_color, $color1, $color2, $color3) + { + $this->plaid_id = $plaid_id; + $this->bg_color = $bg_color; + $this->color1 = $color1; + $this->color2 = $color2; + $this->color3 = $color3; + } + + public function get_plaid_image() + { + $image = new Imagick(); + $draw = new ImagickDraw(); + + switch ($this->plaid_id) { + # グラフチェック + case 0: + $draw->pushPattern("pattern", 0, 0, 50, 50); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 50, 50); + $draw->setFillColor($this->color1); + $draw->rectangle(10, 0, 12, 50); + $draw->rectangle(0, 35, 50, 37); + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + + break; + # タータン1 + case 1: + $draw->pushPattern("pattern", 0, 0, 60, 60); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 60, 60); + + $draw->setFillColor($this->color2); + $draw->rectangle(5, 0, 35, 65); + $draw->rectangle(0, 5, 60, 35); + + $draw->setFillColor($this->color1); + $draw->rectangle(10, 0, 30, 60); + $draw->rectangle(0, 10, 60, 30); + + $draw->setFillOpacity(0.3); + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 10, 60); + $draw->rectangle(30, 0, 60, 60); + $draw->rectangle(0, 0, 60, 20); + $draw->rectangle(0, 30, 60, 60); + + $draw->setFillOpacity(1.0); + $draw->setFillColor($this->color2); + $draw->rectangle(5, 5, 35, 35); + + $draw->setFillOpacity(0.5); + $draw->setFillColor($this->color1); + $draw->rectangle(5, 10, 35, 30); + $draw->rectangle(10, 5, 30, 35); + + $draw->setFillOpacity(1.0); + + $draw->setFillColor($this->color1); + $draw->rectangle(10, 10, 30, 30); + + $draw->setFillColor($this->color2); + $draw->rectangle(19, 0, 21, 60); + $draw->rectangle(49, 0, 51, 60); + $draw->rectangle(0, 19, 60, 21); + $draw->rectangle(0, 49, 60, 51); + + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; + # タータン2 + case 2: + $draw->pushPattern("pattern", 0, 0, 120, 120); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 120, 120); + + $draw->setFillColor($this->color1); + $draw->rectangle(0, 60, 60, 120); + + $draw->setFillOpacity(0.5); + $draw->setFillColor($this->color1); + $draw->rectangle(0, 0, 60, 120); + $draw->rectangle(0, 60, 120, 120); + + $draw->setFillOpacity(0.5); + $draw->setFillColor($this->color2); + $draw->rectangle(25, 0, 45, 120); + $draw->rectangle(0, 25, 120, 45); + + $draw->setFillOpacity(1.0); + $draw->setFillColor($this->color2); + $draw->rectangle(25, 25, 45, 45); + + $draw->setFillOpacity(0.5); + $draw->setFillColor($this->color3); + $draw->rectangle(48, 0, 51, 120); + $draw->rectangle(54, 0, 57, 120); + $draw->rectangle(108, 0, 111, 120); + $draw->rectangle(114, 0, 117, 120); + $draw->rectangle(0, 48, 120, 51); + $draw->rectangle(0, 54, 120, 57); + $draw->rectangle(0, 108, 120, 111); + $draw->rectangle(0, 114, 120, 117); + + $draw->setFillOpacity(1.0); + $draw->setFillColor($this->color3); + $draw->rectangle(48, 48, 51, 51); + $draw->rectangle(54, 48, 57, 51); + $draw->rectangle(48, 54, 51, 57); + $draw->rectangle(54, 54, 57, 57); + $draw->rectangle(108, 48, 111, 51); + $draw->rectangle(108, 54, 111, 57); + $draw->rectangle(114, 48, 117, 51); + $draw->rectangle(114, 54, 117, 57); + $draw->rectangle(48, 108, 51, 111); + $draw->rectangle(54, 108, 57, 111); + $draw->rectangle(108, 108, 111, 111); + $draw->rectangle(108, 114, 111, 117); + $draw->rectangle(114, 108, 117, 111); + $draw->rectangle(114, 114, 117, 117); + + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; + # タータン3 + case 3: + $draw->pushPattern("pattern", 0, 0, 100, 100); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 100, 100); + + $draw->setFillOpacity(0.4); + $draw->setFillColor($this->color2); + $draw->rectangle(0, 0, 75, 100); + $draw->rectangle(95, 0, 100, 100); + $draw->rectangle(0, 0, 100, 5); + $draw->rectangle(0, 25, 100, 100); + + $draw->setFillOpacity(1.0); + $draw->setFillColor($this->color2); + $draw->rectangle(0, 0, 75, 5); + $draw->rectangle(95, 0, 100, 5); + $draw->rectangle(0, 25, 75, 100); + $draw->rectangle(95, 25, 100, 100); + + $draw->setFillOpacity(0.8); + $draw->setFillColor($this->color1); + $draw->rectangle(6, 0, 8, 100); + $draw->rectangle(10, 0, 12, 100); + $draw->rectangle(14, 0, 34, 100); + $draw->rectangle(36, 0, 56, 100); + $draw->rectangle(58, 0, 60, 100); + $draw->rectangle(62, 0, 64, 100); + $draw->rectangle(0, 36, 100, 38); + $draw->rectangle(0, 40, 100, 42); + $draw->rectangle(0, 44, 100, 64); + $draw->rectangle(0, 66, 100, 86); + $draw->rectangle(0, 88, 100, 90); + $draw->rectangle(0, 92, 100, 94); + + $draw->setFillOpacity(0.3); + $draw->setFillColor($this->color2); + $draw->rectangle(0, 0, 75, 5); + $draw->rectangle(95, 0, 100, 5); + $draw->rectangle(0, 25, 75, 100); + $draw->rectangle(95, 25, 100, 100); + + $draw->setFillOpacity(1.0); + $draw->setFillColor($this->color1); + $draw->rectangle(14, 44, 34, 64); + $draw->rectangle(36, 44, 56, 64); + $draw->rectangle(14, 66, 34, 86); + $draw->rectangle(36, 66, 56, 86); + $draw->rectangle(6, 36, 8, 38); + $draw->rectangle(10, 40, 12, 42); + $draw->rectangle(10, 36, 12, 38); + $draw->rectangle(6, 40, 8, 42); + $draw->rectangle(58, 36, 60, 38); + $draw->rectangle(62, 40, 64, 42); + $draw->rectangle(62, 36, 64, 38); + $draw->rectangle(58, 40, 60, 42); + $draw->rectangle(6, 88, 8, 90); + $draw->rectangle(10, 92, 12, 94); + $draw->rectangle(10, 88, 12, 90); + $draw->rectangle(6, 92, 8, 94); + $draw->rectangle(58, 88, 60, 90); + $draw->rectangle(62, 92, 64, 94); + $draw->rectangle(62, 88, 64, 90); + $draw->rectangle(58, 92, 60, 94); + $draw->rectangle(6, 44, 8, 64); + $draw->rectangle(10, 44, 12, 64); + $draw->rectangle(58, 44, 60, 64); + $draw->rectangle(62, 44, 64, 64); + $draw->rectangle(6, 66, 8, 86); + $draw->rectangle(10, 66, 12, 86); + $draw->rectangle(58, 66, 60, 86); + $draw->rectangle(62, 66, 64, 86); + $draw->rectangle(14, 36, 34, 38); + $draw->rectangle(14, 40, 34, 42); + $draw->rectangle(14, 88, 34, 90); + $draw->rectangle(14, 92, 34, 4); + $draw->rectangle(36, 36, 56, 38); + $draw->rectangle(36, 40, 56, 42); + $draw->rectangle(36, 88, 56, 90); + $draw->rectangle(36, 92, 56, 94); + + $draw->setFillColor($this->color3); + $draw->rectangle(85, 0, 87, 100); + $draw->rectangle(0, 15, 100, 17); + + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; + # 3本線 + case 4: + $draw->pushPattern("pattern", 0, 0, 60, 60); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 60, 60); + + $draw->setFillColor($this->color1); + $draw->rectangle(10, 0, 14, 60); + $draw->rectangle(26, 0, 30, 60); + $draw->rectangle(0, 34, 60, 38); + $draw->rectangle(0, 50, 60, 54); + + $draw->setFillColor($this->color2); + $draw->rectangle(18, 0, 22, 60); + $draw->rectangle(0, 42, 60, 46); + + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; + # ガンクラブチェック + case 5; + $draw->pushPattern("pattern", 0, 0, 120, 120); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 120, 120); + + $draw->setFillColor($this->color1); + $draw->rectangle(10, 70, 40, 100); + + $draw->setFillColor($this->color2); + $draw->rectangle(70, 10, 100, 40); + + $draw->setFillOpacity(0.5); + + $draw->setFillColor($this->color1); + $draw->rectangle(10, 0, 40, 120); + $draw->rectangle(0, 70, 120, 100); + + $draw->setFillColor($this->color2); + $draw->rectangle(70, 0, 100, 120); + $draw->rectangle(0, 10, 120, 40); + + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; + # ギンガムチェック + case 6: + $draw->pushPattern("pattern", 0, 0, 60, 60); + + $draw->setFillColor("white"); + $draw->rectangle(0, 0, 60, 60); + + $draw->setFillColor($this->color1); + $draw->rectangle(10, 10, 40, 40); + + $draw->setFillOpacity(0.5); + $draw->setFillColor($this->color1); + $draw->rectangle(10, 0, 40, 60); + $draw->rectangle(0, 10, 60, 40); + + $draw->popPattern(); + + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; + # アーガイルチェック + case 7: + $draw->pushPattern("pattern", 0, 0, 88, 176); + + $draw->setFillColor($this->bg_color); + $draw->rectangle(0, 0, 88, 176); + + $draw->setFillColor($this->color1); + $draw->pathStart(); + $draw->pathMoveToAbsolute(0, 0); + $draw->pathLineToAbsolute(0, 44); + $draw->pathLineToAbsolute(22, 0); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color2); + $draw->pathStart(); + $draw->pathMoveToAbsolute(22, 0); + $draw->pathLineToAbsolute(44, 44); + $draw->pathLineToAbsolute(66, 0); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color1); + $draw->pathStart(); + $draw->pathMoveToAbsolute(66, 0); + $draw->pathLineToAbsolute(88, 44); + $draw->pathLineToAbsolute(88, 0); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color2); + $draw->pathStart(); + $draw->pathMoveToAbsolute(0, 44); + $draw->pathLineToAbsolute(22, 88); + $draw->pathLineToAbsolute(0, 132); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color1); + $draw->pathStart(); + $draw->pathMoveToAbsolute(22, 88); + $draw->pathLineToAbsolute(44, 44); + $draw->pathLineToAbsolute(66, 88); + $draw->pathLineToAbsolute(44, 132); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color2); + $draw->pathStart(); + $draw->pathMoveToAbsolute(88, 44); + $draw->pathLineToAbsolute(66, 88); + $draw->pathLineToAbsolute(88, 132); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color1); + $draw->pathStart(); + $draw->pathMoveToAbsolute(0, 132); + $draw->pathLineToAbsolute(22, 176); + $draw->pathLineToAbsolute(0, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color2); + $draw->pathStart(); + $draw->pathMoveToAbsolute(22, 176); + $draw->pathLineToAbsolute(44, 132); + $draw->pathLineToAbsolute(66, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setFillColor($this->color1); + $draw->pathStart(); + $draw->pathMoveToAbsolute(66, 176); + $draw->pathLineToAbsolute(88, 132); + $draw->pathLineToAbsolute(88, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setStrokeColor($this->color3); + $draw->pathStart(); + $draw->pathMoveToAbsolute(0, 0); + $draw->pathLineToAbsolute(88, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setStrokeColor($this->color3); + $draw->pathStart(); + $draw->pathMoveToAbsolute(88, 0); + $draw->pathLineToAbsolute(0, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setStrokeColor($this->color3); + $draw->pathStart(); + $draw->pathMoveToAbsolute(44, 0); + $draw->pathLineToAbsolute(0, 88); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setStrokeColor($this->color3); + $draw->pathStart(); + $draw->pathMoveToAbsolute(44, 0); + $draw->pathLineToAbsolute(88, 88); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setStrokeColor($this->color3); + $draw->pathStart(); + $draw->pathMoveToAbsolute(0, 88); + $draw->pathLineToAbsolute(44, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->setStrokeColor($this->color3); + $draw->pathStart(); + $draw->pathMoveToAbsolute(88, 88); + $draw->pathLineToAbsolute(44, 176); + $draw->pathClose(); + $draw->pathFinish(); + + $draw->popPattern(); + $draw->setFillPatternURL("#pattern"); + $draw->rectangle(0, 0, 200, 200); + break; } + $image->newImage(200, 200, new ImagickPixel($this->bg_color)); + $image->drawImage($draw); + $image->setImageFormat("png"); - public function get_plaid_image() - { - $image = new Imagick(); - $draw = new ImagickDraw(); - - switch ($this->plaid_id) { - # グラフチェック - case 0: - $draw->pushPattern("pattern", 0, 0, 50, 50); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 50, 50); - $draw->setFillColor($this->color1); - $draw->rectangle(10, 0, 12, 50); - $draw->rectangle(0, 35, 50, 37); - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - - break; - # タータン1 - case 1: - $draw->pushPattern("pattern", 0, 0, 60, 60); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 60, 60); - - $draw->setFillColor($this->color2); - $draw->rectangle(5, 0, 35, 65); - $draw->rectangle(0, 5, 60, 35); - - $draw->setFillColor($this->color1); - $draw->rectangle(10, 0, 30, 60); - $draw->rectangle(0, 10, 60, 30); - - $draw->setFillOpacity(0.3); - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 10, 60); - $draw->rectangle(30, 0, 60, 60); - $draw->rectangle(0, 0, 60, 20); - $draw->rectangle(0, 30, 60, 60); - - $draw->setFillOpacity(1.0); - $draw->setFillColor($this->color2); - $draw->rectangle(5, 5, 35, 35); - - $draw->setFillOpacity(0.5); - $draw->setFillColor($this->color1); - $draw->rectangle(5, 10, 35, 30); - $draw->rectangle(10, 5, 30, 35); - - $draw->setFillOpacity(1.0); - - $draw->setFillColor($this->color1); - $draw->rectangle(10, 10, 30, 30); - - $draw->setFillColor($this->color2); - $draw->rectangle(19, 0, 21, 60); - $draw->rectangle(49, 0, 51, 60); - $draw->rectangle(0, 19, 60, 21); - $draw->rectangle(0, 49, 60, 51); - - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - # タータン2 - case 2: - $draw->pushPattern("pattern", 0, 0, 120, 120); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 120, 120); - - $draw->setFillColor($this->color1); - $draw->rectangle(0, 60, 60, 120); - - $draw->setFillOpacity(0.5); - $draw->setFillColor($this->color1); - $draw->rectangle(0, 0, 60, 120); - $draw->rectangle(0, 60, 120, 120); - - $draw->setFillOpacity(0.5); - $draw->setFillColor($this->color2); - $draw->rectangle(25, 0, 45, 120); - $draw->rectangle(0, 25, 120, 45); - - $draw->setFillOpacity(1.0); - $draw->setFillColor($this->color2); - $draw->rectangle(25, 25, 45, 45); - - $draw->setFillOpacity(0.5); - $draw->setFillColor($this->color3); - $draw->rectangle(48, 0, 51, 120); - $draw->rectangle(54, 0, 57, 120); - $draw->rectangle(108, 0, 111, 120); - $draw->rectangle(114, 0, 117, 120); - $draw->rectangle(0, 48, 120, 51); - $draw->rectangle(0, 54, 120, 57); - $draw->rectangle(0, 108, 120, 111); - $draw->rectangle(0, 114, 120, 117); - - $draw->setFillOpacity(1.0); - $draw->setFillColor($this->color3); - $draw->rectangle(48, 48, 51, 51); - $draw->rectangle(54, 48, 57, 51); - $draw->rectangle(48, 54, 51, 57); - $draw->rectangle(54, 54, 57, 57); - $draw->rectangle(108, 48, 111, 51); - $draw->rectangle(108, 54, 111, 57); - $draw->rectangle(114, 48, 117, 51); - $draw->rectangle(114, 54, 117, 57); - $draw->rectangle(48, 108, 51, 111); - $draw->rectangle(54, 108, 57, 111); - $draw->rectangle(108, 108, 111, 111); - $draw->rectangle(108, 114, 111, 117); - $draw->rectangle(114, 108, 117, 111); - $draw->rectangle(114, 114, 117, 117); - - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - # タータン3 - case 3: - $draw->pushPattern("pattern", 0, 0, 100, 100); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 100, 100); - - $draw->setFillOpacity(0.4); - $draw->setFillColor($this->color2); - $draw->rectangle(0, 0, 75, 100); - $draw->rectangle(95, 0, 100, 100); - $draw->rectangle(0, 0, 100, 5); - $draw->rectangle(0, 25, 100, 100); - - $draw->setFillOpacity(1.0); - $draw->setFillColor($this->color2); - $draw->rectangle(0, 0, 75, 5); - $draw->rectangle(95, 0, 100, 5); - $draw->rectangle(0, 25, 75, 100); - $draw->rectangle(95, 25, 100, 100); - - $draw->setFillOpacity(0.8); - $draw->setFillColor($this->color1); - $draw->rectangle(6, 0, 8, 100); - $draw->rectangle(10, 0, 12, 100); - $draw->rectangle(14, 0, 34, 100); - $draw->rectangle(36, 0, 56, 100); - $draw->rectangle(58, 0, 60, 100); - $draw->rectangle(62, 0, 64, 100); - $draw->rectangle(0, 36, 100, 38); - $draw->rectangle(0, 40, 100, 42); - $draw->rectangle(0, 44, 100, 64); - $draw->rectangle(0, 66, 100, 86); - $draw->rectangle(0, 88, 100, 90); - $draw->rectangle(0, 92, 100, 94); - - $draw->setFillOpacity(0.3); - $draw->setFillColor($this->color2); - $draw->rectangle(0, 0, 75, 5); - $draw->rectangle(95, 0, 100, 5); - $draw->rectangle(0, 25, 75, 100); - $draw->rectangle(95, 25, 100, 100); - - $draw->setFillOpacity(1.0); - $draw->setFillColor($this->color1); - $draw->rectangle(14, 44, 34, 64); - $draw->rectangle(36, 44, 56, 64); - $draw->rectangle(14, 66, 34, 86); - $draw->rectangle(36, 66, 56, 86); - $draw->rectangle(6, 36, 8, 38); - $draw->rectangle(10, 40, 12, 42); - $draw->rectangle(10, 36, 12, 38); - $draw->rectangle(6, 40, 8, 42); - $draw->rectangle(58, 36, 60, 38); - $draw->rectangle(62, 40, 64, 42); - $draw->rectangle(62, 36, 64, 38); - $draw->rectangle(58, 40, 60, 42); - $draw->rectangle(6, 88, 8, 90); - $draw->rectangle(10, 92, 12, 94); - $draw->rectangle(10, 88, 12, 90); - $draw->rectangle(6, 92, 8, 94); - $draw->rectangle(58, 88, 60, 90); - $draw->rectangle(62, 92, 64, 94); - $draw->rectangle(62, 88, 64, 90); - $draw->rectangle(58, 92, 60, 94); - $draw->rectangle(6, 44, 8, 64); - $draw->rectangle(10, 44, 12, 64); - $draw->rectangle(58, 44, 60, 64); - $draw->rectangle(62, 44, 64, 64); - $draw->rectangle(6, 66, 8, 86); - $draw->rectangle(10, 66, 12, 86); - $draw->rectangle(58, 66, 60, 86); - $draw->rectangle(62, 66, 64, 86); - $draw->rectangle(14, 36, 34, 38); - $draw->rectangle(14, 40, 34, 42); - $draw->rectangle(14, 88, 34, 90); - $draw->rectangle(14, 92, 34, 4); - $draw->rectangle(36, 36, 56, 38); - $draw->rectangle(36, 40, 56, 42); - $draw->rectangle(36, 88, 56, 90); - $draw->rectangle(36, 92, 56, 94); - - $draw->setFillColor($this->color3); - $draw->rectangle(85, 0, 87, 100); - $draw->rectangle(0, 15, 100, 17); - - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - # 3本線 - case 4: - $draw->pushPattern("pattern", 0, 0, 60, 60); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 60, 60); - - $draw->setFillColor($this->color1); - $draw->rectangle(10, 0, 14, 60); - $draw->rectangle(26, 0, 30, 60); - $draw->rectangle(0, 34, 60, 38); - $draw->rectangle(0, 50, 60, 54); - - $draw->setFillColor($this->color2); - $draw->rectangle(18, 0, 22, 60); - $draw->rectangle(0, 42, 60, 46); - - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - # ガンクラブチェック - case 5; - $draw->pushPattern("pattern", 0, 0, 120, 120); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 120, 120); - - $draw->setFillColor($this->color1); - $draw->rectangle(10, 70, 40, 100); - - $draw->setFillColor($this->color2); - $draw->rectangle(70, 10, 100, 40); - - $draw->setFillOpacity(0.5); - - $draw->setFillColor($this->color1); - $draw->rectangle(10, 0, 40, 120); - $draw->rectangle(0, 70, 120, 100); - - $draw->setFillColor($this->color2); - $draw->rectangle(70, 0, 100, 120); - $draw->rectangle(0, 10, 120, 40); - - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - # ギンガムチェック - case 6: - $draw->pushPattern("pattern", 0, 0, 60, 60); - - $draw->setFillColor("white"); - $draw->rectangle(0, 0, 60, 60); - - $draw->setFillColor($this->color1); - $draw->rectangle(10, 10, 40, 40); - - $draw->setFillOpacity(0.5); - $draw->setFillColor($this->color1); - $draw->rectangle(10, 0, 40, 60); - $draw->rectangle(0, 10, 60, 40); - - $draw->popPattern(); - - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - # アーガイルチェック - case 7: - $draw->pushPattern("pattern", 0, 0, 88, 176); - - $draw->setFillColor($this->bg_color); - $draw->rectangle(0, 0, 88, 176); - - $draw->setFillColor($this->color1); - $draw->pathStart(); - $draw->pathMoveToAbsolute(0, 0); - $draw->pathLineToAbsolute(0, 44); - $draw->pathLineToAbsolute(22, 0); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color2); - $draw->pathStart(); - $draw->pathMoveToAbsolute(22, 0); - $draw->pathLineToAbsolute(44, 44); - $draw->pathLineToAbsolute(66, 0); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color1); - $draw->pathStart(); - $draw->pathMoveToAbsolute(66, 0); - $draw->pathLineToAbsolute(88, 44); - $draw->pathLineToAbsolute(88, 0); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color2); - $draw->pathStart(); - $draw->pathMoveToAbsolute(0, 44); - $draw->pathLineToAbsolute(22, 88); - $draw->pathLineToAbsolute(0, 132); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color1); - $draw->pathStart(); - $draw->pathMoveToAbsolute(22, 88); - $draw->pathLineToAbsolute(44, 44); - $draw->pathLineToAbsolute(66, 88); - $draw->pathLineToAbsolute(44, 132); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color2); - $draw->pathStart(); - $draw->pathMoveToAbsolute(88, 44); - $draw->pathLineToAbsolute(66, 88); - $draw->pathLineToAbsolute(88, 132); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color1); - $draw->pathStart(); - $draw->pathMoveToAbsolute(0, 132); - $draw->pathLineToAbsolute(22, 176); - $draw->pathLineToAbsolute(0, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color2); - $draw->pathStart(); - $draw->pathMoveToAbsolute(22, 176); - $draw->pathLineToAbsolute(44, 132); - $draw->pathLineToAbsolute(66, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setFillColor($this->color1); - $draw->pathStart(); - $draw->pathMoveToAbsolute(66, 176); - $draw->pathLineToAbsolute(88, 132); - $draw->pathLineToAbsolute(88, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setStrokeColor($this->color3); - $draw->pathStart(); - $draw->pathMoveToAbsolute(0, 0); - $draw->pathLineToAbsolute(88, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setStrokeColor($this->color3); - $draw->pathStart(); - $draw->pathMoveToAbsolute(88, 0); - $draw->pathLineToAbsolute(0, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setStrokeColor($this->color3); - $draw->pathStart(); - $draw->pathMoveToAbsolute(44, 0); - $draw->pathLineToAbsolute(0, 88); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setStrokeColor($this->color3); - $draw->pathStart(); - $draw->pathMoveToAbsolute(44, 0); - $draw->pathLineToAbsolute(88, 88); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setStrokeColor($this->color3); - $draw->pathStart(); - $draw->pathMoveToAbsolute(0, 88); - $draw->pathLineToAbsolute(44, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->setStrokeColor($this->color3); - $draw->pathStart(); - $draw->pathMoveToAbsolute(88, 88); - $draw->pathLineToAbsolute(44, 176); - $draw->pathClose(); - $draw->pathFinish(); - - $draw->popPattern(); - $draw->setFillPatternURL("#pattern"); - $draw->rectangle(0, 0, 200, 200); - break; - } - $image->newImage(200, 200, new ImagickPixel($this->bg_color)); - $image->drawImage($draw); - $image->setImageFormat("png"); - - return $image; - } + return $image; + } } From 50318380057ca542d8863f90c7c38674429f8fdb Mon Sep 17 00:00:00 2001 From: Fukuda Naoto Date: Sat, 23 Feb 2019 02:50:46 +0900 Subject: [PATCH 2/3] Remove checkmaker.php --- checkmaker.php | 165 ------------------------------------------------- 1 file changed, 165 deletions(-) delete mode 100644 checkmaker.php diff --git a/checkmaker.php b/checkmaker.php deleted file mode 100644 index 716955f..0000000 --- a/checkmaker.php +++ /dev/null @@ -1,165 +0,0 @@ -evaluation = null; - $this->chrom = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - } - - // 染色体をランダムで初期化 - public function init_chrom() - { - $this->chrom[0] = rand(0, 7); - for ($i = 1; $i < 13; $i++) { - $this->chrom[$i] = rand(0, 255); - } - } - - // チェック柄の種類を取得 - public function get_plaid_id() - { - return $this->chrom[0]; - } - - // 色情報を取得 - public function get_color($color_num) - { - return "rgb({$this->chrom[1 * $color_num]},{$this->chrom[2 * $color_num]},{$this->chrom[3 * $color_num]})"; - } -} - -// Checkmakerクラス -class Checkmaker -{ - public $population_num; // 個体数 - public $mutation_probability; // 突然変異確率 - - public $population = []; // 集団 - - public function __construct($population_num, $mutation_probability) - { - $this->population_num = $population_num; - $this->mutation_probability = $mutation_probability; - for ($i = 0; $i < $this->population_num; $i++) { - $this->population[$i] = new Individual(); - } - } - - // 初期集団生成 - public function init_population() - { - for ($i = 0; $i < $this->population_num; $i++) { - $this->population[$i]->init_chrom(); - } - } - - // 評価値設定 - public function set_evaluations($evaluations) - { - for ($i = 0; $i < $this->population_num; $i++) { - $this->population[$i]->evaluation = $evaluations[$i]; - } - } - - // 次世代生成 - public function generate_next() - { - $evaluation_sum = 0; - $next_population = []; - - foreach ($this->population as $individual) { - $evaluation_sum += $individual->evaluation; - } - for ($i = 0; $i < $this->population_num; $i++) { - $parent1 = new Individual(); - $parent2 = new Individual(); - $child = new Individual(); - // ルーレット選択(親1) - $roulette_target_num = rand(0, $evaluation_sum); - $roulette_current_num = 0; - foreach ($this->population as $individual) { - $roulette_current_num += $individual->evaluation; - if ($roulette_current_num >= $roulette_target_num) { - for ($j = 0; $j < 13; $j++) { - $parent1->chrom[$j] = $individual->chrom[$j]; - } - break; - } - } - // ルーレット選択(親2) - $roulette_target_num = rand(0, $evaluation_sum); - $roulette_current_num = 0; - foreach ($this->population as $individual) { - $roulette_current_num += $individual->evaluation; - if ($roulette_current_num >= $roulette_target_num) { - for ($j = 0; $j < 13; $j++) { - $parent2->chrom[$j] = $individual->chrom[$j]; - } - break; - } - } - // 一様交叉 - for ($j = 0; $j < 13; $j++) { - $child->chrom[$j] = rand(0, 1) == 0 ? $parent1->chrom[$j] : $parent2->chrom[$j]; - } - // 突然変異 - for ($j = 0; $j < 13; $j++) { - if (rand(0, 100) < $this->mutation_probability * 100) { - $child->chrom[$j] = $j == 0 ? rand(0, 7) : rand(0, 255); - } - } - array_push($next_population, $child); - } - $this->population = $next_population; - } - - // 最良の個体を取得 - public function get_best_individual() - { - $this->sort_population(); - $max_evaluation = $this->population[0]->evaluation; - $result_population = []; - foreach ($this->population as $individual) { - if ($individual->evaluation == $max_evaluation) { - array_push($result_population, $individual); - } - } - $result_population_num = count($result_population); - if ($result_population_num > 1) { - $result_individual = $result_population[rand(0, $result_population_num - 1)]; - } elseif ($result_population_num > 0) { - $result_individual = $result_population[0]; - } - return $result_individual; - } - - // 集団を評価降順にソート - private function sort_population() - { - for ($i = 0; $i < count($this->population) - 1; $i++) { - for ($j = 0; $j < count($this->population) - 1; $j++) { - if ($this->population[$j]->evaluation < $this->population[$j + 1]->evaluation) { - $tmp = $this->population[$j]; - $this->population[$j] = $this->population[$j + 1]; - $this->population[$j + 1] = $this->population[$j]; - } - } - } - } - - public function print_population() - { - foreach ($this->population as $index => $individual) { - echo $index . ":"; - foreach ($individual->chrom as $gene) { - printf("%3d ", $gene); - } - echo "\n"; - } - } -} From bb76df63ff045686de3ac32483d03d8610f319bc Mon Sep 17 00:00:00 2001 From: Fukuda Naoto Date: Sat, 23 Feb 2019 02:51:11 +0900 Subject: [PATCH 3/3] Add iga class and individual class --- iga.php | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ index.php | 3 +- individual.php | 35 ++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 iga.php create mode 100644 individual.php diff --git a/iga.php b/iga.php new file mode 100644 index 0000000..0c2f9e3 --- /dev/null +++ b/iga.php @@ -0,0 +1,107 @@ + 1.0) { + throw new UnexpectedValueException(); + } + $this->population_num = $population_num; + $this->mutation_probability = $mutation_probability; + } + + public function get_population() + { + return $this->poplation; + } + + public function set_population(array $population) + { + if (count($population) != $this->poplation_num) { + throw new LengthException(); + } + foreach ($population as $individual) { + if ($individual instanceof Individual) { + throw new UnexpectedValueException(); + } + } + $this->population = $population; + } + + # 初期集団生成 + public function init_population(int $chrom_num) + { + $chrom = []; + for ($i = 0; $i < $chrom_num; $i++) { + $chrom[$i] = $i == 0 ? rand(0, 7) : rand(0, 255); + } + for ($i = 0; $i < $this->poplation_num; $i++) { + $this->poplation[$i] = new Individual($chrom, 0); + } + } + + # 評価値設定 + public function set_evaluations(array $evaluations) + { + foreach ($evaluations as $index => $evaluation) { + $this->poplation[$index]->evaluation; + } + } + + # 次世代生成 + public function generate_next_population() + { + $evaluation_sum = 0; + $next_population = []; + + foreach ($this->population as $individual) { + $evaluation_sum += $individual->evaluation; + } + for ($i = 0; $i < $this->population_num; $i++) { + $parent1 = new Individual(); + $parent2 = new Individual(); + $child = new Individual(); + # ルーレット選択(親1) + $roulette_target_num = rand(0, $evaluation_sum); + $roulette_current_num = 0; + foreach ($this->population as $individual) { + $roulette_current_num += $individual->evaluation; + if ($roulette_current_num >= $roulette_target_num) { + for ($j = 0; $j < 13; $j++) { + $parent1->chrom[$j] = $individual->chrom[$j]; + } + break; + } + } + # ルーレット選択(親2) + $roulette_target_num = rand(0, $evaluation_sum); + $roulette_current_num = 0; + foreach ($this->population as $individual) { + $roulette_current_num += $individual->evaluation; + if ($roulette_current_num >= $roulette_target_num) { + for ($j = 0; $j < 13; $j++) { + $parent2->chrom[$j] = $individual->chrom[$j]; + } + break; + } + } + # 一様交叉 + for ($j = 0; $j < 13; $j++) { + $child->chrom[$j] = rand(0, 1) == 0 ? $parent1->chrom[$j] : $parent2->chrom[$j]; + } + # 突然変異 + for ($j = 0; $j < 13; $j++) { + if (rand(0, 100) < $this->mutation_probability * 100) { + $child->chrom[$j] = $j == 0 ? rand(0, 7) : rand(0, 255); + } + } + array_push($next_population, $child); + } + $this->population = $next_population; + } +} diff --git a/index.php b/index.php index abdcec3..22e2c15 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ chrom = $chrom; + $this->chrom_num = count($chrom); + $this->evaluation = $evaluation; + } + + public function get_chrom() + { + return $this->chrom; + } + + public function set_chrom(array $chrom) + { + $this->chrom = $chrom; + } + + public function get_evaluation() + { + return $this->evaluation; + } + + public function set_evaluation(int $evaluation) + { + $this->evaluation = $evaluation; + } +}