-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
287 lines (251 loc) · 7.63 KB
/
main.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
276
277
278
279
280
281
282
283
284
285
286
287
#include <iostream>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <chrono>
#include <thread>
#include <vector>
#include <future>
#define DEBUG_EPOCHS false
#define DEBUG_CANDIDATES false
#define DEBUG_LAST_IMPROV true
#define DEBUG_ITERATIONS true
using namespace std;
using namespace std::chrono;
class Data
{
public:
double phi;
double p;
};
double func(double point[2]){
double phi = point[0];
double p = point[1];
double result = 35000 * sin(3 * phi) * sin(2 * p)
+ 9700 * cos(10 * phi) * cos(20 * p)
- 800 * sin(25 * phi + 0.03 * M_PI)
+ 550 * cos(p + 0.2 * M_PI);
return result;
}
double candidate(int c, int acceleration){
switch(c){
case 1:
return -acceleration;
case 2:
return -1.0 / acceleration;
case 3:
return 0;
case 4:
return 1 / acceleration;
case 5:
return acceleration;
default:
break;
}
}
void printStatus(double currentPoint[2], double stepSize[2]){
printf("Current Point : (%.2f, %.2f). Step size : (%.2f, %.2f). \n",
currentPoint[0], currentPoint[1], stepSize[0], stepSize[1]);
}
double* hillClimb(double *finalScore, int coordX, int coordY){
// number of iterations with no moves before we conclude that we converged
int burnoutEpochs = 100;
double stepSize[2] = {0.5, 0.5};
double acceleration = 2; // same acceleration for both dimensions ???
double epsilon = 0.1;
// define initial starting point
double *currentPoint = new (nothrow) double[2];
if(!currentPoint){
return NULL;
}
currentPoint[0] = coordX;
currentPoint[1] = coordY;
int epoch = 1;
int iterations = 0;
double lastImprovement = 0;
while(true){
// compute initial score
double before = func(currentPoint);
for(int i = 0; i < 2; ++i){
int best = -1; //best candidate
double bestScore = - numeric_limits<double>::infinity(); // minus INF
double currentValue = currentPoint[i];
for(int j = 1; j <= 5; ++j){
currentPoint[i] = currentPoint[i] + stepSize[i] * candidate(j, acceleration);
double temp = func(currentPoint);
if(DEBUG_CANDIDATES){
printf("Dimension %d - candidate %d\n",i, j);
printStatus(currentPoint, stepSize);
printf("Value : %.2f\n",temp );
printf("\n");
}
currentPoint[i] = currentValue; //revert changes
if(temp > bestScore){
bestScore = temp;
best = j;
}
}
if(DEBUG_CANDIDATES){
printf("Dimension %d - best candidate %d\n",i, best);
}
if(candidate(best, acceleration) == 0){
stepSize[i] = stepSize[i] / acceleration;
}
else{
currentPoint[i] = currentPoint[i] + stepSize[i] * candidate(best, acceleration);
stepSize[i] = stepSize[i] * candidate(best, acceleration); // accelerate
}
}
double now = func(currentPoint);
double improvement = now - before;
++iterations;
if(DEBUG_EPOCHS){
printf("Epoch %d !!!!!!!!!!!!!!!!!!!!\n",epoch);
printf("Now : %.2f - improvement : %.2f \n", now, improvement );
printStatus(currentPoint, stepSize);
}
if(improvement < epsilon){
if(epoch < burnoutEpochs){
++epoch;
}
else{
*finalScore = now;
return currentPoint;
}
}
else {
epoch = 0;
lastImprovement = improvement;
}
}
}
double* hill_climbing_point(promise<Data> && p, double *finalScore, int coordX, int coordY){
// number of iterations with no moves before we conclude that we converged
int burnoutEpochs = 100;
double stepSize[2] = {0.5, 0.5};
double acceleration = 2; // same acceleration for both dimensions ???
double epsilon = 0.1;
// define initial starting point
double *currentPoint = new (nothrow) double[2];
if(!currentPoint){
return NULL;
}
currentPoint[0] = coordX;
currentPoint[1] = coordY;
int epoch = 1;
int iterations = 0;
double lastImprovement = 0;
while(true){
// compute initial score
double before = func(currentPoint);
for(int i = 0; i < 2; ++i){
int best = -1; //best candidate
double bestScore = - numeric_limits<double>::infinity(); // minus INF
double currentValue = currentPoint[i];
for(int j = 1; j <= 5; ++j){
currentPoint[i] = currentPoint[i] + stepSize[i] * candidate(j, acceleration);
double temp = func(currentPoint);
if(DEBUG_CANDIDATES){
printf("Dimension %d - candidate %d\n",i, j);
printStatus(currentPoint, stepSize);
printf("Value : %.2f\n",temp );
printf("\n");
}
currentPoint[i] = currentValue; //revert changes
if(temp > bestScore){
bestScore = temp;
best = j;
}
}
if(DEBUG_CANDIDATES){
printf("Dimension %d - best candidate %d\n",i, best);
}
if(candidate(best, acceleration) == 0){
stepSize[i] = stepSize[i] / acceleration;
}
else{
currentPoint[i] = currentPoint[i] + stepSize[i] * candidate(best, acceleration);
stepSize[i] = stepSize[i] * candidate(best, acceleration); // accelerate
}
}
double now = func(currentPoint);
double improvement = now - before;
++iterations;
if(DEBUG_EPOCHS){
printf("Epoch %d !!!!!!!!!!!!!!!!!!!!\n",epoch);
printf("Now : %.2f - improvement : %.2f \n", now, improvement );
printStatus(currentPoint, stepSize);
}
if(improvement < epsilon){
if(epoch < burnoutEpochs){
++epoch;
}
else{
*finalScore = now;
Data data;
data.phi = currentPoint[0];
data.p = currentPoint[1];
p.set_value(data);
return currentPoint;
}
}
else {
epoch = 0;
lastImprovement = improvement;
}
}
}
void sequential_climbing() {
cout << "\nSequential climbing:" << endl;
double score;
double bestClimber[2], bestScore;
for(int i = 0; i < 100; i++) {
double *climber = hillClimb(&score, rand() % 100, rand() % 100);
if(score > bestScore) {
bestScore = score;
bestClimber[0] = climber[0];
bestClimber[1] = climber[1];
}
}
printf("Finest climber at %.2f meters : (%.2f, %.2f)\n", bestScore, bestClimber[0], bestClimber[1]);
}
void parallel_climbing() {
cout << "\nParallel climbing:" << endl;
std::vector<std::thread> threads;
promise<Data> promises[100];
std::vector<future<Data>> futures;
double score;
double best, best_climber[2], coord[2];
for(int i = 0; i < 100; i++) {
futures.push_back(promises[i].get_future());
threads.push_back(std::thread(hill_climbing_point, std::move(promises[i]),&score, rand() % 100, rand() % 100));
}
for(auto &t : threads){
t.join();
}
for(auto &future : futures) {
Data data = future.get();
coord[0] = data.phi;
coord[1] = data.p;
if(func(coord) > best) {
best = func(coord);
best_climber[0] = data.phi;
best_climber[1] = data.p;
}
}
printf("Finest climber at %.2f meters : (%.2f, %.2f)\n", func(best_climber), best_climber[0], best_climber[1]);
}
int main(int argc, char const *argv[])
{
std::srand (std::time (0));
high_resolution_clock::time_point t3 = high_resolution_clock::now();
sequential_climbing();
high_resolution_clock::time_point t4 = high_resolution_clock::now();
high_resolution_clock::time_point t1 = high_resolution_clock::now();
parallel_climbing();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
cout << "\nExecution sequential climbing : " << duration_cast<milliseconds>( t4 - t3 ).count() << " milliseconds.";
cout << "\nExecution parallel climbing : " << duration_cast<milliseconds>( t2 - t1 ).count() << " milliseconds." << endl;
return 0;
}