Skip to content

Commit 98f09a6

Browse files
author
ali
committed
selection DONE!
1 parent 892fe1a commit 98f09a6

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

GeneticAlgorithm.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,9 @@ public function __construct()
2828
for ($i=0;$i<5;$i++)
2929
$this->chromosome[$i] = array(rand(0,10),rand(0,10),rand(0,10));
3030

31-
$this->calcFx();
32-
33-
$this->calcFitness();
34-
$this->calcProbability();
35-
36-
$new_chromosome = array();
37-
38-
for ($i=0;$i<5;$i++) {
39-
$r[$i] = mt_rand() / mt_getrandmax();
40-
41-
for ($j=0;$j<5;$j++){
42-
if ($j == 0) {
43-
if ($r[$i] < $this->cumulative_probability[0])
44-
$new_chromosome[$i] = $this->chromosome[0];
45-
}else {
46-
if ($r[$i] > $this->cumulative_probability[$j - 1] && $r[$i] < $this->cumulative_probability[$j])
47-
$new_chromosome[$i] = $this->chromosome[$j];
48-
}
49-
}
50-
}
51-
31+
$this->selection();
5232

53-
var_dump($new_chromosome);
33+
var_dump($this->chromosome);
5434
}
5535

5636
public function calcFx(){
@@ -73,4 +53,27 @@ public function calcProbability(){
7353
$this->cumulative_probability[$i] = $sum;
7454
}
7555
}
56+
57+
public function selection(){
58+
$this->calcFx();
59+
60+
$this->calcFitness();
61+
$this->calcProbability();
62+
63+
$new_chromosome = array();
64+
for ($i=0;$i<5;$i++) {
65+
$r[$i] = mt_rand() / mt_getrandmax();
66+
67+
for ($j=0;$j<5;$j++){
68+
if ($j == 0) {
69+
if ($r[$i] < $this->cumulative_probability[0])
70+
$new_chromosome[$i] = $this->chromosome[0];
71+
}else {
72+
if ($r[$i] > $this->cumulative_probability[$j - 1] && $r[$i] < $this->cumulative_probability[$j])
73+
$new_chromosome[$i] = $this->chromosome[$j];
74+
}
75+
}
76+
}
77+
$this->chromosome = $new_chromosome;
78+
}
7679
}

0 commit comments

Comments
 (0)