forked from Conedy/Conedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatialNetwork.h
224 lines (167 loc) · 5.12 KB
/
spatialNetwork.h
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
#ifndef spatialNetwork_h
#define spatialNetwork_h spatialNetwork_h
#include "dynNetwork.h"
namespace conedy
{
//! Basisklasse für verschiedene Funktionen, die Neuronenpositionen zurückgeben.
class determineNodePositions
{
public:
determineNodePositions() {};
virtual vector <double> getNodePosition() = 0;
};
//! Funktionsklasse die 2-dimensional gleichverteilte Vektoren zurückgibt.
class randomPositions : public determineNodePositions
{
public:
randomPositions() {};
virtual vector <double> getNodePosition()
{
vector <double> res(3);
res[0] = gslNoise::getUniform(-0.5,0.5);
res[1] = gslNoise::getUniform(-0.5,0.5);
res[2] = 0.0;
return res;
}
};
//! Funktionsklasse, die (reihenweise) Vectoren auf einem Gitter zurückgibt
class squareLatticePositions : public determineNodePositions
{
private:
unsigned int sizex, sizey;
unsigned int countx, county;
public:
squareLatticePositions(unsigned int sx, unsigned int sy) : sizex(sx), sizey(sy), countx(0), county(0) {};
virtual vector <double> getNodePosition()
{
unsigned int size;
if (sizex > sizey)
size = sizex;
else
size = sizey;
vector <double> res(3);
res[0] = countx / ((double) size) - 0.5;
res[1] = county / ((double) size) - 0.5;
res[2] = 0.0;
countx++;
if (countx == sizex)
{
county++;
countx = 0;
if (county == sizey)
{
county = 0;
}
}
return res;
}
};
//! Netzerk, das zusätzlich zu den Verbindungen 3-dimensionale Koordinaten für jeden Knoten speichert. Wird unter anderem für neuro3d verwendet.
class spatialNetwork : public dynNetwork
{
private:
determineNodePositions *nodePositioner;
public:
double rectSize;
typedef vector<double> pos;
typedef vector <pos> nodePositions;
nodePositions nodePos;
spatialNetwork() : nodePositioner(NULL) {};
void connectCloseNodes( node * source, node *target, baseType dist, edgeBlueprint * l);
void connectCloseNodesTorus( node * source, node *target, baseType dist, edgeBlueprint * l);
baseType torifyDistance(baseType d)
{
if (abs(d) < abs(1.0-abs(d)))
return d;
else
return abs(1.0 - abs(d));
}
baseType euclidianDistanceTorus(nodeDescriptor node1, nodeDescriptor node2)
{
pos diff(3);
diff[0] = torifyDistance((nodePos[node1][0] - nodePos[node2][0]));
diff[1] = torifyDistance((nodePos[node1][1] - nodePos[node2][1]));
diff[2] = torifyDistance((nodePos[node1][2] - nodePos[node2][2]));
return diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
}
baseType euclidianDistance(nodeDescriptor node1, nodeDescriptor node2)
{
pos diff(3);
diff[0] = (nodePos[node1][0] - nodePos[node2][0]);
diff[1] = (nodePos[node1][1] - nodePos[node2][1]);
diff[2] = (nodePos[node1][2] - nodePos[node2][2]);
return diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
}
void useRandomPositioning()
{
if (nodePositioner == NULL)
nodePositioner = new randomPositions ();
rectSize = 0.99 / 100;
}
void useLatticePositioning(unsigned int sizex, unsigned int sizey)
{
if (nodePositioner == NULL)
nodePositioner = new squareLatticePositions(sizex,sizey);
if (sizex > sizey)
rectSize = 0.99 / sizex;
else
rectSize = 0.99 / sizey;
}
// virtual nodeDescriptor addNode();
//! fügt eine Kopie von *n zum Netzwerk hinzu, gibt die Nummer vom neuen Knoten zurück.
virtual nodeDescriptor addNode ( nodeBlueprint *n=stdNode);
/* void randomize ( vector<pos> &theNodePos, double sigma )
{
uniform<double> rn ( -sigma,sigma );
pos newPos ( 3 );
for ( unsigned int i = 0; i < theNodePos.size(); i++ )
{
// cout << "Vorher:" << theNodePos[i][0] << endl;
newPos[0] = rn();
newPos[1] =rn();
newPos[2] = rn();
// cout << newPos[0] << ":" << endl;
// theNodePos[i]= theNodePos[i] + newPos;
// cout << "Nachher:" << theNodePos[i][0] << endl;
}
}
void circle ( vector <pos> &theNodePos )
{
unsigned int N = nodePos.size();
for ( unsigned int i=0;i<N;i++ )
{
nodePos[i][0]=cos ( ( 2.0* ( double ) i*M_PI/ ( double ) N ) );
nodePos[i][1]=sin ( ( 2.0* ( double ) i*M_PI/ ( double ) N ) );
nodePos[i][2]=0.0;
}
}
void squareLattice ( vector <pos> &theNodePos )
{
unsigned int N = ( unsigned int ) sqrt ( nodePos.size() );
// cout << "Groesse:" << N << endl;
for ( unsigned int i = 0; i < N ; i++ )
for ( unsigned int j = 0; j < N ; j++ )
{
theNodePos[i+N*j][0] = i/ ( ( double ) N ) - 0.5;
theNodePos[i+N*j][1] = j/ ( ( double ) N ) - 0.5;
theNodePos[i+N*j][2] = 0;
}
rectSize = 0.99/N;
cout << "Lattice Fertig " << endl;
}
void randomPos ( vector <pos> &theNodePos )
{
uniform<double> rn ( -0.5,0.5 );
for ( unsigned int i = 0; i < network::numberVertices(); i++ )
{
nodePos[i][0] = rn();
nodePos[i][1] = rn();
nodePos[i][2] = rn();
}
rectSize = 0.99/network::numberVertices();
cout << "Lattice Fertig " << endl;
}
*/
};
};
#endif