Skip to content

Commit 50412ee

Browse files
author
ali
committed
dont know what did I do :(((
1 parent 38d7bb8 commit 50412ee

File tree

1 file changed

+5
-46
lines changed

1 file changed

+5
-46
lines changed

GeneticAlgorithm.php

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

2724
public $total_fitness = 0;
2825

@@ -31,9 +28,12 @@ public function __construct()
3128
for ($i=0;$i<5;$i++)
3229
$this->chromosome[$i] = array(rand(0,10),rand(0,10),rand(0,10));
3330

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

36-
var_dump($this->chromosome);
36+
var_dump($this->cumulative_probability);
3737
}
3838

3939
public function calcFx(){
@@ -56,45 +56,4 @@ public function calcProbability(){
5656
$this->cumulative_probability[$i] = $sum;
5757
}
5858
}
59-
60-
public function selection(){
61-
$this->calcFx();
62-
63-
$this->calcFitness();
64-
$this->calcProbability();
65-
66-
$new_chromosome = array();
67-
for ($i=0;$i<5;$i++) {
68-
$r[$i] = mt_rand() / mt_getrandmax();
69-
70-
for ($j=0;$j<5;$j++){
71-
if ($j == 0) {
72-
if ($r[$i] < $this->cumulative_probability[0])
73-
$new_chromosome[$i] = $this->chromosome[0];
74-
}else {
75-
if ($r[$i] > $this->cumulative_probability[$j - 1] && $r[$i] < $this->cumulative_probability[$j])
76-
$new_chromosome[$i] = $this->chromosome[$j];
77-
}
78-
}
79-
}
80-
$this->chromosome = $new_chromosome;
81-
}
82-
83-
public function crossOver() {
84-
$iteration = 0;
85-
var_export($this->chromosome(1));
86-
}
87-
88-
public function mutation(){
89-
90-
$mutation_count = round((3 * 5) * $this->mutation_rate, 0, PHP_ROUND_HALF_DOWN);
91-
92-
for ($i=0;$i<$mutation_count;$i++){
93-
$position = rand(1,(3 * 5));
94-
$chosen_chromosome = round($position / 3 , 0 , PHP_ROUND_HALF_UP) - 1;
95-
$chosen_gen = (3 * 5) - ($chosen_chromosome * 3);
96-
97-
$this->chromosome[$chosen_chromosome][$chosen_gen] = rand(0,10);
98-
}
99-
}
10059
}

0 commit comments

Comments
 (0)