IAtari
Genetic algorithm generating AI capable to play Atari2600 games.
agent.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <vector>
3 #include <boost/serialization/vector.hpp>
4 
16 struct agent{
22  struct parameters{
23  int input_size;
28  int nb_parts;
36  parameters() = default;
37 
52  int program_size, int nb_parts, int time_by_part)
56  {}
57 
58  template <typename Archive>
67  void serialize(Archive& ar, const unsigned int)
68  {
69  ar & input_size
70  & output_size
71  & nb_registers
73  & program_size
74  & nb_parts
75  & time_by_part;
76  }
77  };
78 
84  enum opcode
85  {
102  };
103  using instruction = uint64_t;
104  using reg = double;
106  std::vector<instruction> program;
107  std::vector<reg> registers;
108  int nb_steps = 0;
109  int current_part = 0;
116  agent() = default;
117 
125  agent(const parameters& params);
126 
134  int get_action(const std::vector<unsigned char>& screen);
135 
136  template <typename Archive>
145  void serialize(Archive& ar, const unsigned int)
146  {
147  ar & params
148  & program
149  & registers;
150  }
151 
157  void reset_registers();
158 
164  void reset();
165 
173  inline void execute_instruction(const std::vector<unsigned char>& screen, int& pc);
174 
181  inline auto begin_outputs();
182 
189  inline auto end_outputs();
190 
198  std::string reg_to_string(int reg) const;
199 
207  void print_instruction(std::ostream&, instruction ins) const;
208 };
209 
216 std::ostream& operator<<(std::ostream&, const agent&);
217 
223 namespace genetic_operators
224 {
231  void initialize(agent&);
232 
239  void mutate(agent&);
240 
248  void crossover(agent&, agent&);
249 }
Definition: agent.hpp:94
Definition: agent.hpp:101
int time_by_part
Definition: agent.hpp:29
Definition: agent.hpp:97
int nb_long_term_registers
Definition: agent.hpp:26
parameters(int input_size, int output_size, int nb_registers, int nb_long_term_registers, int program_size, int nb_parts, int time_by_part)
Constructor entirely initializing parameters.
Definition: agent.hpp:50
Definition: agent.hpp:96
int nb_steps
Definition: agent.hpp:108
void serialize(Archive &ar, const unsigned int)
Save/Load parameters with archive.
Definition: agent.hpp:67
void mutate(agent &a)
Mutate an agent, i.e. modify some instructions.
Definition: agent.cpp:327
void initialize(agent &a)
Initialize an agent with random instructions.
Definition: agent.cpp:319
void reset()
Fills all the registers files with 0 and set nb_steps and current_part to 0.
Definition: agent.cpp:19
Represents the caracteristics of an agent.
Definition: agent.hpp:22
auto begin_outputs()
Access the beginning of the register file "outputs".
Definition: agent.cpp:121
int input_size
Definition: agent.hpp:23
agent()=default
Standard constructor (Empty agent)
int nb_parts
Definition: agent.hpp:28
Definition: agent.hpp:99
void execute_instruction(const std::vector< unsigned char > &screen, int &pc)
Perform the operation corresponding to this instruction.
Definition: agent.cpp:31
void reset_registers()
Fills the registers "temporary" and "outputs" files with 0.
Definition: agent.cpp:26
int program_size
Definition: agent.hpp:27
Represents an agent, i.e. an AI.
Definition: agent.hpp:16
Handle genetic operations performed on agents.
Definition: agent.cpp:271
parameters()=default
Standard constructor.
Definition: agent.hpp:90
Definition: agent.hpp:93
Definition: agent.hpp:88
opcode
Enumeration of all the operation available for an agent.
Definition: agent.hpp:84
auto end_outputs()
Access the end of the register file "outputs".
Definition: agent.cpp:126
std::string reg_to_string(int reg) const
Represent a register as a string.
Definition: agent.cpp:166
void serialize(Archive &ar, const unsigned int)
Save/Load agent with archive.
Definition: agent.hpp:145
int output_size
Definition: agent.hpp:24
Definition: agent.hpp:98
std::ostream & operator<<(std::ostream &, const agent &)
Operator << overloaded so that an agent's program can be displayed by using this operator directly on...
Definition: agent.cpp:260
Definition: agent.hpp:92
int get_action(const std::vector< unsigned char > &screen)
Execute the program on a particular frame and choose an action.
Definition: agent.cpp:131
Definition: agent.hpp:86
Definition: agent.hpp:95
std::vector< reg > registers
Definition: agent.hpp:107
parameters params
Definition: agent.hpp:105
int nb_registers
Definition: agent.hpp:25
Definition: agent.hpp:89
Definition: agent.hpp:91
void crossover(agent &a1, agent &a2)
Swap instructions between two agents.
Definition: agent.cpp:345
void print_instruction(std::ostream &, instruction ins) const
Display an instruction as a String through a outputStream.
Definition: agent.cpp:177
Definition: agent.hpp:100
Definition: agent.hpp:87
uint64_t instruction
Definition: agent.hpp:103
double reg
Definition: agent.hpp:104
std::vector< instruction > program
Definition: agent.hpp:106
int current_part
Definition: agent.hpp:109