-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConstructs.cpp
More file actions
164 lines (130 loc) · 4.9 KB
/
Copy pathConstructs.cpp
File metadata and controls
164 lines (130 loc) · 4.9 KB
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
#include "Constructs.h"
int randRange(int min, int max) {
int ran = min + (rand() % (int)(max - min + 1));
// std::cout << "min is: " << min << " max is: " << max << " random is: " << ran << endl;
return ran;
}
typedef boost::minstd_rand base_generator_type;
double randRange(double min, double max) {
base_generator_type generator(42u);
srand(rand());
generator.seed(static_cast<unsigned int>(rand()));
// Define a uniform random number distribution which produces "double"
// values between 0 and 1 (0 inclusive, 1 exclusive).
boost::uniform_real<> uni_dist(0,1);
boost::variate_generator<base_generator_type&, boost::uniform_real<> > uni(generator, uni_dist);
double ran = min + (uni()*(max-min));
// std::cout << "min is: " << min << " max is: " << max << " random is: " << ran << " uni is: " << uni() << endl;
return ran;
}
unsigned int randRange(unsigned int min, unsigned int max) {
unsigned int ran = min + (rand() % (int)(max - min + 1));
// std::cout << "min is: " << min << " max is: " << max << " random is: " << ran << endl;
return ran;
}
std::string string_format(const std::string fmt_str, ...) {
int final_n, n = ((int)fmt_str.size()) * 2; /* Reserve two times as much as the length of the fmt_str */
std::string str;
std::unique_ptr<char[]> formatted;
va_list ap;
while(1) {
formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */
strcpy(&formatted[0], fmt_str.c_str());
va_start(ap, fmt_str);
final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap);
va_end(ap);
if (final_n < 0 || final_n >= n)
n += abs(final_n - n + 1);
else
break;
}
return std::string(formatted.get());
}
//this assumes an std deviation as percentage of mean
//i.e. deviation = 50 means that std's will deviate from mean
//as 50%
double generateGaussian(double mean, double deviation) {
double std_deviation = abs((deviation/100.0)*mean) ;
boost::mt19937 *rng = new boost::mt19937();
srand(rand());
rng->seed(rand());
boost::normal_distribution<> distribution(mean, std_deviation);
boost::variate_generator< boost::mt19937, boost::normal_distribution<> > dist(*rng, distribution);
delete rng;
return dist();
}
GeneralLayer::GeneralLayer(unsigned int minIndex, int x, int y, int z, unsigned int w, unsigned int h, unsigned int d)
: _nNeurons(w*h*d),
_minIndex(minIndex),
_maxIndex(minIndex+_nNeurons),
_x(x), _y(y), _z(z),
_w(w), _h(h), _d(d) {
}
void GeneralLayer::clear() {
_stimulus_input.clear();
_forced_to_fire.clear();
}
Eigen::VectorXd GeneralLayer::makeVector(std::vector<unsigned> firings) {
VectorXd neuronFirings = VectorXd::Zero(_nNeurons);
for( unsigned i = 0; i < firings.size(); i++ ) {
if( checkNeuron(firings[i]) ) {
neuronFirings(firings[i]-_minIndex) = 1.0;
}
}
return neuronFirings;
}
Eigen::MatrixXd GeneralLayer::makeMatrix(std::vector< std::vector<unsigned> > allFirings) {
Eigen::MatrixXd ret(_nNeurons, allFirings.size());
for( unsigned i = 0; i < allFirings.size(); i++ ) {
Eigen::VectorXd thisFirings = makeVector( allFirings[i] );
ret.col(i) = thisFirings;
}
return ret;
}
bool GeneralLayer::checkNeuron(unsigned int neuronIndex) {
return (neuronIndex >= _minIndex) && (neuronIndex < _maxIndex);
}
unsigned int GeneralLayer::getNeuronIndexInLSM(unsigned int neuronIndex) {
return neuronIndex + _minIndex;
}
Eigen::Vector3i GeneralLayer::getNeuronPosition(unsigned int neuronIndex) {
unsigned int nPosZ = ceil(neuronIndex / (_w * _h));
unsigned int posInLayer = neuronIndex % (_w * _h);
unsigned int nPosY = ceil(posInLayer/_w);
unsigned int nPosX = posInLayer % _w;
Eigen::Vector3i position;
position << nPosX, nPosY, nPosZ;
return position;
}
void TicToc::tic() {
tictoc_stack.push(clock());
}
void TicToc::toc(bool bOut) {
if( bOut ) {
std::cout << "Time elapsed: "
<< ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC
<< std::endl;
}
tictoc_stack.pop();
}
PoissonLayer::PoissonLayer(unsigned int minIndex,
int x, int y, int z,
unsigned int w, unsigned int h, unsigned int d)
: GeneralLayer(minIndex, x, y, z, w, h, d) {
}
void PoissonLayer::inputNeuron(LSM *net, unsigned int neuronIndex, float input) {
// _stimulus_input.push_back(std::make_pair<unsigned, float>(_minIndex+neuronIndex, input));
// float locInput[1];
// locInput[0] = input;
// net->setNeuron(_minIndex+neuronIndex, 1, locInput);
net->_sim->_sim->setNeuron(_minIndex+neuronIndex, 1, &input);
// net->setNeuronParameter(_minIndex+neuronIndex, 0, input);
}
InputLayer::InputLayer(unsigned int minIndex,
int x, int y, int z,
unsigned int w, unsigned int h, unsigned int d)
: GeneralLayer(minIndex, x, y, z, w, h, d) {
}
void InputLayer::fireNeuron(unsigned int neuronIndex) {
_forced_to_fire.push_back(neuronIndex);
}