Skip to content

Commit 67a60e1

Browse files
author
ali
committed
max_fitness
population loop
1 parent fff2f1d commit 67a60e1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

GeneticAlgorithm.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class GeneticAlgorithm
2121
public $probability = array();
2222
public $cumulative_probability = array();
2323
public $parents = array();
24+
public $max_fitness = array();
2425

2526
public $crossover_rate = 0.25;
2627
public $mutation_rate = 0.1;
@@ -33,7 +34,17 @@ public function __construct()
3334
for ($i=0;$i<5;$i++)
3435
$this->chromosome[$i] = array(rand(0,10),rand(0,10),rand(0,10));
3536

36-
$this->selection();
37+
for ($i=0;$i<$this->population;$i++) {
38+
$this->selection();
39+
$this->crossOver();
40+
$this->mutation();
41+
42+
for ($i=0;$i<5;$i++)
43+
if ($this->chromosome[$i][0] + 2 * $this->chromosome[$i][1] + 3 * $this->chromosome[$i][2] == 10)
44+
break;
45+
46+
$this->iteration++;
47+
}
3748

3849
var_dump($this->chromosome);
3950
}
@@ -46,6 +57,8 @@ public function calcFx(){
4657
public function calcFitness(){
4758
for ($i=0;$i<5;$i++) {
4859
$this->fitness[$i] = 1 / (1 + $this->fx[$i]);
60+
if ( !isset($this->max_fitness[$this->iteration]) || ($this->fitness[$i] > $this->max_fitness[$this->iteration]))
61+
$this->max_fitness[$this->iteration] = $this->fitness[$i];
4962
$this->total_fitness += $this->fitness[$i];
5063
}
5164
}

0 commit comments

Comments
 (0)