-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarmer.cpp
400 lines (361 loc) · 12.4 KB
/
swarmer.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//#include "objective.hpp"
#include "runnables.hpp"
#include <algorithm>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <memory>
#include <mutex>
#include <numeric>
#include <random>
#include <vector>
#include <system_error>
particle<5,double,5>
template <typename T = double, unsigned D = 2>
class particle : protected std::vector<T>
{
using super = std::vector<T>;
public:
using super::cbegin;
using super::cend;
using super::const_iterator;
using super::begin;
using super::end;
using super::iterator;
using super::size;
using solution = std::pair<value_type,super>;
using dimension = D;
//using super::vector; // this should replace the constructors below
particle() : super() {}
particle(std::tuple<D> lb, std::tuple<D> ub)
: super(n), velocity(n,0)
{
std::generate(begin(), end(), [this](){
return std::generate_canonical<double,16>(this->rng);
});
}
particle(std::initializer_list<double> x) : super(x) {}
friend bool operator< ( solution a, solution b );
friend bool operator< ( double a, solution b );
friend bool operator< ( solution a, double b );
private:
super velocity;
};
bool operator< ( particle::solution a, particle::solution b ) { return a.first < b.first; }
bool operator< ( double a, particle::solution b ) { return a < b.first; }
bool operator< ( particle::solution a, double b ) { return a.first < b; }
class swarmer
: public runnable, particle, public std::enable_shared_from_this<swarmer>
{
public:
using swarm = std::forward_list<std::shared_ptr<swarmer>>;
using cost_function = std::function<double(param,param)>;
static long long update_count;
explicit swarmer ( int n = 1, cost_function f =
std::bind(std::accumulate<std::vector<double>::const_iterator,double,std::plus<double>>,
std::placeholders::_1, std::placeholders::_2, 0, std::plus<double>()) )
: particle(n),
leader(std::shared_ptr<swarmer>(this)),
neighbors(new swarm(1,shared_from_this())),
objective(f),
local_best(std::numeric_limits<double>::infinity(),*this),
rng(std::random_device()())
{
std::generate(begin(), end(), [this](){
return std::generate_canonical<double,16>(this->rng);
});
}
swarmer ( swarmer & s )
: particle(s.size()),
leader(s.shared_from_this()),
objective(s.objective),
local_best(std::numeric_limits<double>::infinity(),*this),
rng(std::random_device()())
{
leader->neighbors->push_front(std::shared_ptr<swarmer>(this));
std::generate(begin(), end(), [this](){
return std::generate_canonical<double,16>(this->rng);
});
}
void start()
{
std::lock_guard<std::mutex> lock(leader_mutex);
for ( auto s : *(leader->neighbors) ) { s->run(); }
}
void watch() { for ( auto s : *(leader->neighbors) ) { s->join(); } }
solution best_solution() { return leader->local_best; }
private:
static bool const DEBUG = true;
void operator() ()
{
while (leader->local_best.first > std::numeric_limits<double>::min())
{ ++update_count; update(); }
}
void update()
{
// compute velocity
auto pcurr = cbegin();
{
std::lock_guard<std::mutex> lock(leader_mutex);
auto pbest = local_best.second.cbegin();
auto gbest = leader->local_best.second.cbegin();
std::transform(velocity.cbegin(), velocity.cend(),
velocity.begin(),
[&,this](double v){
auto prand = std::generate_canonical<double,16>(this->rng);
auto grand = std::generate_canonical<double,16>(this->rng);
return v * this->INERTIA
+ prand * this->P_AFFINITY * (*(pbest++) - *(pcurr))
+ grand * this->G_AFFINITY * (*(gbest++) - *(pcurr++));
});
}
// update position
std::transform(cbegin(), cend(), velocity.cbegin(),
begin(), std::plus<double>());
// compute cost
auto cost = objective(begin(), end());
// update personal best
if ( cost < local_best ) {
local_best.second.assign(cbegin(),cend());
local_best.first = cost;
}
// update global best
if ( local_best < leader->local_best ) { lead(); }
}
void lead()
{
std::lock_guard<std::mutex> lock(leader_mutex);
neighbors = std::move(leader->neighbors);
for ( auto s : *neighbors )
{ if (s.get() != this) { s->follow(*this); } }
follow(*this);
}
void follow( swarmer & s ) { leader = s.shared_from_this(); }
std::shared_ptr<swarmer> leader;
std::unique_ptr<swarm> neighbors;
cost_function objective;
solution local_best;
std::mt19937 rng;
static std::mutex leader_mutex;
constexpr static double const INERTIA = 0.9;
constexpr static double const P_AFFINITY = 1;
constexpr static double const G_AFFINITY = 1;
};
std::mutex swarmer::leader_mutex;
long long swarmer::update_count {0};
class objective
{
using param = particle::const_iterator;
public:
class sphere
{
public:
double operator() ( param a, param b )
{
double cost = std::inner_product(a, b, a, 0.0);
return cost;
}
};
class rosenbrock
{
public:
double operator() ( param a, param b )
{
std::vector<double> tmp;
std::adjacent_difference(a, b, std::back_inserter(tmp),
[](double x2, double x1) {
double t1 = x2 - x1 * x1;
double t2 = 1 - x1;
return 100.0 * t1 * t1 + t2 * t2;
}
);
double cost = std::accumulate(tmp.begin()+1, tmp.end(), 0.0);
return cost;
}
};
class rastrigin
{
public:
double operator() ( param a, param b )
{
double cost = std::accumulate(a, b, 0.0,
[](double sum, double x) {
static double const TWOPI = 8 * std::atan(1.0);
return sum + x * x - 10 * std::cos(TWOPI * x);
}
);
unsigned n = distance(a,b);
return 10 * n + cost;
}
};
class griewangk
{
public:
double operator() ( param a, param b )
{
double cost1 = std::accumulate(a, b, 0.0,
[](double sum, double x) { return sum + x * x / 4000.0; }
);
unsigned i = 0;
double cost2 = std::accumulate(a, b, 1.0,
[&i](double prod, double x) {
return prod * std::cos(x / std::sqrt(++i));
}
);
return cost1 - cost2 + 1;
}
};
class shaffer_f6
{
public:
double operator() ( param a, param b )
{
auto x = *(a++), y = *a;
auto h = x * x + y * y;
auto denom = 1 + 0.001 * h;
auto numer = std::sin(std::sqrt(h));
double cost = 0.5 + (numer * numer - 0.5) / (denom * denom);
return cost;
}
};
class beale
{
public:
auto operator() ( param a, param b ) const -> double
{
if (std::distance(a, b) != 2)
{ throw std::logic_error("must have exactly 2 dimensions"); }
auto& x1 = *(a++);
auto& x2 = *a;
auto t1 = 1.5 - x1 * (1 - x2);
auto t2 = 2.25 - x1 * (1 - x2 * x2);
auto t3 = 2.625 - x1 * (1-x2*x2*x2);
double cost = t1 * t1 + t2 * t2 + t3 * t3;
return cost;
}
//auto domain() const -> domain_type { return std::make_pair(-4.5, 4.5); }
};
class beale
{
public:
auto operator() ( param a, param b ) const -> double
{
if (std::distance(a, b) != 2)
{ throw std::logic_error("must have exactly 2 dimensions"); }
auto& x1 = *(a++);
auto& x2 = *a;
auto t1 = 1.5 - x1 * (1 - x2);
auto t2 = 2.25 - x1 * (1 - x2 * x2);
auto t3 = 2.625 - x1 * (1-x2*x2*x2);
double cost = t1 * t1 + t2 * t2 + t3 * t3;
return cost;
}
//auto domain() const -> domain_type { return std::make_pair(-4.5, 4.5); }
//auto extremum() const -> domain_type { return std::make_pair(0.0,0.0); }
};
/*
class bohachevsky1
{
public:
auto operator() ( param a, param b ) const -> double
{
if (std::distance(a, b) != 2)
{ throw std::logic_error("must have exactly 2 dimensions"); }
auto& x1 = *(a++);
auto& x2 = *a;
auto t1 = x1 * x1;
auto t2 = 2 * x2 * x2;
auto t3 = 0.3 * std::cos( THREE_PI * x1 + FOUR_PI * x2 );
double cost = t1 * t1 + t2 * t2 + t3 * t3;
return cost;
}
//auto domain() const -> domain_type { return std::make_pair(-100.0, 100.0); }
};
*/
class booth
{
public:
auto operator() ( param a, param b ) const -> double
{
if (std::distance(a, b) != 2)
{ throw std::logic_error("must have exactly 2 dimensions"); }
auto& x1 = *(a++);
auto& x2 = *a;
auto t1 = x1 + 2 * x2 - 7;
auto t2 = 2 * x1 + x2 - 5;
double cost = t1 * t1 + t2 * t2;
return cost;
}
//auto domain() const -> domain_type { return std::make_pair(-10.0, 10.0); }
//auto extremum() const -> domain_type { return std::make_pair(1.0,3.0); }
};
class branin
{
public:
auto operator() ( param a, param b ) const -> double
{
if (std::distance(a, b) != 2)
{ throw std::logic_error("must have exactly 2 dimensions"); }
auto& x1 = *(a++);
auto& x2 = *a;
auto t1 = 1.5 - x1 * (1 - x2);
auto t2 = 2.25 - x1 * (1 - x2 * x2);
auto t3 = 2.625 - x1 * (1-x2*x2*x2);
double cost = t1 * t1 + t2 * t2 + t3 * t3;
return cost;
}
//auto domain() const -> domain_type { return std::make_pair(-5.0, 15); /*[-5,10]&[0,15]*/}
//auto extremum() const -> domain_type { return std::make_pair(0.0,0.0); }
};
class colville
{
public:
auto operator() ( param a, param b ) const -> double
{
if (std::distance(a, b) != 4)
{ throw std::logic_error("must have exactly 4 dimensions"); }
auto& x1 = *(a++);
auto& x2 = *(a++);
auto& x3 = *(a++);
auto& x4 = *a;
auto t1 = x1 * x1 - x2;
auto t2 = x1 - 1;
auto t3 = x3 - 1;
auto t4 = x3 * x3 - x4;
auto t5 = x4 - 1;
auto t6 = x2 - 1;
double cost = 100.0 * t1 * t1 + t2 * t2 + t3 * t3 + 90.0 * t4 * t4
+ 10.1 * (t3 * t3 + t5 * t5) + 19.8 * t6 * t5;
return cost;
}
//auto domain() const -> domain_type { return std::make_pair(-10.0, 10.0); }
//auto extremum() const -> domain_type { return std::make_pair(1.0,1.0); }
//auto extremum() const -> domain_type { return std::make_pair(1.0,1.0); }
};
};
int main (int argc, char** argv)
{
int const N = atoi(argv[1]);
//std::shared_ptr<swarmer> s {new swarmer(2, objective::shaffer_f6())};
std::shared_ptr<swarmer> s {new swarmer(2, objective::beale())};
for ( auto i = 1; i < N; ++i )
new swarmer(*s);
try {
s->start();
s->watch();
} catch (std::system_error e) {
std::cerr << e.code() << std::endl;
} catch (std::exception e) {
std::cerr << e.what() << std::endl;
} catch (...) {
std::cerr << "unknown error" << std::endl;
}
double bestcost = s->best_solution().first;
auto const& best = s->best_solution().second;
std::cout << swarmer::update_count << std::endl;
std::cout << bestcost << std::endl;
std::copy(best.cbegin(), best.cend(), std::ostream_iterator<double>(std::cout," "));
std::cout << std::endl;
return 0;
}