@@ -20,9 +20,6 @@ class GeneticAlgorithm
20
20
public $ fitness = array ();
21
21
public $ probability = array ();
22
22
public $ cumulative_probability = array ();
23
- public $ crossover_rate = 0.25 ;
24
- public $ mutation_rate = 0.1 ;
25
- public $ population = 200 ;
26
23
27
24
public $ total_fitness = 0 ;
28
25
@@ -31,9 +28,12 @@ public function __construct()
31
28
for ($ i =0 ;$ i <5 ;$ i ++)
32
29
$ this ->chromosome [$ i ] = array (rand (0 ,10 ),rand (0 ,10 ),rand (0 ,10 ));
33
30
34
- $ this ->selection ();
31
+ $ this ->calcFx ();
32
+
33
+ $ this ->calcFitness ();
34
+ $ this ->calcProbability ();
35
35
36
- var_dump ($ this ->chromosome );
36
+ var_dump ($ this ->cumulative_probability );
37
37
}
38
38
39
39
public function calcFx (){
@@ -56,45 +56,4 @@ public function calcProbability(){
56
56
$ this ->cumulative_probability [$ i ] = $ sum ;
57
57
}
58
58
}
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
- }
100
59
}
0 commit comments