blob: 0c2c1bccafbf73a779140c2d8dcf016e1cee70d6 (
plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include <HyperNeat/Cppn.hpp>
#include <HyperNeat/Behavior.hpp>
#include <HyperNeat/NeuralNet.hpp>
#include <HyperNeat/Population.hpp>
#include <HyperNeat/Utils/Thread.hpp>
#include <HyperNeat/NeuralNetPrms.hpp>
using namespace std;
using namespace hyperneat;
Organism::Organism(const Organism& other)
{
*this = other;
}
Organism&
Organism::operator=(const Organism& other)
{
_fitness = other._fitness;
_index = other._index;
_isLocked = other._isLocked;
_isFrozen = other._isFrozen;
_specie = other._specie;
_lifetime = other._lifetime;
_genome = other._genome;
_population = other._population;
return *this;
}
size_t
Organism::getIndex() const
{
return _index;
}
void
Organism::lock()
{
if (!_isLocked) {
_isLocked = true;
++_population->_lockedOrganisms;
}
}
void
Organism::unlock()
{
if (_isLocked) {
_isLocked = false;
--_population->_lockedOrganisms;
}
}
bool
Organism::isLocked() const
{
return _isLocked;
}
void
Organism::freeze()
{
if (!_isFrozen) {
_isFrozen = true;
++_population->_frozenOrganisms;
}
}
void
Organism::unfreeze()
{
if (_isFrozen) {
_isFrozen = false;
--_population->_frozenOrganisms;
}
}
bool
Organism::isFrozen() const
{
return _isFrozen;
}
bool
Organism::isBeingGenerated() const
{
return _isBeingGenerated;
}
size_t
Organism::getSpecie() const
{
return _specie;
}
bool
Organism::isOld() const {
return _lifetime >= _population->_prms._minimumLifetime;
}
size_t
Organism::getLifetime() const
{
return _lifetime;
}
Behavior&
Organism::getBehavior()
{
return *_behavior;
}
const Genome&
Organism::getGenome() const
{
return _genome;
}
bool
Organism::isChampion() const
{
return &_population->getChampion() == this;
}
Population&
Organism::getPopulation() const
{
return *_population;
}
void
Organism::createNeuralNet()
{
if (_isBeingGenerated) {
return;
}
_isBeingGenerated = true;
++_population->_organismsBeingGenerated;
Thread generator([&]() {
Cppn cppn;
cppn.create(_genome);
_neuralNet->clear();
_neuralNet->create(cppn, _population->getNeuralNetPrms());
_isBeingGenerated = false;
--_population->_organismsBeingGenerated;
});
generator.detach();
}
Organism::Organism(Population* population)
: _population(population)
{}
Organism::Organism(size_t inputs, Population* population)
: _genome(inputs), _population(population)
{}
void
Organism::reset(bool archive)
{
unlock();
unfreeze();
if (isOld()) {
--_population->_oldOrganisms;
}
_lifetime = 0;
_fitness = 0.0;
if (_behavior) {
_behavior->reset(archive);
}
}
|