@@ -125,28 +125,50 @@ namespace Genetic{
125125 // fitness for catching the food
126126 if (board->get_caught_the_food ())
127127 ind->fitness += 5000 ;
128-
129- if (player->get_score () >= this ->total_food ){
128+
129+ uint16_t score = player->get_score ();
130+ if (score > this ->best_score )
131+ this ->best_score = score;
132+
133+ if (score >= this ->total_food ){
130134 player->set_died ();
131135 this ->total_win ++;
132136 this ->total_alive --;
133137 ind->fitness += 20000 ;
138+ this ->update_best_fitness (ind);
139+ continue ;
140+ }
141+
142+ if (player->is_dead ()){
143+ ind->fitness += -1000 ;
144+ this ->update_best_fitness (ind);
134145 continue ;
135146 }
136147
148+ this ->update_best_fitness (ind);
137149 this ->update_individual_food_position (ind);
138150 }
139151 }
140152
153+ void Population::update_best_fitness (Individual* ind){
154+ if (ind->fitness > this ->best_fitness )
155+ this ->best_fitness = ind->fitness ;
156+ }
157+
141158 void Population::next_gen (){
142159 this ->gen ++;
160+ this ->best_score = 0 ;
161+ std::cout << " next gen: " << this ->gen << " \n " ;
143162 Individual** parents = this ->select_parents ();
144163 Chromosome* offspring = this ->generate_offspring (parents[0 ]->player ->get_chromossome (), parents[1 ]->player ->get_chromossome ());
164+ delete parents;
145165 Gene* offspring_genes = offspring->get_genes ();
146166
147167 uint64_t offspring_ch_size = offspring->get_size ();
148168
149169 this ->clear ();
170+ std::cout << " no segmentaion fault\n " ;
171+ this ->individuals .clear ();
150172 this ->food_positions .clear ();
151173 this ->generate_food_positions (total_food, board_w, board_h);
152174 vec2 first_food_pos = this ->food_positions .at (0 );
@@ -161,7 +183,7 @@ namespace Genetic{
161183 else {
162184 Chromosome* player_chromosome = new Chromosome (offspring_ch_size);
163185 player_chromosome->copy_genes (offspring_genes);
164- player_chromosome->mutate (0.02 );
186+ player_chromosome->mutate (0.3 );
165187
166188 ind->player = new AIPlayer (board_w, board_h, player_chromosome);
167189 }
@@ -175,7 +197,6 @@ namespace Genetic{
175197
176198 this ->individuals .push_back (ind);
177199 }
178- delete parents;
179200 }
180201
181202 Individual** Population::select_parents (){
@@ -238,13 +259,7 @@ namespace Genetic{
238259 }
239260
240261 uint16_t Population::get_best_score (){
241- uint16_t best_score = this ->individuals .at (0 )->player ->get_score ();
242- for (Individual* ind: this ->individuals ){
243- uint16_t ind_score = ind->player ->get_score ();
244- if (ind_score > best_score)
245- best_score = ind_score;
246- }
247- return best_score;
262+ return this ->best_score ;
248263 }
249264
250265 uint16_t Population::get_total_alive (){
@@ -256,7 +271,7 @@ namespace Genetic{
256271 }
257272
258273 int64_t Population::get_best_fitness (){
259- return this ->get_best_individual ()-> fitness ;
274+ return this ->best_fitness ;
260275 }
261276
262277 void Population::clear (){
0 commit comments