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