@@ -20,7 +20,12 @@ class GeneticAlgorithm
20
20
public $ fitness = array ();
21
21
public $ probability = array ();
22
22
public $ cumulative_probability = array ();
23
+ public $ parents = array ();
23
24
25
+ public $ crossover_rate = 0.25 ;
26
+ public $ mutation_rate = 0.1 ;
27
+ public $ population = 200 ;
28
+ public $ iteration = 0 ;
24
29
public $ total_fitness = 0 ;
25
30
26
31
public function __construct ()
@@ -76,4 +81,32 @@ public function selection(){
76
81
}
77
82
$ this ->chromosome = $ new_chromosome ;
78
83
}
84
+
85
+ public function crossOver () {
86
+ while ($ this ->iteration < $ this ->population ) {
87
+ for ($ m =0 ; $ m <4 ; $ m ++) {
88
+ $ random [$ m ]=mt_rand () / mt_getrandmax ();
89
+ if ($ random [$ m ] < $ this ->crossover_rate ) {
90
+ // $this->parents[$m] = $this->chromosome[$m];
91
+ $ ids = $ m ;
92
+ }
93
+ }
94
+ $ cuts =
95
+ $ this ->iteration += 1 ;
96
+ }
97
+ }
98
+
99
+ public function mutation (){
100
+
101
+ $ mutation_count = round ((3 * 5 ) * $ this ->mutation_rate , 0 , PHP_ROUND_HALF_DOWN );
102
+
103
+ for ($ i =0 ;$ i <$ mutation_count ;$ i ++){
104
+ $ position = rand (1 ,(3 * 5 ));
105
+ $ chosen_chromosome = round ($ position / 3 , 0 , PHP_ROUND_HALF_UP ) - 1 ;
106
+ $ chosen_gen = (3 * 5 ) - ($ chosen_chromosome * 3 );
107
+
108
+ $ this ->chromosome [$ chosen_chromosome ][$ chosen_gen ] = rand (0 ,10 );
109
+ }
110
+ }
111
+
79
112
}
0 commit comments