Skip to content

Commit

Permalink
ai v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dpbm committed May 2, 2024
1 parent c433736 commit fb91fbe
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
14 changes: 14 additions & 0 deletions game/players/ai_player.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <ctime>
#include <cstdint>
#include <sstream>
#include "ai_player.h"
#include "../../machine/weights.h"
#include "../../genetic/chromosome.h"
#include "../../helpers/utils.h"
#include "../../helpers/constants.h"
#include "player.h"

using std::stringstream;
using Machine::Weights;
using Chromosomes::Chromosome;
using Utils::vec2;
Expand Down Expand Up @@ -100,6 +103,17 @@ namespace Players{
this->set_dir((Directions)new_dir);
}

void AIPlayer::save_weights(){
time_t now = time(0);
string time = asctime(localtime(&now));
//remove the $\n at the end of the string
time.pop_back();

stringstream filename;
filename << time;
filename << ".wg";
this->nn->save_weights(filename.str());
}

AIPlayer::~AIPlayer(){
delete this->chromosome;
Expand Down
1 change: 1 addition & 0 deletions game/players/ai_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Players{
AIPlayer(uint8_t board_w, uint8_t board_h, Chromosome* chromosome);
~AIPlayer();

void save_weights();
void load_genes_into_weights();
void update_input_data(const vec2& food, uint16_t w, uint16_t h);
void compute_next_dir();
Expand Down
22 changes: 20 additions & 2 deletions game/screens/ai_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using Genetic::Individual;
namespace Screens {

AIScreen::AIScreen(SDL_Renderer* render){
if(!this->font){
if(!this->font || !this->tiny_font){
cout << "Failed on getting font!" << TTF_GetError() << endl;
exit(1);
}
Expand Down Expand Up @@ -58,13 +58,20 @@ namespace Screens {
this->timer_text_texture = SDL_CreateTextureFromSurface(render, timer_text_surface);
this->timer_text_shape = SDL_Rect{20, 370, timer_text_surface->w, timer_text_surface->h};
SDL_FreeSurface(timer_text_surface);


SDL_Surface* weights_text_surface = TTF_RenderText_Solid(this->tiny_font, "'S' save weights", this->text_color);
this->weights_text_texture = SDL_CreateTextureFromSurface(render, weights_text_surface);
this->weights_text_shape = SDL_Rect{20, 530, weights_text_surface->w, weights_text_surface->h};
SDL_FreeSurface(weights_text_surface);

if(this->score_text_texture == nullptr ||
this->gen_text_texture == nullptr ||
this->alive_text_texture == nullptr ||
this->win_text_texture == nullptr ||
this->fitness_text_texture == nullptr ||
this->timer_text_texture == nullptr){
this->timer_text_texture == nullptr ||
this->weights_text_texture == nullptr){
cout << "Failed on creating text textures!" << SDL_GetError() << endl;
exit(1);
}
Expand Down Expand Up @@ -115,6 +122,7 @@ namespace Screens {
SDL_RenderCopy(render, this->win_text_texture, NULL, &this->win_text_shape);
SDL_RenderCopy(render, this->fitness_text_texture, NULL, &this->fitness_text_shape);
SDL_RenderCopy(render, this->timer_text_texture, NULL, &this->timer_text_shape);
SDL_RenderCopy(render, this->weights_text_texture, NULL, &this->weights_text_shape);

if(this->score_texture != nullptr)
SDL_DestroyTexture(this->score_texture);
Expand Down Expand Up @@ -178,10 +186,19 @@ namespace Screens {
}

Screen* AIScreen::key_event(const SDL_Keycode& key){
if(key == SDLK_s)
this->save_weights();

return nullptr;
}

void AIScreen::close_event(){
this->save_weights();
}

void AIScreen::save_weights(){
cout << "Saving weights!..." << endl;
this->population.get_best_individual()->player->save_weights();
}

AIScreen::~AIScreen(){
Expand All @@ -197,5 +214,6 @@ namespace Screens {
SDL_DestroyTexture(this->fitness_text_texture);
SDL_DestroyTexture(this->timer_texture);
SDL_DestroyTexture(this->timer_text_texture);
SDL_DestroyTexture(this->weights_text_texture);
}
}
8 changes: 7 additions & 1 deletion game/screens/ai_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ namespace Screens{
uint8_t gen_time = 30; //in seconds
uint32_t control_tick = 0;

Population population{2000, board_w, board_h, 20};
Population population{3000, board_w, board_h, 20};

SDL_Renderer* render;
TTF_Font* font = TTF_OpenFont("./assets/pressstart.ttf", 20);
TTF_Font* tiny_font = TTF_OpenFont("./assets/pressstart.ttf", 12);
SDL_Color text_color{ 255, 255, 255 };

SDL_Rect score_text_shape;
Expand Down Expand Up @@ -68,5 +69,10 @@ namespace Screens{
SDL_Rect timer_shape;
SDL_Texture* timer_text_texture = nullptr;
SDL_Texture* timer_texture = nullptr;

SDL_Rect weights_text_shape;
SDL_Texture* weights_text_texture = nullptr;

void save_weights();
};
};
39 changes: 27 additions & 12 deletions genetic/population.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,50 @@ namespace Genetic{
// fitness for catching the food
if(board->get_caught_the_food())
ind->fitness += 5000;

if(player->get_score() >= this->total_food){

uint16_t score = player->get_score();
if(score > this->best_score)
this->best_score = score;

if(score >= this->total_food){
player->set_died();
this->total_win++;
this->total_alive--;
ind->fitness += 20000;
this->update_best_fitness(ind);
continue;
}

if(player->is_dead()){
ind->fitness += -1000;
this->update_best_fitness(ind);
continue;
}

this->update_best_fitness(ind);
this->update_individual_food_position(ind);
}
}

void Population::update_best_fitness(Individual* ind){
if(ind->fitness > this->best_fitness)
this->best_fitness = ind->fitness;
}

void Population::next_gen(){
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;
Gene* offspring_genes = offspring->get_genes();

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);
vec2 first_food_pos = this->food_positions.at(0);
Expand All @@ -161,7 +183,7 @@ namespace Genetic{
else{
Chromosome* player_chromosome = new Chromosome(offspring_ch_size);
player_chromosome->copy_genes(offspring_genes);
player_chromosome->mutate(0.02);
player_chromosome->mutate(0.3);

ind->player = new AIPlayer(board_w, board_h, player_chromosome);
}
Expand All @@ -175,7 +197,6 @@ namespace Genetic{

this->individuals.push_back(ind);
}
delete parents;
}

Individual** Population::select_parents(){
Expand Down Expand Up @@ -238,13 +259,7 @@ namespace Genetic{
}

uint16_t Population::get_best_score(){
uint16_t best_score = this->individuals.at(0)->player->get_score();
for(Individual* ind: this->individuals){
uint16_t ind_score = ind->player->get_score();
if(ind_score > best_score)
best_score = ind_score;
}
return best_score;
return this->best_score;
}

uint16_t Population::get_total_alive(){
Expand All @@ -256,7 +271,7 @@ namespace Genetic{
}

int64_t Population::get_best_fitness(){
return this->get_best_individual()->fitness;
return this->best_fitness;
}

void Population::clear(){
Expand Down
8 changes: 7 additions & 1 deletion genetic/population.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Genetic {
public:
Population(uint16_t total, uint8_t board_w, uint8_t board_h, uint8_t total_food);
Individual* get_best_alive_individual();
Individual* get_best_individual();
void next_gen();
void run();
uint32_t get_gen();
Expand All @@ -53,10 +54,15 @@ namespace Genetic {
uint16_t total_win = 0;
uint8_t board_w = 0;
uint8_t board_h = 0;

uint16_t best_score = 0;
int64_t best_fitness = -1000000000000;

void update_best_fitness(Individual* ind);

vector<Individual*> individuals;
vector<vec2> food_positions;

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

0 comments on commit fb91fbe

Please sign in to comment.