-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCellSim.hpp
More file actions
135 lines (130 loc) · 4.24 KB
/
CellSim.hpp
File metadata and controls
135 lines (130 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef EVOAI_CELL_SIM_HPP
#define EVOAI_CELL_SIM_HPP
#include <SFML/Graphics.hpp>
#include <EvoAI.hpp>
#include "Tournament.hpp"
#include "Options.hpp"
#include "Averages.hpp"
#include "Cell.hpp"
#include "Trainer.hpp"
#include "Timer.hpp"
#include <stack>
namespace EvoAI{
/**
* @brief CellSim App example
*/
class CellSim final{
public:
using Clock = std::chrono::system_clock;
public:
/**
* @brief constructor
* @param opts
*/
CellSim(const Options& opts);
/**
* @brief loads a
* @param o JsonBox::Object
*/
CellSim(JsonBox::Object o);
/**
* @brief converts CellSim to JsonBox::Value
* @return JsonBox::Value
*/
JsonBox::Value toJson() const noexcept;
/**
* @brief write the simulation into a file.
* @param filename
*/
void writeToFile(std::string_view filename) const noexcept;
/**
* @brief run the app
*/
void run() noexcept;
/**
* @brief help aka usage for the cli
*/
static void usage() noexcept;
private: // population management
/**
* @brief static member func to make ids for cells.
*/
static inline std::size_t genID() noexcept;
/**
* @brief Fn to give to Population, manages the creation of cells
* @return Cell*
*/
Cell* createCells() noexcept;
/**
* @brief replaces the cells that are dead with the new generation.
* @param toReplace std::vector<Cell*>&
* @param toAdd std::vector<Cell>&
*/
void replace(std::vector<Cell*>& toReplace, std::vector<Cell>& toAdd) noexcept;
/**
* @brief removes the species that are stagnant
* @param ids std::vector<std::size_t>&&
*/
void removeCellsFromSpecies(std::vector<std::size_t>&& ids) noexcept;
/**
* @brief uses SelectionAlgorithms::Tournament<Cell*> to reproduce
* the best and substitute the dead cells
*/
void nextGeneration() noexcept;
private:
/**
* @brief configure GUI.
*/
void setupGUI() noexcept;
/**
* @brief makes a visualization of brain activity.
* @return sf::VertexArray
*/
sf::VertexArray getBrainActivity(Cell& cell) noexcept;
/**
* @brief makes a Genome with a hybrid topology for Cells.
* @return Genome
*/
Genome makeCellGenome() noexcept;
/**
* @brief checks collisions
* @todo add a quadtree if you want to be able to handle more cells
* @param c1 Cell&
* @param c2 Cell&
*/
void checkCollision(Cell& c1, Cell& c2) noexcept;
/**
* @brief fixes the position of the cell inside to keep inside the screen.
* @param c Cell&
*/
void checkBounds(Cell& c) noexcept;
private:
void handleInput(sf::Event& e) noexcept;
void update(sf::Time dt) noexcept;
void updateFPS(sf::Time dt) noexcept;
void render() noexcept;
private:
Options opts;
sf::FloatRect bounds;
sf::RenderWindow win;
std::vector<Cell> cells;
sf::Font FPSFont;
sf::Text FPSText;
sf::Text sizeText;
sf::Text genInfo;
std::unique_ptr<Population<Cell*>> pop;
std::unique_ptr<Averages> avgs;
std::size_t FPSNumFrames;
std::size_t gen;
std::size_t lastAlive;
sf::Time FPSUpdateTime;
Timer nextGenTimer;
Trainer trainer;
static const sf::Time TimePerFrame;
};
std::size_t CellSim::genID() noexcept{
static std::size_t id = 0u;
return id++;
}
}
#endif // EVOAI_CELL_SIM_HPP