-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneat_xor_fitnesseval.cc
165 lines (127 loc) · 4.31 KB
/
neat_xor_fitnesseval.cc
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
#include <algorithm>
#include <random>
#include <chrono>
#include <cmath>
#include <iostream>
#include "Population.hh"
#include "Timer.hh"
struct EachAnswer {
EachAnswer(_float_ a, _float_ b)
: a(a), b(b), correct_result(int(a)^int(b)),
nn_result(std::numeric_limits<_float_>::quiet_NaN()) { }
_float_ a;
_float_ b;
_float_ correct_result;
_float_ nn_result;
};
class XorFitness : public FitnessEvaluator {
public:
XorFitness()
: all_answers{
EachAnswer(0,0),
EachAnswer(0,1),
EachAnswer(1,0),
EachAnswer(1,1)} {
//std::random_shuffle(all_answers.begin(), all_answers.end());
}
void step(NetProxy& proxy) {
for(auto& ans : all_answers) {
if(std::isnan(ans.nn_result)) {
proxy.request_calc({ans.a, ans.b},
[&](const auto& nn_output) {
ans.nn_result = nn_output[0];
});
return;
}
}
if(proxy.num_connections() == 0) {
proxy.set_fitness_value(0.0);
return;
}
double error = 0;
for(auto& ans : all_answers) {
//error += std::abs(ans.nn_result - ans.correct_result);
error += std::pow(ans.nn_result - ans.correct_result,2);
}
double fitness = std::pow(4.0 - error, 2); //16 - std::pow(cum_sum, 2);
proxy.set_fitness_value(fitness);
}
private:
std::array<EachAnswer, 4> all_answers;
};
std::array<EachAnswer, 4> inputs = {EachAnswer(0,0),
EachAnswer(0,1),
EachAnswer(1,0),
EachAnswer(1,1)};
int main() {
auto seed = Genome::ConnectedSeed(2,1);
auto prob = std::make_shared<Probabilities>();
prob->new_connection_is_recurrent = 0;
prob->keep_non_matching_father_gene = 0;
prob->population_size = 1000;
prob->number_of_children_given_in_nursery = 1000;
Population pop(seed,
std::make_shared<RNG_MersenneTwister>(100),
prob);
//pop.SetNetType<ConsecutiveNeuralNet>();
pop.SetNetType<ConcurrentGPUNeuralNet>();
pop.EnableCompositeNet(/*hetero_inputs = */false);
auto max_generations = 1000u;
std::unique_ptr<NeuralNet> winner = nullptr;
unsigned int generation;
auto show = [&](){
auto best = pop.BestNet<ConsecutiveNeuralNet>();
if(!best) { return; }
std::cout << " ----------- Gen " << generation << " ----------------" << std::endl;
auto num_species = pop.NumSpecies();
std::cout << num_species << " species total" << std::endl;
std::cout << pop.NumViableSpecies() << " viable species" << std::endl;
//for (auto i=0u; i< std::min(num_species,5u); i++) {
for (auto i=0u; i< num_species; i++) {
size_t size = pop.SpeciesSize(i);
if (size>0) {
std::cout << "- species #" << i << " size: " << pop.SpeciesSize(i) << std::endl;
}
}
std::cout << "Best (nodes, conn) = (" << best->num_nodes() << ", " << best->num_connections()
<< ")" << std::endl;
_float_ error = 0;
for(auto& input : inputs) {
_float_ val = best->evaluate({input.a, input.b})[0];
std::cout << input.a << " ^ " << input.b << " = " << val << std::endl;
error += std::abs(val - input.correct_result);
}
std::cout << "Error: " << error << std::endl;
};
std::function<std::unique_ptr<FitnessEvaluator>(void)> fitness_factory = [](){return std::make_unique<XorFitness>();};
for (generation = 0u; generation < max_generations; generation++) {
auto next_gen = pop.Reproduce(fitness_factory);
auto best = pop.BestNet<ConsecutiveNeuralNet>();
bool have_winner = true;
for(auto& input : inputs) {
_float_ val = best->evaluate({input.a, input.b})[0];
if(std::abs(val - input.correct_result) >= 0.5) {
have_winner = false;
break;
}
}
if(have_winner) {
winner = best->clone();
break;
}
if(generation%10 == 0) {
show();
}
pop = std::move(next_gen);
}
show();
if(winner) {
std::cout << "Winner found in generation " << generation << ".\n"
<< *winner << std::endl;
} else {
std::cout << "No winner found after " << generation << " generations" << std::endl;
pop.Evaluate(fitness_factory);
std::cout << "Best: " << *pop.BestNet<ConsecutiveNeuralNet>() << std::endl;
}
return 0;
}