-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSphere.cpp
More file actions
356 lines (341 loc) · 8.35 KB
/
Copy pathNSphere.cpp
File metadata and controls
356 lines (341 loc) · 8.35 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "NSphere.h"
NSphere::NSphere(unsigned dimensionality)
{
this->dimensionality = dimensionality;
this->numPoints = 0;
this->points = new float*[MAX_POINTS];
#pragma omp parallel for schedule(static,256) num_threads(4)
for (unsigned i = 0; i < this->MAX_POINTS; i++) {
this->points[i] = new float[dimensionality];
}
this->weights = new float[MAX_WEIGHTS];
#pragma omp parallel for schedule(static,256) num_threads(4)
for (unsigned i = 0; i < MAX_WEIGHTS; i++) {
this->weights[i] = this->getDefaultWeight();
}
}
float NSphere::computeDistance(unsigned i, unsigned j)
{
return this->computeDistance(this->points[i], this->points[j]);
}
float NSphere::computeDistance(float* p, float* q)
{
float distance = this->computeSquaredDistance(p, q);
distance = sqrt(distance);
return distance;
}
float NSphere::computeSquaredDistance(unsigned i, unsigned j)
{
return this->computeSquaredDistance(this->points[i], this->points[j]);
}
float NSphere::computeSquaredDistance(float* p, float* q)
{
float distance = 0.0;
for (int i = 0; i < this->dimensionality; i++) {
float distanceI = p[i] - q[i];
distance += distanceI * distanceI;
}
return distance;
}
float NSphere::computeEnergy()
{
float energy = 0.0;
for (unsigned i = 0; i < this->numPoints; i++) {
for (unsigned j = i + 1; j < this->numPoints; j++) {
energy += this->computeEnergyInPoints(i, j);
}
}
return energy;
}
float NSphere::computeEnergyDueToPoint(unsigned i)
{
float energy = 0.0;
#pragma omp parallel for reduction(+:energy) schedule(static,256) num_threads(4)
for (unsigned i_ = 0; i_ < this->numPoints; i_++) {
energy += this->computeEnergyInPoints(i, i_);
}
return energy;
}
float NSphere::computeEnergyInPoints(unsigned i, unsigned j)
{
float distance = this->computeDistance(i, j);
if (distance <= std::numeric_limits<float>::epsilon()) {
return 0.0;
}
return - this->getWeight(i, j) / distance;
}
float NSphere::computeLength(unsigned i)
{
return this->computeLength(this->points[i]);
}
float NSphere::computeLength(float* p)
{
float length = 0.0;
for (int i = 0; i < this->dimensionality; i++) {
length += p[i] * p[i];
}
length = sqrt(length);
return length;
}
float NSphere::getWeight(unsigned i, unsigned j)
{
unsigned index;
this->weightIndexesToWeightArrayIndex(index, i, j);
return this->weights[index];
}
float NSphere::randUniform(float range)
{
int rand = std::rand() - RAND_MAX / 2; // interval [-RAND_MAX/2, RAND_MAX/2]
float rand_ = rand / (RAND_MAX / 2.0); // interval [-1, 1]
return rand_ * range;
}
float NSphere::getDefaultWeight()
{
return sqrt(2.0);
}
float* NSphere::getPoint(unsigned i)
{
return this->points[i];
}
float* NSphere::getPointClone(unsigned i)
{
float* point = new float[this->dimensionality];
this->copyPoint(this->points[i], point);
return point;
}
float* NSphere::makeRandomUnitPoint()
{
float* point = new float[this->dimensionality];
for (int i = 0; i < this->dimensionality; i++) {
point[i] = this->randUniform(1.0);
}
this->normalisePoint(point);
return point;
}
std::string NSphere::pointToString(float* p)
{
if (this->dimensionality == 0) {
return "[]";
}
typedef std::numeric_limits<float> fl;
std::ostringstream ss;
ss.precision(fl::digits10);
ss << "[ ";
for (int i = 0; i < this->dimensionality - 1; i++) {
ss << std::fixed << p[i];
ss << ",";
}
ss << std::fixed << p[this->dimensionality - 1];
ss << "]";
return ss.str();
}
std::string NSphere::pointToString(unsigned i)
{
return this->pointToString(this->points[i]);
}
std::string NSphere::pointsToString()
{
std::ostringstream ss;
for (unsigned i = 0; i < this->numPoints; i++) {
ss << this->pointToString(i) << std::endl;
}
return ss.str();
}
std::string NSphere::pointsToPyDict()
{
if (this->numPoints == 0) {
return "";
}
std::ostringstream ss;
ss << "{" << std::endl;
for (unsigned i = 0; i < this->numPoints - 1; i++) {
ss << "\t";
ss << "\"" << std::to_string(i) << "\": ";
ss << this->pointToString(i);
ss << "," << std::endl;
}
ss << "\t";
ss << "\"" << std::to_string(this->numPoints - 1) << "\": ";
ss << this->pointToString(this->numPoints - 1);
ss << std::endl;
ss << "}";
return ss.str();
}
unsigned NSphere::computeNearestPoint(unsigned i)
{
return this->computeNearestPoint(this->points[i]);
}
unsigned NSphere::computeNearestPoint(float* p)
{
float minDistance = std::numeric_limits<float>::max();
unsigned minPoint;
for (unsigned i = 0; i < this->numPoints; i++) {
if (p == this->points[i]) {
continue;
}
float thisDistance = this->computeDistance(p, this->points[i]);
if (thisDistance <= minDistance) {
minDistance = thisDistance;
minPoint = i;
}
}
return minPoint;
}
/*
unsigned* NSphere::computeNearestPoints(unsigned i, unsigned n)
{
}
*/
unsigned NSphere::computeFurthestPoint(unsigned i)
{
return this->computeFurthestPoint(this->points[i]);
}
unsigned NSphere::computeFurthestPoint(float* p)
{
float maxDistance = std::numeric_limits<float>::min();
unsigned maxPoint;
for (unsigned i = 0; i < this->numPoints; i++) {
float thisDistance = this->computeDistance(p, this->points[i]);
if (thisDistance > maxDistance) {
maxDistance = thisDistance;
maxPoint = i;
}
}
return maxPoint;
}
unsigned NSphere::getNumPoints()
{
return this->numPoints;
}
unsigned NSphere::getDimensionality()
{
return this->dimensionality;
}
unsigned NSphere::getMaxPoints()
{
return this->MAX_POINTS;
}
unsigned NSphere::addPoint(float* p)
{
this->setPoint(p, this->numPoints);
return this->numPoints++;
}
unsigned NSphere::addRandomUnitPoint()
{
float* point = this->makeRandomUnitPoint();
unsigned index = this->addPoint(point);
delete point;
return index;
}
void NSphere::addRandomUnitPoints(unsigned i)
{
for (unsigned i_ = 0; i_ < i; i_++) {
this->addRandomUnitPoint();
}
}
void NSphere::computeNearestPoints(unsigned &i, unsigned &j)
{
float minDistance = std::numeric_limits<float>::max();
for (unsigned i_ = 0; i_ < this->numPoints; i_++) {
unsigned j_ = this->computeNearestPoint(i_);
float thisDistance = this->computeDistance(i_, j_);
if (thisDistance < minDistance) {
minDistance = thisDistance;
i = i_;
j = j_;
}
}
}
void NSphere::computeFurthestPoints(unsigned &i, unsigned &j)
{
float maxDistance = std::numeric_limits<float>::min();
for (unsigned i_ = 0; i_ < this->numPoints; i_++) {
unsigned j_ = this->computeFurthestPoint(i_);
float thisDistance = this->computeDistance(i_, j_);
if (thisDistance > maxDistance) {
maxDistance = thisDistance;
i = i_;
j = j_;
}
}
}
void NSphere::copyPoint(float* source, float* dest)
{
std::memcpy(dest, source, this->dimensionality * sizeof(float));
}
void NSphere::normalisePoint(unsigned i)
{
this->normalisePoint(this->points[i]);
}
void NSphere::normalisePoint(float* p)
{
float length = this->computeLength(p);
this->scalarMultiplication(p, 1.0/length);
}
void NSphere::perturbPoint(float* p, float d)
{
for (int i = 0; i < this->dimensionality; i++) {
p[i] += ((rand() % 3) - 1) * d;
}
}
void NSphere::scalarMultiplication(float* p, float scalar)
{
for (int i = 0; i < this->dimensionality; i++) {
p[i] *= scalar;
}
}
void NSphere::setWeight(unsigned i, unsigned j, float weight)
{
unsigned index;
this->weightIndexesToWeightArrayIndex(index, i, j);
this->weights[index] = weight;
}
void NSphere::solve()
{
float totalEnergyRise = 0.0;
for (int i = 0; i < 10; i++) {
for (int j = 10; j >= 0; j--) {
float energyRise = this->solveStep(2.0 * pow(j+3.5, -3.5));
//std::cout << energyRise << std::endl;
totalEnergyRise += energyRise;
}
//std::cout << std::endl;
}
std::cout << totalEnergyRise << " *" << std::endl;
}
float NSphere::solveStep(float inc)
{
float totalEnergyRise = 0.0;
for (unsigned i = 0; i < this->numPoints; i++) {
float energyRise = 0.0;
float* savedPoint = this->getPointClone(i);
energyRise -= this->computeEnergyDueToPoint(i);
this->perturbPoint(this->points[i], inc);
this->normalisePoint(this->points[i]);
energyRise += this->computeEnergyDueToPoint(i);
if (energyRise >= 0) {
// Keep change
totalEnergyRise += energyRise;
} else {
// Undo change
this->setPoint(savedPoint, i);
}
delete savedPoint;
}
return totalEnergyRise;
}
void NSphere::setPoint(float* p, unsigned i)
{
this->copyPoint(p, this->points[i]);
}
void NSphere::weightIndexesToWeightArrayIndex(
unsigned &index
, unsigned i
, unsigned j)
{
if (i <= j) {
index = j * (j - 1) / 2 + i;
} else {
index = i * (i - 1) / 2 + j;
}
}