@@ -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,31 +95,40 @@ public function selection(){
95
95
}
96
96
97
97
public function crossOver () {
98
- $ count = 0 ;
99
- for ($ m =0 ; $ m <5 ; $ m ++) {
100
- $ random [$ m ]=mt_rand () / mt_getrandmax ();
101
- if ($ random [$ m ] < $ this ->crossover_rate ) {
102
- $ this ->parents [$ count ] = $ m ;
103
- $ count ++;
104
- }
98
+ $ this ->parents = array ();
99
+
100
+ for ($ m =0 ; $ m <5 ; $ m ++) {
101
+ $ random = mt_rand () / mt_getrandmax ();
102
+ if ($ random < $ this ->crossover_rate ) {
103
+ $ this ->parents [] = $ m ;
105
104
}
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 ];
105
+ }
106
+
107
+ for ($ n = 0 ; $ n <count ($ this ->parents ) ; $ n ++) {
108
+ $ cut_position = rand (1 ,2 );
109
+
110
+ $ dady = array_slice ($ this ->chromosome [$ this ->parents [$ n ]], 0 , $ cut_position );
111
+ $ momy = array_slice ($ this ->chromosome [$ this ->parents [$ n ]], 3 -$ cut_position );
112
+
113
+ $ newChromosome = array_merge ($ dady , $ momy );
114
+
115
+ $ this ->chromosome [$ this ->parents [$ n ]] = $ newChromosome ;
116
+
117
+ // echo $this->chromosome[$this->parents[$n]];
118
+ /*for($p = 0; $p < $cut; $p++) {
119
+ $newChromosome[$p] = $this->chromosome[$this->parents[$n]][$p];
120
+ }
121
+ for($t = $cut ;$t < count($this->chromosome[$this->parents[$n]]); $t++) {
122
+ $u = $t + 1;
123
+ if($u > $count) {
124
+ $newChromosome[$t] = $this->chromosome[0][$t];
119
125
}
120
- var_export ( $ newChromosome );
121
- // $ this->chromosome[$this->parents[$n]] = ??
126
+ else
127
+ $newChromosome[$t] = $ this->chromosome[$this->parents[$u]][$t];
122
128
}
129
+ var_export($newChromosome);*/
130
+ // $this->chromosome[$this->parents[$n]] = ??
131
+ }
123
132
}
124
133
125
134
public function mutation (){
0 commit comments