@@ -22,7 +22,7 @@ class GeneticAlgorithm
22
22
public $ cumulative_probability = array ();
23
23
public $ parents = array ();
24
24
25
- public $ crossover_rate = 0.25 ;
25
+ public $ crossover_rate = 0.35 ;
26
26
public $ mutation_rate = 0.1 ;
27
27
public $ population = 200 ;
28
28
public $ iteration = 0 ;
@@ -33,19 +33,19 @@ public function __construct()
33
33
for ($ i =0 ;$ i <5 ;$ i ++)
34
34
$ this ->chromosome [$ i ] = array (rand (0 ,10 ),rand (0 ,10 ),rand (0 ,10 ));
35
35
36
- for ($ i =0 ;$ i <$ this ->population ;$ i ++) {
37
- $ this ->selection ();
38
- $ this ->crossOver ();
39
- $ this ->mutation ();
36
+ // for ($i=0;$i<$this->population;$i++) {
37
+ // $this->selection();
38
+ // $this->crossOver();
39
+ // $this->mutation();
40
40
41
- for ($ i =0 ;$ i <5 ;$ i ++)
42
- if ($ this ->chromosome [$ i ][0 ] + 2 * $ this ->chromosome [$ i ][1 ] + 3 * $ this ->chromosome [$ i ][2 ] == 10 )
43
- break ;
41
+ // for ($i=0;$i<5;$i++)
42
+ // if ($this->chromosome[$i][0] + 2 * $this->chromosome[$i][1] + 3 * $this->chromosome[$i][2] == 10)
43
+ // break;
44
44
45
- $ this ->iteration ++;
46
- }
45
+ // $this->iteration++;
46
+ // }
47
47
48
- var_dump ($ this ->chromosome );
48
+ // var_dump($this->chromosome);
49
49
}
50
50
51
51
public function calcFx (){
@@ -95,17 +95,31 @@ public function selection(){
95
95
}
96
96
97
97
public function crossOver () {
98
- while ( $ this -> iteration < $ this -> population ) {
99
- for ($ m =0 ; $ m <4 ; $ m ++) {
98
+ $ count = 0 ;
99
+ for ($ m =0 ; $ m <5 ; $ m ++) {
100
100
$ random [$ m ]=mt_rand () / mt_getrandmax ();
101
101
if ($ random [$ m ] < $ this ->crossover_rate ) {
102
- // $this->parents[$m ] = $this->chromosome[$m] ;
103
- $ ids = $ m ;
102
+ $ this ->parents [$ count ] = $ m ;
103
+ $ count ++ ;
104
104
}
105
105
}
106
- $ cuts =
107
- $ this ->iteration += 1 ;
108
- }
106
+ for ($ n = 0 ; $ n <$ count ; $ n ++) {
107
+ $ cut = rand (1 ,2 );
108
+ // echo $this->chromosome[$this->parents[$n]];
109
+ for ($ p = 0 ; $ p < $ cut ; $ p ++) {
110
+ $ newChromosome [$ p ] = $ this ->chromosome [$ this ->parents [$ n ]][$ p ];
111
+ }
112
+ for ($ t = $ cut ;$ t < count ($ this ->chromosome [$ this ->parents [$ n ]]); $ t ++) {
113
+ $ u = $ t + 1 ;
114
+ if ($ u > $ count ) {
115
+ $ newChromosome [$ t ] = $ this ->chromosome [0 ][$ t ];
116
+ }
117
+ else
118
+ $ newChromosome [$ t ] = $ this ->chromosome [$ this ->parents [$ u ]][$ t ];
119
+ }
120
+ var_export ($ newChromosome );
121
+ // $this->chromosome[$this->parents[$n]] = ??
122
+ }
109
123
}
110
124
111
125
public function mutation (){
0 commit comments