Skip to content

Commit

Permalink
updated population
Browse files Browse the repository at this point in the history
  • Loading branch information
Dpbm committed May 2, 2024
1 parent fb91fbe commit 89e2d32
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion game/players/ai_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace Players{
int16_t fx = food.x;
int16_t fy = food.y;

this->input_data->update_value(0, 0, (double)(px-fx)/w);
this->input_data->update_value(0, 0, (double)(px-fx)/w);
this->input_data->update_value(0, 1, (double)(py-fy)/h);
// this->input_data->update_value(0, 2, get_angle(px, py, fx, fy) / (2*PI));
}
Expand Down
20 changes: 13 additions & 7 deletions genetic/population.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
#include "population.h"
Expand All @@ -7,6 +8,8 @@
#include "chromosome.h"
#include "gene.h"

using std::cout;
using std::endl;
using std::invalid_argument;
using Game::Board;
using Chromosomes::Chromosome;
Expand All @@ -25,7 +28,7 @@ namespace Genetic{
this->board_h = board_h;
this->board_w = board_w;

this->generate_food_positions(total_food, board_w, board_h);
this->generate_food_positions();
vec2 first_food_pos = this->food_positions.at(0);

for(size_t i = 0; i < this->total_ind; i++){
Expand All @@ -44,9 +47,9 @@ namespace Genetic{
}
}

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

Expand Down Expand Up @@ -156,9 +159,13 @@ namespace Genetic{
}

void Population::next_gen(){
cout << "gen: " << this->gen << endl;
cout << "best fit: " << this->best_fitness << endl;
cout << "best score: " << this->best_score << endl << endl;

this->gen++;
this->best_score = 0;
std::cout << "next gen: " << this->gen << "\n";

Individual** parents = this->select_parents();
Chromosome* offspring = this->generate_offspring(parents[0]->player->get_chromossome(), parents[1]->player->get_chromossome());
delete parents;
Expand All @@ -167,10 +174,9 @@ namespace Genetic{
uint64_t offspring_ch_size = offspring->get_size();

this->clear();
std::cout << "no segmentaion fault\n";
this->individuals.clear();
this->food_positions.clear();
this->generate_food_positions(total_food, board_w, board_h);
this->generate_food_positions();
vec2 first_food_pos = this->food_positions.at(0);

this->individuals.clear();
Expand Down
2 changes: 1 addition & 1 deletion genetic/population.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace Genetic {
vector<Individual*> individuals;
vector<vec2> food_positions;

void generate_food_positions(uint8_t total, uint8_t w, uint8_t h);
void generate_food_positions();
void update_individual_food_position(Individual *ind);
void clear();
};
Expand Down

0 comments on commit 89e2d32

Please sign in to comment.