RockyML  0.0.1
A High-Performance Scientific Computing Framework
benchmark.h
1 /*
2  Copyright (C) 2022 Amirabbas Asadi , All Rights Reserved
3  distributed under Apache-2.0 license
4 */
5 #ifndef ROCKY_BENCHMARK_GUARD
6 #define ROCKY_BENCHMARK_GUARD
7 #define _USE_MATH_DEFINES
8 
9 #include<string>
10 #include<sstream>
11 #include<cmath>
12 #include<random>
13 #include<algorithm>
14 #include<numeric>
15 #include<fstream>
16 
17 #include<tbb/tbb.h>
18 #include<Eigen/Core>
19 #include<Eigen/QR>
20 
21 
22 #include<rocky/zagros/system.h>
23 #include<rocky/utils.h>
24 
25 namespace rocky{
26 namespace zagros
27 {
28 namespace benchmark
29 {
34 template<typename T_e>
35 class sphere: public rocky::zagros::system<T_e>{
36 protected:
37  int dim_;
38 public:
39  sphere(int dim){
40  dim_ = dim;
41  }
42  virtual T_e objective(T_e* x){
43  T_e S = 0.0;
44  for(size_t i=0; i<dim_; i++)
45  S += x[i] * x[i];
46  return sqrt(S);
47  }
48  virtual T_e lower_bound(){ return -10.0; }
49  virtual T_e upper_bound(){ return 10.0; }
50  virtual std::string to_string(){
51  std::stringstream name;
52  name << "Sphere(dim=" << dim_ << ")";
53  return name.str();
54  }
55 };
56 
61 template<typename T_e>
62 class rosenbrock: public rocky::zagros::system<T_e>{
63 protected:
64  int dim_;
65 
66 public:
67  rosenbrock(int dim=2){
68  dim_ = dim;
69  }
70  virtual T_e objective(T_e* x){
71  T_e S = 0;
72  for(size_t i=0; i<dim_-1; i++)
73  S += 100.0 * pow(x[i+1] - pow(x[i], 2), 2) + pow(1 - x[i], 2);
74  return S;
75  }
76  virtual T_e lower_bound(){ return -10.0; }
77  virtual T_e upper_bound(){ return 10.0; }
78  virtual std::string to_string(){
79  std::stringstream name;
80  name << "Rosenbrock(dim=" << dim_ << ")";
81  return name.str();
82  }
83 };
84 
85 template<typename T_e>
86 class rastrigin: public rocky::zagros::system<T_e>{
87 protected:
88  int dim_;
89  T_e shift_;
90 public:
97  rastrigin(int dim=2, T_e shift=0.0){
98  dim_ = dim;
99  shift_ = shift;
100  }
101  virtual T_e objective(T_e* x){
102  T_e S = 10.0 * dim_;
103  for(size_t i=0; i<dim_; i++)
104  S += (x[i]-shift_) * (x[i]-shift_) - 10.0*cos(2*M_PI * (x[i]-shift_));
105  return S;
106  }
107  virtual T_e lower_bound(){ return -5.12; }
108  virtual T_e upper_bound(){ return 5.12; }
109  virtual std::string to_string(){
110  std::stringstream name;
111  name << "Rastrigin(dim=" << dim_ << ")";
112  return name.str();
113  }
114 };
115 
116 template<typename T_e>
118 protected:
119  int dim_;
120  T_e shift_;
121 public:
128  rastrigin_parallel(int dim=2, T_e shift=0.0){
129  dim_ = dim;
130  shift_ = shift;
131  }
132  virtual T_e objective(T_e* x){
133  T_e S = 10.0 * dim_;
134  tbb::combinable<T_e> partial_sums{0.0};
135  tbb::parallel_for(0, dim_, [&](auto i){
136  partial_sums.local() += (x[i]-shift_) * (x[i]-shift_) - 10.0*cos(2*M_PI * (x[i]-shift_));
137  });
138  S += partial_sums.combine([](auto x, auto y){ return x+y; });
139  return S;
140  }
141  virtual T_e lower_bound(){ return -5.12; }
142  virtual T_e upper_bound(){ return 5.12; }
143  virtual std::string to_string(){
144  std::stringstream name;
145  name << "Parallel Rastrigin(dim=" << dim_ << ")";
146  return name.str();
147  }
148 };
149 
150 template<typename T_e>
151 class ackley: public rocky::zagros::system<T_e>{
152 protected:
153  int dim_;
154 
155 public:
156  ackley(int dim=2){
157  dim_ = dim;
158  }
159  virtual T_e objective(T_e* x){
160  T_e S = 20.0 + exp(1.0);
161  T_e S_c = 0;
162  T_e S_s = 0;
163  for(size_t i=0; i<dim_; i++){
164  S_s += pow(x[i], 2);
165  S_c += cos(2*M_PI * x[i]);
166  }
167  S_s /= dim_;
168  S_c /= dim_;
169  S_s = -0.20 * sqrt(S_s);
170  S_s = -20.0 * exp(S_s);
171  S_c = -exp(S_c);
172 
173  S += S_s + S_c;
174  return S;
175  }
176  virtual T_e lower_bound(){ return -5.0; }
177  virtual T_e upper_bound(){ return 5.0; }
178  virtual std::string to_string(){
179  std::stringstream name;
180  name << "Ackley(dim=" << dim_ << ")";
181  return name.str();
182  }
183 };
184 
189 template<typename T_e>
190 class griewank: public rocky::zagros::system<T_e>{
191 protected:
192  int dim_;
193  T_e lb_;
194  T_e ub_;
195 
196 public:
204  griewank(int dim=2, T_e lb=-20.0, T_e ub=20.0){
205  dim_ = dim;
206  lb_ = lb;
207  ub_ = ub;
208  }
209  virtual T_e objective(T_e* x){
210  T_e S = 1;
211  T_e S_p = 0;
212  T_e P_c = 1.0;
213  for(size_t i=0; i<dim_; i++){
214  S_p += pow(x[i], 2)/4000.0;
215  P_c *= cos(x[i]/sqrt(i+1));
216  }
217  S += S_p - P_c;
218  return S;
219  }
220  virtual T_e lower_bound(){ return lb_; }
221  virtual T_e upper_bound(){ return ub_; }
222  virtual std::string to_string(){
223  std::stringstream name;
224  name << "griewank(dim=" << dim_ << ")";
225  return name.str();
226  }
227 };
228 
234 template<typename T_e>
235 class dropwave: public rocky::zagros::system<T_e>{
236 protected:
237  int dim_;
238  T_e lb_;
239  T_e ub_;
240 
241 public:
247  dropwave(int dim=2){
248  dim_ = dim;
249  }
250  virtual T_e objective(T_e* x){
251  T_e S_s = 0;
252  for(size_t i=0; i<dim_; i++)
253  S_s += pow(x[i], 2);
254  T_e value = -(1 + cos(12.0 * sqrt(S_s)))/(0.5 * S_s + 1);
255  return value;
256  }
257  virtual T_e lower_bound(){ return -5.12; }
258  virtual T_e upper_bound(){ return 5.12; }
259  virtual std::string to_string(){
260  std::stringstream name;
261  name << "dropwave(dim=" << dim_ << ")";
262  return name.str();
263  }
264 };
265 
266 
267 
268 template<typename T_e>
270 public:
271  std::vector<T_e> A_;
272  std::vector<T_e> b_;
273 
274  thread_safe_least_squares(int m, int n){
275  std::mt19937 local_rng(0);
276  A_.resize(m * n);
277  b_.resize(m);
278  std::uniform_real_distribution<T_e> dist(-10.0, 10.0);
279  for(int i=0; i<m; i++)
280  b_[i] = dist(local_rng);
281 
282  for(int i=0; i<m*n; i++)
283  A_[i] = dist(local_rng);
284  }
285 };
286 
287 template<typename T_e>
289 protected:
290  int m_;
291  int n_;
292  tbb::enumerable_thread_specific<thread_safe_least_squares<T_e>> problem_;
293 
294 public:
295  least_squares(int m, int n): problem_(m, n){
296  m_ = m;
297  n_ = n;
298  }
299  virtual T_e objective(T_e* x_){
300  T_e* A_ = problem_.local().A_.data();
301  T_e* b_ = problem_.local().b_.data();
302 
303  Eigen::Map<Eigen::Matrix<T_e, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> A(A_, m_, n_);
304  Eigen::Map<Eigen::Matrix<T_e, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> b(b_, m_, 1);
305  Eigen::Map<Eigen::Matrix<T_e, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> x(x_, n_, 1);
306 
307  auto error = (A*x - b).norm();
308 
309  return error;
310  }
311  virtual T_e lower_bound(){ return -20.0; }
312  virtual T_e upper_bound(){ return 20.0; }
313 };
314 
315 }; // namespace benchmark
316 
317 }; // namespace zagros
318 
319 }; // namespace rocky
320 
321 #endif
rocky::zagros::benchmark::griewank::griewank
griewank(int dim=2, T_e lb=-20.0, T_e ub=20.0)
Construct a new griewank object.
Definition: benchmark.h:204
rocky::zagros::benchmark::ackley::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:177
rocky::zagros::benchmark::dropwave::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:258
rocky::zagros::benchmark::rastrigin
Definition: benchmark.h:86
rocky::zagros::benchmark::rastrigin::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:108
rocky::zagros::benchmark::griewank::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:221
rocky::zagros::benchmark::rastrigin_parallel::rastrigin_parallel
rastrigin_parallel(int dim=2, T_e shift=0.0)
Construct a new rastrigin object.
Definition: benchmark.h:128
rocky::zagros::benchmark::rastrigin_parallel::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:142
rocky::zagros::benchmark::rastrigin_parallel
Definition: benchmark.h:117
rocky::zagros::benchmark::griewank::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:220
rocky::zagros::benchmark::dropwave::dropwave
dropwave(int dim=2)
Construct a new dropwave problem.
Definition: benchmark.h:247
rocky::zagros::benchmark::sphere
Sphere function.
Definition: benchmark.h:35
rocky::zagros::benchmark::least_squares::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:312
rocky::zagros::benchmark::rastrigin_parallel::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:141
rocky::zagros::benchmark::rastrigin::rastrigin
rastrigin(int dim=2, T_e shift=0.0)
Construct a new rastrigin object.
Definition: benchmark.h:97
rocky::zagros::benchmark::dropwave
Dropwave function referenc : https://www.sfu.ca/~ssurjano/drop.html.
Definition: benchmark.h:235
rocky::zagros::benchmark::sphere::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:49
rocky::zagros::benchmark::ackley::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:176
rocky::zagros::benchmark::thread_safe_least_squares
Definition: benchmark.h:269
rocky::zagros::system
Definition: system.h:20
rocky::zagros::benchmark::least_squares::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:311
rocky::zagros::benchmark::rosenbrock::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: benchmark.h:77
rocky::zagros::benchmark::rosenbrock
Rosenbrock function.
Definition: benchmark.h:62
rocky::zagros::benchmark::least_squares
Definition: benchmark.h:288
rocky::zagros::benchmark::sphere::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:48
rocky::zagros::benchmark::griewank
Griewank function reference: https://www.sfu.ca/~ssurjano/griewank.html.
Definition: benchmark.h:190
rocky::zagros::benchmark::ackley
Definition: benchmark.h:151
rocky::zagros::benchmark::dropwave::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:257
rocky::zagros::benchmark::rastrigin::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:107
rocky::zagros::benchmark::rosenbrock::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: benchmark.h:76