Pac-Man Evolution
Loading...
Searching...
No Matches
GeneticAlgorithm.h
1/*----------------------------------------------------------------------
2Pac-Man Evolution - Roberto Prieto
3Copyright (C) 2018-2024 MegaStorm Systems
4contact@megastormsystems.com - http://www.megastormsystems.com
5
6This software is provided 'as-is', without any express or implied
7warranty. In no event will the authors be held liable for any damages
8arising from the use of this software.
9
10Permission is granted to anyone to use this software for any purpose,
11including commercial applications, and to alter it and redistribute it
12freely, subject to the following restrictions:
13
141. The origin of this software must not be misrepresented; you must not
15claim that you wrote the original software. If you use this software
16in a product, an acknowledgment in the product documentation would be
17appreciated but is not required.
182. Altered source versions must be plainly marked as such, and must not be
19misrepresented as being the original software.
203. This notice may not be removed or altered from any source distribution.
21
22------------------------------------------------------------------------
23
24Genetic Algorithm class
25
26Based on the amazing docs created by Mat Buckland (http://www.ai-junkie.com/ga/intro/gat1.html)
27
28------------------------------------------------------------------------ */
29
30#ifndef GENETICALGORITHM_H
31#define GENETICALGORITHM_H
32
33// Includes and forward definitions
34#include "MemoryManager.h"
35
36// Genome struct
37struct Genome
38{
39 vector<double> vWeights;
40 double dFitness;
41
42 Genome() { dFitness = 0; }
43 Genome(vector<double> w, double f) { vWeights = w; dFitness = f; }
44
45 // Overload '<' used for sorting
46 friend bool operator<(const Genome& lhs, const Genome& rhs)
47 {
48 return(lhs.dFitness < rhs.dFitness);
49 }
50};
51
52// Genetic algorithm class
53class GeneticAlgorithm : public CMemPME
54{
55 public:
56 // Constructor
57 GeneticAlgorithm(Sint32, double, double, double, Sint32, Sint32, Sint32);
58
59 // Initialize the algorithm
60 void init(Sint32, vector<double>);
61
62 // Evolve one generation
63 vector<Genome> epoch(vector<Genome>&);
64
65 // Access to attributes
66 vector<Genome> getChromos()const;
67 double fitnessAverage()const;
68 double fitnessBest()const;
69 Sint32 getMutation()const;
70 Sint32 getCrossOver()const;
71
72private:
73 // Hold all population's chromosomes (NN weights plus the fitness)
74 vector<Genome> vPopulation;
75
76 // Population and generation
77 Sint32 iPopulation;
78 Sint32 iGeneration;
79
80 // Amount of weights per chromo
81 Sint32 iChromoLength;
82
83 // Fitness vars
84 double dTotalFitness;
85 double dFitnessBest;
86 double dFitnessAverage;
87 double dWorstFitness;
88
89 // Keeps best genome
90 Sint32 iFittestGenome;
91
92 // Internal vars
93 double dMutationRate;
94 double dCrossoverRate;
95 double dMaxPerturbation;
96 Sint32 iNumElite, iNumCopiesElite;
97
98 // Stats vars
99 Sint32 iNumMutation;
100 Sint32 iNumCrossOver;
101
102 // Private methods doing the real job
103 void crossover(const vector<double>&, const vector<double>&, vector<double>&, vector<double>&);
104 void mutate(vector<double>&);
105 Genome getChromoRoulette();
106 void grabNBest(Sint32, const Sint32, vector<Genome>&);
107 void calculateStats();
108 void reset();
109};
110
111#endif
112