-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjs.hpp
291 lines (272 loc) · 8.92 KB
/
objs.hpp
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
//#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 <sstream>
#include <system_error>
#include <vector>
class swarm;
class particle : protected std::vector<double>
{
protected:
using super = std::vector<double>;
using param = super::const_iterator;
using solution = std::pair<super::value_type,super>;
super velocity;
public:
//using super::vector; // this should replace the constructors below
particle() : super() {}
particle(unsigned n)
: super(n), velocity(n,0),
local_best(std::numeric_limits<double>::infinity(),*this)
{}
using super::operator[];
using super::begin;
using super::cbegin;
using super::end;
using super::cend;
using super::size;
using super::const_iterator;
friend bool operator< ( solution const& a, solution const& b );
friend bool operator< ( double a, solution const& b );
friend bool operator< ( solution const& a, double b );
private:
friend swarm;
solution local_best;
};
bool operator< ( particle::solution const& a, particle::solution const& b ) { return a.first < b.first; }
bool operator< ( double a, particle::solution const& b ) { return a < b.first; }
bool operator< ( particle::solution const& a, double b ) { return a.first < b; }
struct objective
{
using param = std::forward_list<double>::const_iterator;
using domain_type = std::pair<double,double>;
virtual auto operator() ( param a, param b ) const -> double = 0;
virtual auto domain ( unsigned i ) const -> domain_type = 0;
virtual auto extremum ( unsigned i ) const -> double = 0;
};
class sphere : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto cost = std::inner_product(a, b, a, 0.0);
return cost;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-5.12, 5.12); }
auto extremum ( unsigned i ) const -> double { return 0.0; }
};
class rosenbrock : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto c = a;
auto cost = std::inner_product(++a, b, c, 0.0,
std::plus<double>(),
[](double x2, double x1) {
auto t1 = x1 * x1 - x2;
auto t2 = x1 - 1.0;
return 100.0 * t1 * t1 + t2 * t2;
}
);
return cost;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-5.0, 10.0); }
auto extremum ( unsigned i ) const -> double { return (i == 0 ? 0.0 : 1.0); }
};
class rastrigin : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto cost = std::accumulate(a, b, 0.0,
[](double sum, double x) {
static auto const TWOPI = 8.0 * std::atan(1.0);
return sum + x * x - 10.0 * std::cos(TWOPI * x);
}
);
unsigned n = distance(a,b);
return 10.0 * n + cost;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-5.12, 5.12); }
auto extremum ( unsigned i ) const -> double { return 0.0; }
};
class griewangk : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto cost1 = std::accumulate(a, b, 0.0,
[](double sum, double x) { return sum + x * x / 4000.0; }
);
auto i = 0.0;
auto cost2 = std::accumulate(a, b, 1.0,
[&i](double prod, double x) {
return prod * std::cos(x / std::sqrt(++i));
}
);
return cost1 - cost2 + 1.0;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-600.0, 600.0); }
auto extremum ( unsigned i ) const -> double { return 0.0; }
};
class shaffer_f6 : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto x1 = *(a++), x2 = *a;
auto h = x1 * x1 + x2 * x2;
auto denom = 1 + 0.001 * h;
auto numer = std::sin(std::sqrt(h));
auto cost = 0.5 + (numer * numer - 0.5) / (denom * denom);
return cost;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-10.0, 10.0); } //!!
auto extremum ( unsigned i ) const -> double { return 0.0; }
};
class shaffer_f6_inv : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto x1 = *(a++), x2 = *a;
auto h = x1 * x1 + x2 * x2;
auto denom = 1 + 0.001 * h;
auto numer = std::sin(std::sqrt(h));
auto cost = 0.5 - (numer * numer - 0.5) / (denom * denom);
return cost;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-10.0, 10.0); } //!!
auto extremum ( unsigned i ) const -> double { return 0.0; }
};
class beale : public objective
{
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 ( unsigned i ) const -> domain_type { return std::make_pair(-4.5, 4.5); }
auto extremum ( unsigned i ) const -> double
{ return (i == 0 ? 0.0 : (i == 1 ? 3.0 : 0.5)); }
};
/*
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 ( unsigned i ) const -> domain_type { return std::make_pair(-600.0, 600.0); }
//auto extremum ( unsigned i ) const -> double { return 0.0; }
};
*/
class booth : public objective
{
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 ( unsigned i ) const -> domain_type { return std::make_pair(-10.0, 10.0); }
auto extremum ( unsigned i ) const -> double
{ return (i == 0 ? 0.0 : (i == 1 ? 1.0 : 3.0)); }
};
class branin : public objective
{
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 ( unsigned i ) const -> domain_type
{ return (i == 1 ? std::make_pair(-5.0, 10.0) : std::make_pair(-0.0, 15.0)); }
auto extremum ( unsigned i ) const -> double
{ return (i == 0 ? 0.397887 : (i == 1 ? 9.42478 : 2.475)); }
};
class colville : public objective
{
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 ( unsigned i ) const -> domain_type { return std::make_pair(-10.0, 10.0); }
auto extremum ( unsigned i ) const -> double { return (i == 0 ? 0.0 : 1.0); }
};
class dixon_price : public objective
{
public:
auto operator() ( param a, param b ) const -> double
{
auto cost = *a - 1.0;
cost *= cost;
unsigned i = 1;
auto c = a;
cost += std::inner_product(++a, b, c, 0.0,
std::plus<double>(),
[&i](double x2, double x1) {
auto t = 2 * x2 * x2 - x1;
return (++i) * t * t;
}
);
return cost;
}
auto domain ( unsigned i ) const -> domain_type { return std::make_pair(-10.0, 10.0); }
auto extremum ( unsigned i ) const -> double { return 0.0; }
};