Skip to content

Commit 1c38c04

Browse files
author
ali
committed
Merge branch 'master' into ali
2 parents 50412ee + 98f09a6 commit 1c38c04

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

GeneticAlgorithm.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +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();
31+
$this->selection();
3532

36-
var_dump($this->cumulative_probability);
33+
var_dump($this->chromosome);
3734
}
3835

3936
public function calcFx(){
@@ -56,4 +53,27 @@ public function calcProbability(){
5653
$this->cumulative_probability[$i] = $sum;
5754
}
5855
}
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+
}
5979
}

0 commit comments

Comments
 (0)