Pac-Man Evolution
Loading...
Searching...
No Matches
EVNTrainer.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
24EVolution Neural Trainer
25
26------------------------------------------------------------------------ */
27
28#ifndef EVNTRAINER_H
29#define EVNTRAINER_H
30
31#define DEBUG_EVN
32
33// Includes and forward definitions
34#include "MemoryManager.h"
35#include "ArtificialNeuralNet.h"
36class GameField;
37struct GlobalStatus;
38class GeneticAlgorithm;
39struct Genome;
40class BrainEvolved;
41
42#define PME_GHOSTS_NN_FILE "neuralnet.cdc"
43#define PME_GHOSTS_NN_BLOCK "ghosts.xml"
44
45#define PME_GA_CROSSOVER_RATE 0.7
46#define PME_GA_MUTATION_RATE 0.2
47#define PME_GA_MAX_PERTURBATION 0.4
48#define PME_GA_NUM_ELITE 2
49#define PME_GA_NUM_COPIES_ELITE 2
50#define PME_GA_POPULATION 15 // Match PME_BRAIN_TYPE_TRAININGx defined in BrainsFactory.h
51
52#define PME_ANN_GHOST_RED_INPUT 2
53#define PME_ANN_GHOST_RED_HIDDEN 0
54#define PME_ANN_GHOST_RED_ACTIVATION ANN_ACTIVATION_SIGMOID
55
56#define PME_ANN_GHOST_PINK_INPUT 4
57#define PME_ANN_GHOST_PINK_HIDDEN 3
58#define PME_ANN_GHOST_PINK_ACTIVATION ANN_ACTIVATION_SIGMOID
59
60#define PME_ANN_GHOST_BLUE_INPUT 3
61#define PME_ANN_GHOST_BLUE_HIDDEN 2
62#define PME_ANN_GHOST_BLUE_ACTIVATION ANN_ACTIVATION_SIGMOID
63
64#define PME_ANN_GHOST_ORANGE_INPUT 3
65#define PME_ANN_GHOST_ORANGE_HIDDEN 0
66#define PME_ANN_GHOST_ORANGE_ACTIVATION ANN_ACTIVATION_SIGMOID
67
68#define PME_ANN_GHOST_OUTPUT 2
69
70// EVNTrainer class implemented as a singleton
71class EVNTrainer : public CMemPME
72{
73public:
74 static EVNTrainer& Instance(); // Singleton accessor by object reference
75 static void Terminate(); // Explicit singleton destruction
76
77 Sint32 load(string& sGhostName, ArtificialNeuralNet* pNN, string& sCDCFile, string& sXMLName);
78 Sint32 execute(GlobalStatus&, GameField&);
79
80private:
81 EVNTrainer(); // Heap-based and stack-based creation are forbidden.
82 ~EVNTrainer(); // Inheritance is also forbidden.
83 static EVNTrainer* mInstance;
84
85 // Creature, in our case we ca have up to 4 (Red, Pink, Blue and Orange ghosts)
86 struct Creature : public CMemPME
87 {
88 public:
89 Sint32 info();
90
91 Sint32 iID;
92 Sint32 iGeneration;
93
94 // Keep track of the best creature(s)
95 Sint32 iBestCreatureGeneration;
96 double dBestCreatureFitness;
97 Sint32 iNumBestCreatures;
98 vector<Genome> vBestCreaturesNN;
99
100 // Brain's creature pointers
101 vector<BrainEvolved*> vBrains;
102
103 // Genetics algorithm
104 GeneticAlgorithm* pGA;
105 vector<Genome> vGenomesPopulation;
106
107 // Constructor & Destructor
108 Creature(Sint32);
109 ~Creature();
110 };
111 vector<Creature*> vCreatures;
112
113 // Methods
114 Sint32 initCreatures(GlobalStatus& pGlobalStatus);
115 Sint32 createDefaultANN(const string& sGhostName, string sCDCFile, string sXMLName);
116 Sint32 saveCreatures(string sCDCFile, string sXMLName);
117 Sint32 updateNode(XML* pXML, Creature* pCreature, Sint32 iNum);
118 Sint32 parseWeights(char*, vector<double>&);
119
120 // Genetics algorithm attributes common to all creatures
121 Sint32 iPopulation;
122 double dMutationRate;
123 double dCrossOverRate;
124 double dMaxPerturbation;
125 Sint32 iNumElite;
126 Sint32 iNumCopiesElite;
127};
128
129#endif