-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHCWeight.cpp
104 lines (83 loc) · 1.91 KB
/
SHCWeight.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
#ifndef SHC_WEIGHT
#define SHC_WEIGHT
#include "SHCWeight.hpp"
#include "GEPNN.hpp"
#include <iostream>
#include <fstream>
SHCWeight::SHCWeight( double mean, double stddev, double p_mean_move = 0.05, double p_std_dev_reduce = 0.997241 )
{
mean_move = p_mean_move;
std_dev_reduce = p_std_dev_reduce;
this->mean = mean;
this->stddev = stddev;
num_best_weights = 0;
for( int i = 0; i < MAX_BEST_WEIGHTS; i++ )
{
best_weights[i].fitness = 0;
best_weights[i].weight = -29.7;
}
}
SHCWeight::~SHCWeight()
{
}
double SHCWeight::getWeight( RandomNumberGenerator& rng )
{
return rng.getNormal( mean, stddev );
}
double SHCWeight::getMean()
{
return mean;
}
double SHCWeight::getStddev()
{
return stddev;
}
void SHCWeight::updateMeanAndStdDev()
{
double middle = 0.;
for( int i = 0; i < num_best_weights; i++ )
middle += best_weights[i].weight;
middle /= num_best_weights;
mean += mean_move * ( middle - mean );
stddev *= std_dev_reduce;
}
void SHCWeight::setBestWeight( double w, int f )
{
int index = -1;
// IF BEST_WEIGHTS NOT FULL ADD THE FITNESS
if( num_best_weights < MAX_BEST_WEIGHTS )
{
index = num_best_weights;
num_best_weights++;
}
else
{
// IF BEST_WEIGHTS IS FULL, FIND THE ONE WITH THE BIGGEST FITNESS (worst fitness)
int biggest = 0;
for( int i = 0; i < MAX_BEST_WEIGHTS; i++ )
{
if( best_weights[i].fitness < best_weights[biggest].fitness )
biggest = i;
}
if( f > best_weights[biggest].fitness )
{
index = biggest;
}
}
if( index >= 0 )
{
best_weights[index].fitness = f;
best_weights[index].weight = w;
}
updateMeanAndStdDev();
}
void SHCWeight::print( std::ostream &fout )
{
using namespace std;
fout << " { ";
fout.precision(2);
for( int i = 0; i < num_best_weights; i++ )
fout << "{" << fixed << best_weights[i].weight << ", " << best_weights[i].fitness << "}, ";
fout << "mean= " << fixed << mean << " std_dev= " << fixed << stddev << " }";
}
#endif