Pac-Man Evolution
Loading...
Searching...
No Matches
ArtificialNeuralNet.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
24Artificial Neural Network class
25
26Based on the amazing docs created by Mat Buckland (http://www.ai-junkie.com/ann/evolved/nnt1.html)
27
28------------------------------------------------------------------------ */
29
30#ifndef ARTIFICIALNEURALNET_H
31#define ARTIFICIALNEURALNET_H
32
33// Includes and forward definitions
34#include "MemoryManager.h"
35
36// Enable or disable Bias
37// https://stackoverflow.com/questions/2480650/role-of-bias-in-neural-networks
38//#define ANN_ENABLE_BIAS
39
40// Activation function
41#define ANN_ACTIVATION_LINEAR 0
42#define ANN_ACTIVATION_STEP 1
43#define ANN_ACTIVATION_SIGMOID 2
44
45// Weight interval and precision
46#define ANN_WEIGHT_MIN -2
47#define ANN_WEIGHT_MAX 2
48#define ANN_PRECISION 0.000001
49
50// Function for generating random weights in [ANN_WEIGHT_MIN, ANN_WEIGHT_MAX] with ANN_PRECISION
51double generateWeights(double dMin = ANN_WEIGHT_MIN, double dMax = ANN_WEIGHT_MAX);
52// Function for applying ANN_PRECISION
53double applyWeightPrecision(double);
54
55// Neuron struct
56struct Neuron
57{
58 // The number of inputs into the neuron
59 Sint32 iNumInputs;
60
61 // The weights for each input
62 vector<double> vWeight;
63
64 // Constructor
65 Neuron(Sint32 NumInputs);
66};
67
68// Layer of neurons
69struct NeuronLayer
70{
71 // The number of neurons in this layer
72 Sint32 iNumNeurons;
73
74 // The layer of neurons
75 vector<Neuron> vNeurons;
76
77 NeuronLayer(Sint32, Sint32);
78};
79
80// Artificial Neural Network class
81class ArtificialNeuralNet
82{
83public:
84 ArtificialNeuralNet();
85 void init(Sint32, Sint32, Sint32, Sint32);
86
87 // Activation function
88 void setActivationFunction(Sint32);
89 Sint32 getActivationFunction();
90
91 // Gets the weights from the NN
92 vector<double> getWeights();
93
94 // Get neural network components
95 Sint32 getNumOutputs();
96 Sint32 getNumInputs();
97 Sint32 getNumHiddenLayers();
98 Sint32 getNumNeuronsPerLayer();
99
100 // Returns total number of weights in net
101 Sint32 getNumberOfWeights();
102
103 // Replaces the weights with new ones
104 Sint32 setWeights(vector<double> &vW);
105
106 // Calculates the outputs from a set of inputs
107 vector<double> update(vector<double> &inputs);
108
109private:
110 Sint32 iActivationFunc;
111 Sint32 iNumInputs;
112 Sint32 iNumOutputs;
113 Sint32 iNumHiddenLayers;
114 Sint32 iNeuronsPerHiddenLyr;
115
116 // Storage for each layer of neurons including the output layer
117 vector<NeuronLayer> vNeuronsLayers;
118
119 // Activation functions:
120 inline double sigmoid(double, double); // Sigmoid response curve
121 inline double linear(double, double); // Linear
122 inline double step(double, double); // Step
123};
124
125#endif