Skip to content

Commit 53a9ca8

Browse files
author
ali
committed
Merge branches 'master' and 'rziw' of https://gitlab.com/AliJafari13/ga
2 parents 2682d68 + fd98d4a commit 53a9ca8

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

GeneticAlgorithm.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GeneticAlgorithm
2222
public $cumulative_probability = array();
2323
public $parents = array();
2424

25-
public $crossover_rate = 0.25;
25+
public $crossover_rate = 0.35;
2626
public $mutation_rate = 0.1;
2727
public $population = 200;
2828
public $iteration = 0;
@@ -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,17 +95,31 @@ public function selection(){
9595
}
9696

9797
public function crossOver() {
98-
while($this->iteration < $this->population) {
99-
for($m=0; $m<4 ; $m++) {
98+
$count = 0;
99+
for($m=0; $m<5 ; $m++) {
100100
$random[$m]=mt_rand() / mt_getrandmax();
101101
if($random[$m] < $this->crossover_rate) {
102-
// $this->parents[$m] = $this->chromosome[$m];
103-
$ids = $m;
102+
$this->parents[$count] = $m;
103+
$count++;
104104
}
105105
}
106-
$cuts =
107-
$this->iteration += 1;
108-
}
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];
119+
}
120+
var_export($newChromosome);
121+
// $this->chromosome[$this->parents[$n]] = ??
122+
}
109123
}
110124

111125
public function mutation(){

index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
require_once "GeneticAlgorithm.php";
1313

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

0 commit comments

Comments
 (0)