-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNet.cpp
275 lines (243 loc) · 6.01 KB
/
NeuralNet.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "NeuralNet.h"
#include <cmath>
#include <fstream>
#include <iostream>
namespace
{
const double MUTATION_RATE = 0.12;
const double MAX_PERTURBATION = 0.3;
double
get_random_number( )
{
double res = rand( );
res /= RAND_MAX;
return res;
}
double
get_random_weight( )
{
double r = get_random_number( );
double res = 2 * r - 1;
return res;
}
double
tan_h( double x )
{
if ( x > +20.0 )
{
return +1.0;
}
else if ( x < -20.0 )
{
return -1.0;
}
else
{
return std::tanh( x );
}
}
double
sigmoid( double x )
{
if ( x > +20.0 )
{
return +1.0;
}
else if ( x < -20.0 )
{
return 0.0;
}
else
{
double res = 1 / ( 1 + std::exp( -x ) );
return res;
}
}
bool
are_equal_with_epsilon( double x, double y )
{
const double eps = 1e-15;
return fabs( x - y ) < eps;
}
}
NeuralNet::NeuralNet( )
{
}
NeuralNet::NeuralNet( int32_t num_inputs, int32_t num_hidden )
: m_num_inputs( num_inputs )
, m_num_hidden( num_hidden ? num_hidden : ( num_inputs / 2 + 1 ) )
, m_fitness( 0.0 )
{
int32_t weights_count = ( m_num_inputs + 1 ) * m_num_hidden + m_num_hidden + 1;
for ( int32_t i = 0; i < weights_count; ++i )
{
m_weights.push_back( get_random_weight( ) );
}
}
int32_t
NeuralNet::get_num_inputs( ) const
{
return m_num_inputs;
}
int32_t
NeuralNet::get_num_hidden( ) const
{
return m_num_hidden;
}
double
NeuralNet::get_fitness( ) const
{
return m_fitness;
}
void
NeuralNet::set_fitness( double val )
{
m_fitness = val;
}
double
NeuralNet::activate( const std::vector< double >& input ) const
{
auto weight_iterator = m_weights.cbegin( );
std::vector< double > hidden_layer( m_num_hidden, 0.0 );
for ( auto& hidden_node : hidden_layer )
{
for ( auto i : input )
{
hidden_node += i * ( *weight_iterator++ );
}
hidden_node += *weight_iterator++;
hidden_node = tan_h( hidden_node );
}
auto x = 0.0;
for ( auto& hidden_node : hidden_layer )
{
x += hidden_node * ( *weight_iterator++ );
}
x += *weight_iterator++;
return tan_h( x );
}
void
NeuralNet::mutate( )
{
for ( auto& weight : m_weights )
{
if ( get_random_number( ) < MUTATION_RATE )
{
weight += get_random_weight( ) * MAX_PERTURBATION;
}
}
}
bool
NeuralNet::save( const std::string& file_name ) const
{
// std::cerr << "saving neural net to " << file_name << std::endl;
std::ofstream out( file_name.c_str( ), std::ios::out | std::ios::binary );
if ( !out )
{
std::cerr << "Can't open file " << file_name << std::endl;
return false;
}
else
{
out.write( (char*)&m_num_inputs, sizeof( int32_t ) );
out.write( (char*)&m_num_hidden, sizeof( int32_t ) );
for ( auto& weight : m_weights )
{
out.write( (char*)&weight, sizeof( double ) );
}
out.write( (char*)&m_fitness, sizeof( double ) );
out.close( );
return true;
}
}
NeuralNet
NeuralNet::load( const std::string& file_name )
{
// std::cerr << "loading neural net from " << file_name << std::endl;
NeuralNet neural_net;
std::ifstream in( file_name.c_str( ), std::ios::in | std::ios::binary );
if ( !in )
{
std::cerr << "Can't open file %s\n" << file_name << std::endl;
return false;
}
else
{
in.read( (char*)&neural_net.m_num_inputs, sizeof( int32_t ) );
in.read( (char*)&neural_net.m_num_hidden, sizeof( int32_t ) );
int32_t total_weights = ( neural_net.m_num_inputs + 1 ) * neural_net.m_num_hidden
+ neural_net.m_num_hidden + 1;
neural_net.m_weights = std::vector< double >( total_weights );
for ( auto& weight : neural_net.m_weights )
{
in.read( (char*)&weight, sizeof( double ) );
}
in.read( (char*)&neural_net.m_fitness, sizeof( double ) );
in.close( );
return neural_net;
}
}
NeuralNet operator*( const NeuralNet& lhs, const NeuralNet& rhs )
{
NeuralNet baby;
baby.m_num_inputs = lhs.m_num_inputs;
baby.m_num_hidden = lhs.m_num_hidden;
int32_t total_weights = lhs.m_weights.size( );
auto ratio = 0.5;
const auto lhs_fitness = lhs.get_fitness( );
const auto rhs_fitness = rhs.get_fitness( );
const auto sum_fitness = lhs_fitness + rhs_fitness;
if ( !are_equal_with_epsilon( sum_fitness, 0.0 ) )
{
ratio = lhs_fitness / sum_fitness;
}
for ( int32_t i = 0; i < total_weights; ++i )
{
double r = get_random_number( );
if ( r < ratio )
{
baby.m_weights.push_back( lhs.m_weights[ i ] );
}
else
{
baby.m_weights.push_back( rhs.m_weights[ i ] );
}
}
return baby;
}
bool
operator==( const NeuralNet& lhs, const NeuralNet& rhs )
{
if ( lhs.m_num_inputs != rhs.m_num_inputs )
{
return false;
}
if ( lhs.m_num_hidden != rhs.m_num_hidden )
{
return false;
}
int32_t total_weights = (int32_t)lhs.m_weights.size( );
if ( total_weights != ( (int32_t)rhs.m_weights.size( ) ) )
{
return false;
}
for ( int32_t i = 0; i < total_weights; ++i )
{
if ( !are_equal_with_epsilon( lhs.m_weights[ i ], rhs.m_weights[ i ] ) )
{
return false;
}
}
return true;
}
std::ostream&
operator<<( std::ostream& out, const NeuralNet& neural_net )
{
out << neural_net.m_num_inputs << " " << neural_net.m_num_hidden << std::endl;
for ( const auto& weight : neural_net.m_weights )
{
out << weight << " ";
}
out << std::endl;
return out;
}