IAtari
Genetic algorithm generating AI capable to play Atari2600 games.
ga.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <utility>
5 #include <iostream>
6 #include <climits>
7 #include <algorithm>
8 #include <cassert>
9 #include <functional>
10 #include "Random.h"
11 #include <fstream>
12 #include <boost/archive/text_oarchive.hpp>
13 #include <boost/archive/text_iarchive.hpp>
14 #include <boost/serialization/vector.hpp>
15 #include "agent.hpp"
16 #include <ctime>
17 #include <iostream>
18 #include <string>
19 #include "Utils.hpp"
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
34 class ga{
35  public:
41  struct parameters{
42  double mutation_rate;
43  double crossover_rate;
47  };
48  private:
50  std::vector<agent> current_population;
51  std::vector<agent> new_population;
52  std::vector<double> population_fitness;
60  int selection() const;
61 
67  void init_population();
68 
75  void fill_population();
76 
82  void select_population();
83 
89  void crossover_population();
90 
96  void mutate_population();
97 
98  public:
106  ga(const parameters& params, const agent::parameters& agent_params);
107 
108  template <typename Fitness>
121  void run(bool fresh, Fitness fitness);
122 };
123 
124 template <typename Fitness>
125 void ga::run(bool fresh, Fitness fitness){
126  std::time_t t = std::time(0);
127  std::tm* now = std::localtime(&t);
128  const char* rom = ALE.romSettings->rom(); //Nom de la rom
129 
130  char aux[100];
131  char best[100];
132  char fichier_stat[100];
133  char fichier_instr[100];
134  sprintf(best,"agents/%s_%d_%d",rom,(now->tm_mon +1),(now->tm_mday));
135  mkdir(best,0777);
136  sprintf(fichier_stat,"%s/stat_agent_%s_%d_%d.txt",best,rom,(now->tm_mon +1),(now->tm_mday));
137  sprintf(fichier_instr,"%s/instr_agent_%s_%d_%d.txt",best,rom,(now->tm_mon +1),(now->tm_mday));
138  sprintf(aux,"/best_agent_%s_%d_%d.txt",rom,(now->tm_mon +1),(now->tm_mday));
139  strcat(best,aux);
140  printf("%s\n",best);
141 
142  std::ofstream snake; //ofstream du fichier de stat (Liste des meilleurs scores)
143  std::ofstream bond; //ofstream du programme des best agent
144 
145  //Intro du fichier de stat
146  snake.open(fichier_stat);
147  snake << "Rom : "
148  << rom
149  << std::endl
150  << "Mutation : "
152  << ", Croisement : "
154  << ", Taille du pool : "
156  << ", Population : "
158  << std::endl;
159 
160  if(fresh){
161  init_population();
162  }else{
163  fill_population();
164  }
165  double best_value = 0;
166  agent best_agent;
167  for (int i = 1; i <= params.nb_iterations; ++i){
169  auto it = std::max_element(population_fitness.begin(), population_fitness.end());
170 
171  snake << *it
172  << ";"
173  << i
174  <<std::endl;
175 
176  if (*it > best_value){
177  std::cout << "ga iter: "
178  << i
179  << " fitness: "
180  << *it
181  << std::endl;
182  best_value = *it;
183  int best_index = std::distance(population_fitness.begin(), it);
184  best_agent = current_population[best_index];
185  std::ofstream ofs(best);
186  boost::archive::text_oarchive oa(ofs);
187  oa << best_agent;
188  ofs.close();
189 
190  bond.open(fichier_instr);
191  bond << best_agent;
192  bond.close();
193  }
198  current_population[0] = best_agent;
199  std::sort(population_fitness.begin(), population_fitness.end(), std::greater<double>());
200  // for (int i = 0; i < params.population_size / 10; ++i)
201  // {
202  // std::cout << population_fitness[i] << " | ";
203  // }
204  // std::cout << std::endl;
205  }
206  snake.close();
207 
208  std::ofstream ofpop("best_pop.txt");
209  boost::archive::text_oarchive opop(ofpop);
210  opop << current_population;
211  ofpop.close();
212 
213  auto sp = current_population.size();
214  std::cout << sp << " agents archived successfully" << std::endl;
215 }
Parameters of the genetic algorithm.
Definition: ga.hpp:41
void fill_population()
Fill the population with the last population saved.
Definition: ga.cpp:27
ALEInterface ALE
Definition: Utils.cpp:108
std::vector< agent > current_population
Definition: ga.hpp:50
Represents the caracteristics of an agent.
Definition: agent.hpp:22
Represents an agent, i.e. an AI.
Definition: agent.hpp:16
int nb_iterations
Definition: ga.hpp:46
void crossover_population()
Perform crossover on an entire population in accordance with the crossover rate.
Definition: ga.cpp:70
int tournament_size
Definition: ga.hpp:44
Header of Utils.hpp.
double mutation_rate
Definition: ga.hpp:42
void mutate_population()
Perform mutation on an entire population in accordance with the mutation rate.
Definition: ga.cpp:81
void select_population()
Create a new population by selecting good agent.
Definition: ga.cpp:62
void run(bool fresh, Fitness fitness)
Executes the genetic algorithm.
Definition: ga.hpp:125
Header of Random.cpp.
int population_size
Definition: ga.hpp:45
double crossover_rate
Definition: ga.hpp:43
Header of agent.cpp.
const parameters params
Definition: ga.hpp:49
void init_population()
Create whole new population with agents made of random instructions.
Definition: ga.cpp:19
ga(const parameters &params, const agent::parameters &agent_params)
Standard constructor. Initializes parameters of the genetic algorithm. Also initializes vectors corre...
Definition: ga.cpp:10
int selection() const
Select the best agent among a pool of randomly selected agent.
Definition: ga.cpp:45
std::vector< double > population_fitness
Definition: ga.hpp:52
Implementation of the genetic algorithm.
Definition: ga.hpp:34
std::vector< agent > new_population
Definition: ga.hpp:51