Skip to content

Commit 89e2d32

Browse files
committed
updated population
1 parent fb91fbe commit 89e2d32

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

game/players/ai_player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace Players{
7676
int16_t fx = food.x;
7777
int16_t fy = food.y;
7878

79-
this->input_data->update_value(0, 0, (double)(px-fx)/w);
79+
this->input_data->update_value(0, 0, (double)(px-fx)/w);
8080
this->input_data->update_value(0, 1, (double)(py-fy)/h);
8181
// this->input_data->update_value(0, 2, get_angle(px, py, fx, fy) / (2*PI));
8282
}

genetic/population.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cstdint>
2+
#include <iostream>
23
#include <stdexcept>
34
#include <unistd.h>
45
#include "population.h"
@@ -7,6 +8,8 @@
78
#include "chromosome.h"
89
#include "gene.h"
910

11+
using std::cout;
12+
using std::endl;
1013
using std::invalid_argument;
1114
using Game::Board;
1215
using Chromosomes::Chromosome;
@@ -25,7 +28,7 @@ namespace Genetic{
2528
this->board_h = board_h;
2629
this->board_w = board_w;
2730

28-
this->generate_food_positions(total_food, board_w, board_h);
31+
this->generate_food_positions();
2932
vec2 first_food_pos = this->food_positions.at(0);
3033

3134
for(size_t i = 0; i < this->total_ind; i++){
@@ -44,9 +47,9 @@ namespace Genetic{
4447
}
4548
}
4649

47-
void Population::generate_food_positions(uint8_t total, uint8_t w, uint8_t h){
48-
for(size_t _ = 0; _ < total; _++)
49-
this->food_positions.push_back(vec2{(int16_t)random_int(0, h-1), (int16_t)random_int(0, w-1)});
50+
void Population::generate_food_positions(){
51+
for(size_t _ = 0; _ < this->total_food; _++)
52+
this->food_positions.push_back(vec2{(int16_t)random_int(0, this->board_h-1), (int16_t)random_int(0, this->board_w-1)});
5053
// TODO: refactor this random position to a especialist class or something like this
5154
}
5255

@@ -156,9 +159,13 @@ namespace Genetic{
156159
}
157160

158161
void Population::next_gen(){
162+
cout << "gen: " << this->gen << endl;
163+
cout << "best fit: " << this->best_fitness << endl;
164+
cout << "best score: " << this->best_score << endl << endl;
165+
159166
this->gen++;
160167
this->best_score = 0;
161-
std::cout << "next gen: " << this->gen << "\n";
168+
162169
Individual** parents = this->select_parents();
163170
Chromosome* offspring = this->generate_offspring(parents[0]->player->get_chromossome(), parents[1]->player->get_chromossome());
164171
delete parents;
@@ -167,10 +174,9 @@ namespace Genetic{
167174
uint64_t offspring_ch_size = offspring->get_size();
168175

169176
this->clear();
170-
std::cout << "no segmentaion fault\n";
171177
this->individuals.clear();
172178
this->food_positions.clear();
173-
this->generate_food_positions(total_food, board_w, board_h);
179+
this->generate_food_positions();
174180
vec2 first_food_pos = this->food_positions.at(0);
175181

176182
this->individuals.clear();

genetic/population.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace Genetic {
6363
vector<Individual*> individuals;
6464
vector<vec2> food_positions;
6565

66-
void generate_food_positions(uint8_t total, uint8_t w, uint8_t h);
66+
void generate_food_positions();
6767
void update_individual_food_position(Individual *ind);
6868
void clear();
6969
};

0 commit comments

Comments
 (0)