@@ -20,6 +20,8 @@ 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 $ population = 200 ;
23
25
24
26
public $ total_fitness = 0 ;
25
27
@@ -28,12 +30,9 @@ public function __construct()
28
30
for ($ i =0 ;$ i <5 ;$ i ++)
29
31
$ this ->chromosome [$ i ] = array (rand (0 ,10 ),rand (0 ,10 ),rand (0 ,10 ));
30
32
31
- $ this ->calcFx ();
32
-
33
- $ this ->calcFitness ();
34
- $ this ->calcProbability ();
33
+ $ this ->selection ();
35
34
36
- var_dump ($ this ->cumulative_probability );
35
+ var_dump ($ this ->chromosome );
37
36
}
38
37
39
38
public function calcFx (){
@@ -56,4 +55,32 @@ public function calcProbability(){
56
55
$ this ->cumulative_probability [$ i ] = $ sum ;
57
56
}
58
57
}
58
+
59
+ public function selection (){
60
+ $ this ->calcFx ();
61
+
62
+ $ this ->calcFitness ();
63
+ $ this ->calcProbability ();
64
+
65
+ $ new_chromosome = array ();
66
+ for ($ i =0 ;$ i <5 ;$ i ++) {
67
+ $ r [$ i ] = mt_rand () / mt_getrandmax ();
68
+
69
+ for ($ j =0 ;$ j <5 ;$ j ++){
70
+ if ($ j == 0 ) {
71
+ if ($ r [$ i ] < $ this ->cumulative_probability [0 ])
72
+ $ new_chromosome [$ i ] = $ this ->chromosome [0 ];
73
+ }else {
74
+ if ($ r [$ i ] > $ this ->cumulative_probability [$ j - 1 ] && $ r [$ i ] < $ this ->cumulative_probability [$ j ])
75
+ $ new_chromosome [$ i ] = $ this ->chromosome [$ j ];
76
+ }
77
+ }
78
+ }
79
+ $ this ->chromosome = $ new_chromosome ;
80
+ }
81
+
82
+ public function crossOver () {
83
+ $ iteration = 0 ;
84
+ var_export ($ this ->chromosome (1 ));
85
+ }
59
86
}
0 commit comments