Skip to content

Commit 6e1a430

Browse files
committed
crossover part1
1 parent 0146050 commit 6e1a430

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

GeneticAlgorithm.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class GeneticAlgorithm
2020
public $fitness = array();
2121
public $probability = array();
2222
public $cumulative_probability = array();
23+
public $crossover_rate = 0.25;
24+
public $population = 200;
2325

2426
public $total_fitness = 0;
2527

@@ -28,12 +30,9 @@ public function __construct()
2830
for ($i=0;$i<5;$i++)
2931
$this->chromosome[$i] = array(rand(0,10),rand(0,10),rand(0,10));
3032

31-
$this->calcFx();
32-
33-
$this->calcFitness();
34-
$this->calcProbability();
33+
$this->selection();
3534

36-
var_dump($this->cumulative_probability);
35+
var_dump($this->chromosome);
3736
}
3837

3938
public function calcFx(){
@@ -56,4 +55,32 @@ public function calcProbability(){
5655
$this->cumulative_probability[$i] = $sum;
5756
}
5857
}
58+
59+
public function selection(){
60+
$this->calcFx();
61+
62+
$this->calcFitness();
63+
$this->calcProbability();
64+
65+
$new_chromosome = array();
66+
for ($i=0;$i<5;$i++) {
67+
$r[$i] = mt_rand() / mt_getrandmax();
68+
69+
for ($j=0;$j<5;$j++){
70+
if ($j == 0) {
71+
if ($r[$i] < $this->cumulative_probability[0])
72+
$new_chromosome[$i] = $this->chromosome[0];
73+
}else {
74+
if ($r[$i] > $this->cumulative_probability[$j - 1] && $r[$i] < $this->cumulative_probability[$j])
75+
$new_chromosome[$i] = $this->chromosome[$j];
76+
}
77+
}
78+
}
79+
$this->chromosome = $new_chromosome;
80+
}
81+
82+
public function crossOver() {
83+
$iteration = 0;
84+
var_export($this->chromosome(1));
85+
}
5986
}

0 commit comments

Comments
 (0)