Skip to content

Commit 0de68d4

Browse files
author
ali
committed
crossover finished
1 parent 53a9ca8 commit 0de68d4

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

GeneticAlgorithm.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ public function __construct()
3333
for ($i=0;$i<5;$i++)
3434
$this->chromosome[$i] = array(rand(0,10),rand(0,10),rand(0,10));
3535

36-
// for ($i=0;$i<$this->population;$i++) {
37-
// $this->selection();
38-
// $this->crossOver();
39-
// $this->mutation();
36+
for ($i=0;$i<$this->population;$i++) {
37+
$this->selection();
38+
$this->crossOver();
39+
$this->mutation();
4040

41-
// for ($i=0;$i<5;$i++)
42-
// if ($this->chromosome[$i][0] + 2 * $this->chromosome[$i][1] + 3 * $this->chromosome[$i][2] == 10)
43-
// break;
41+
for ($i=0;$i<5;$i++)
42+
if ($this->chromosome[$i][0] + 2 * $this->chromosome[$i][1] + 3 * $this->chromosome[$i][2] == 10)
43+
break;
4444

45-
// $this->iteration++;
46-
// }
45+
$this->iteration++;
46+
}
4747

48-
// var_dump($this->chromosome);
48+
var_dump($this->chromosome);
4949
}
5050

5151
public function calcFx(){
@@ -95,31 +95,40 @@ public function selection(){
9595
}
9696

9797
public function crossOver() {
98-
$count = 0;
99-
for($m=0; $m<5 ; $m++) {
100-
$random[$m]=mt_rand() / mt_getrandmax();
101-
if($random[$m] < $this->crossover_rate) {
102-
$this->parents[$count] = $m;
103-
$count++;
104-
}
98+
$this->parents = array();
99+
100+
for($m=0; $m<5 ; $m++) {
101+
$random = mt_rand() / mt_getrandmax();
102+
if($random < $this->crossover_rate) {
103+
$this->parents[] = $m;
105104
}
106-
for($n = 0 ; $n<$count ; $n++) {
107-
$cut = rand(1,2);
108-
// echo $this->chromosome[$this->parents[$n]];
109-
for($p = 0; $p < $cut; $p++) {
110-
$newChromosome[$p] = $this->chromosome[$this->parents[$n]][$p];
111-
}
112-
for($t = $cut ;$t < count($this->chromosome[$this->parents[$n]]); $t++) {
113-
$u = $t + 1;
114-
if($u > $count) {
115-
$newChromosome[$t] = $this->chromosome[0][$t];
116-
}
117-
else
118-
$newChromosome[$t] = $this->chromosome[$this->parents[$u]][$t];
105+
}
106+
107+
for($n = 0 ; $n<count($this->parents) ; $n++) {
108+
$cut_position = rand(1,2);
109+
110+
$dady = array_slice($this->chromosome[$this->parents[$n]], 0, $cut_position);
111+
$momy = array_slice($this->chromosome[$this->parents[$n]], 3-$cut_position);
112+
113+
$newChromosome = array_merge($dady , $momy);
114+
115+
$this->chromosome[$this->parents[$n]] = $newChromosome;
116+
117+
// echo $this->chromosome[$this->parents[$n]];
118+
/*for($p = 0; $p < $cut; $p++) {
119+
$newChromosome[$p] = $this->chromosome[$this->parents[$n]][$p];
120+
}
121+
for($t = $cut ;$t < count($this->chromosome[$this->parents[$n]]); $t++) {
122+
$u = $t + 1;
123+
if($u > $count) {
124+
$newChromosome[$t] = $this->chromosome[0][$t];
119125
}
120-
var_export($newChromosome);
121-
// $this->chromosome[$this->parents[$n]] = ??
126+
else
127+
$newChromosome[$t] = $this->chromosome[$this->parents[$u]][$t];
122128
}
129+
var_export($newChromosome);*/
130+
// $this->chromosome[$this->parents[$n]] = ??
131+
}
123132
}
124133

125134
public function mutation(){

index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111

1212
require_once "GeneticAlgorithm.php";
1313

14-
$ga = new GeneticAlgorithm();
15-
echo $ga->crossOver();
14+
$ga = new GeneticAlgorithm();

0 commit comments

Comments
 (0)