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 "agent.hpp"
14 
15 class ga
16 {
17 public:
18  struct parameters
19  {
20  double mutation_rate;
25  };
26 private:
27  const parameters params;
28  std::vector<agent> current_population;
29  std::vector<agent> new_population;
30  std::vector<double> population_fitness;
31 
32  int selection() const;
33  void init_population();
34  void select_population();
35  void crossover_population();
36  void mutate_population();
37 public:
38  ga(const parameters& params, const agent::parameters& agent_params);
39  template <typename Fitness>
40  void run(Fitness fitness);
41 };
42 
43 template <typename Fitness>
44 void ga::run(Fitness fitness)
45 {
46  init_population();
47  double best_value = 0;
48  agent best_agent;
49  for (int i = 1; i <= params.nb_iterations; ++i)
50  {
51  fitness->run(i, params.nb_iterations, current_population, population_fitness);
52  auto it = std::max_element(population_fitness.begin(), population_fitness.end());
53  if (*it > best_value)
54  {
55  std::cout << "ga iter: "
56  << i
57  << " fitness: "
58  << *it
59  << std::endl;
60  best_value = *it;
61  int best_index = std::distance(population_fitness.begin(), it);
62  best_agent = current_population[best_index];
63  std::ofstream ofs("best_agent.txt");
64  boost::archive::text_oarchive oa(ofs);
65  oa << best_agent;
66  }
67  select_population();
68  crossover_population();
69  mutate_population();
70  std::swap(current_population, new_population);
71  current_population[0] = best_agent;
72  std::sort(population_fitness.begin(), population_fitness.end(), std::greater<double>());
73  // for (int i = 0; i < params.population_size / 10; ++i)
74  // {
75  // std::cout << population_fitness[i] << " | ";
76  // }
77  // std::cout << std::endl;
78  }
79 }
Definition: ga.hpp:18
Definition: agent.hpp:7
Definition: agent.hpp:5
int nb_iterations
Definition: ga.hpp:24
int tournament_size
Definition: ga.hpp:22
double mutation_rate
Definition: ga.hpp:20
int population_size
Definition: ga.hpp:23
double crossover_rate
Definition: ga.hpp:21
ga(const parameters &params, const agent::parameters &agent_params)
Definition: ga.cpp:4
void run(Fitness fitness)
Definition: ga.hpp:44
Definition: ga.hpp:15