-
Notifications
You must be signed in to change notification settings - Fork 0
/
sevalengine.h
154 lines (127 loc) · 3.59 KB
/
sevalengine.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef SEVALENGINE_H
#define SEVALENGINE_H
#include "snode.h"
#include <vector>
#include "sortedarray.h"
/*
* Stores and evaluates a graph of SNodes.
*
* Initialisation and GP mutation functions are also available.
*/
class SEvalEngine
{
public:
SEvalEngine();
virtual ~SEvalEngine();
/*
* Set the number of nodes.
*/
void setSize(int size) { _size = size; }
/*
* Get the number of nodes.
*/
int getSize() { return _size; }
/*
* Generates new set of nodes and resets any internal state
*/
void init();
/*
* Set the number of nodes to act as inputs
*/
void setNumInputs(int inputs) { _numInputs = inputs; }
/*
* Set the list of available ops used to generate the random
* list of nodes.
*/
void setAvailableOps(const std::vector<SNode::Op>& ops);
const std::vector<SNode>& getNodes() { return _nodes; }
/*
* Evaluates all nodes.
*
* The param 'values' is the stored list of results from
* the last time evalAll() or evalChanged() was called.
* Must be the same number of values as there are nodes
* (including inputs).
*/
void evalAll(std::vector<int> &values);
/*
* Mutate a random node.
*/
void mutate();
/*
* Restore the previous mutation.
*/
void restore();
/*
* Clear list of changed nodes, should only call after
* evalAll() or evalChanged().
*/
void clearChanged();
/*
* Eval a single node.
* The param 'values' is the stored list of results from
* the last time evalAll() or evalChanged() was called.
* Must be the same number of values as there are nodes
* (including inputs).
*/
int evalNode(int i, const std::vector<int> &values);
int evalNode(const SNode &node, const std::vector<int> &values);
/*
* Get the nodes that have changed by mutate() and
* restore() since the last time clearChanged() was called.
* Also returns any nodes that depend on the values
* returned by the nodes that have been modified.
*/
SortedArray<int>& getChangedNodes() { return _changedNodes; }
private:
/*
* Useful during debugging.
* Verify that all links in the link list are real.
* Fast.
*/
bool verifyLinksExist();
/*
* Useful during debugging.
* Verify all links exist that should exist exist
* and that there are no extra links.
* Slow.
*/
bool verifyAllLinks();
/*
* Generate a random node at 'i'.
*/
void randomise(int i);
/*
* Mutate one of the links for the given node at
* index 'i'. The node operation stays the same.
*/
void smut(int i);
/*
* Switch a link in the link list for the given node.
*/
void switchLink(int i, int oldLink, int newLink);
/*
* Mark that a node has to be reevaluated. Any nodes that
* depend on that node also have to be reevaluated.
*/
void markChanged(int index);
/*
* Generate all links
*/
void generateLinks();
int _numInputs;
int _size;
std::vector<SNode> _nodes;
// Available operators
std::vector<SNode::Op> _ops;
// For each node, the set of nodes that refer back to that node
typedef std::vector<int> NodeLinks;
typedef NodeLinks::iterator NodeLinksIterator;
std::vector<NodeLinks> _nodeLinks;
// Ordered list of nodes that were changed by smut() and/or restore()
SortedArray<int> _changedNodes;
// The last node that was changed by smut()
SNode _oldNode;
int _oldNodeIndex;
};
#endif // SEVALENGINE_H