12 #include <boost/archive/text_oarchive.hpp> 13 #include <boost/archive/text_iarchive.hpp> 14 #include <boost/serialization/vector.hpp> 21 #include <sys/types.h> 108 template <
typename Fitness>
121 void run(
bool fresh, Fitness fitness);
124 template <
typename Fitness>
126 std::time_t t = std::time(0);
127 std::tm* now = std::localtime(&t);
128 const char* rom =
ALE.romSettings->rom();
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));
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));
146 snake.open(fichier_stat);
154 <<
", Taille du pool : " 165 double best_value = 0;
176 if (*it > best_value){
177 std::cout <<
"ga iter: " 185 std::ofstream ofs(best);
186 boost::archive::text_oarchive oa(ofs);
190 bond.open(fichier_instr);
208 std::ofstream ofpop(
"best_pop.txt");
209 boost::archive::text_oarchive opop(ofpop);
214 std::cout << sp <<
" agents archived successfully" << std::endl;
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
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
int population_size
Definition: ga.hpp:45
double crossover_rate
Definition: ga.hpp:43
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 ¶ms, 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