-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenome.h
108 lines (92 loc) · 2.93 KB
/
Genome.h
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
// Main reference: https://nn.cs.utexas.edu/downloads/papers/stanley.ec02.pdf
#ifndef GENOME_CLASS_H
#define GENOME_CLASS_H
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <json/json.h>
#include "Helpers/Mathematics.h"
#include "Helpers/Utils.h"
#include "Globals.h"
enum LayerType {
Sensor,
Hidden,
Output
};
class Node {
public:
int index;
LayerType type;
// Output Activation
float outputActivation;
float input;
std::vector<int> connectionIndices;
int layerIndex;
Node( int id, LayerType type );
// Activation Function
void Activate();
static float SigmoidActivation( float value );
static float TanHActivation( float value );
void ShowData();
};
struct Connection {
int inNodeIndex;
int outNodeIndex;
float weight;
bool isEnabled;
int innovNum;
};
// Consists of Nodes and Connections
class Genome {
private:
// static global innovation Number
static int innovNumber;
// Global Genome Counter
static int globalCounter;
// HashMap var containing connection innovation history
static std::map<std::string, int> innoDictionary;
static int GetInnovationNum( int inIndex, int outIndex );
// Counter for assigning node ids
int nodeCounter = 0;
std::vector<int> GetRandomConnIndices();
public:
int id = -1;
int generation = -1;
int inputCount = 0;
int outputCount = 0;
float fitness = -1.0f;
std::vector<Node> nodes;
std::vector<Connection> connections;
static Genome GenerateTestGenome();
static void ResetGenomeCounter();
Genome( int inputCount, int outputCount, int gen = -1 );
Genome( const Genome& copy, int gen = -1 );
Genome( const char* path );
void SaveToJSON( const char* path );
void Initialize( int inputCount, int outputCount );
bool CreateConnection(
int inNodeIndex,
int outNodeIndex,
float weight,
bool isEnabled
);
bool CreateConnection( const Connection& connection );
bool SetConnectionEnable( int index, bool value );
void ShowNodeData();
int AddNode( LayerType type );
bool AddHiddenNodeWithId( int id );
int GetHiddenNodeCount() const;
void Mutate();
void MutateConnectionWeights();
bool AddRandomConnection();
void InsertNodeRandom();
Genome* CrossOver( const Genome& other );
float GetAverageGeneWeight() const;
void GetExcessDisjointCount( const Genome& other, int& eCount, int& dCount );
// Compare function that inputs other genome. Returns the number of Disjoint, Excess genes, avg diff weights, and N
bool IsCompatible( const Genome& other );
};
#endif