CSMOn
Convergence Stabilization Modeling operating in Online mode
PSO.hpp
1 /*
2  * Copyright (c) 2017, Peter Frank Perroni (pfperroni@gmail.com)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * Additionally, if used for any scientific purpose, the following reference
15  * must be cited:
16  *
17  * Peter Frank Perroni, Daniel Weingaertner, and Myriam Regattieri Delgado.
18  * 2017. Estimating Stop Conditions of Swarm Based Stochastic Metaheuristic
19  * Algorithms. In Proceedings of GECCO '17, Berlin, Germany, July 15-19, 2017,
20  * pg 43-50. DOI: http://dx.doi.org/10.1145/3071178.3071326
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28  * THE SOFTWARE.
29  */
30 
31 #ifndef PSO_HPP_
32 #define PSO_HPP_
33 
34 #include <sys/time.h>
35 #include <float.h>
36 #include <stdlib.h>
37 #include "ISearch.hpp"
38 
39 #define PI (3.141592653589793238462643383279)
40 
41 #define COPY_ARR(orig, dest, sz) for(int _i_=0; _i_ < sz; dest[_i_] = orig[_i_], _i_++);
42 
43 #define RAND_DOUBLE(seed, a, b) ((a==b)?a:(a+(((double)rand_r(&seed))/RAND_MAX)*(b-a)))
44 
45 typedef double (*callback_t)(double *buf, int n);
46 
53 class PSO : public ISearch{
54  unsigned int seed;
55  bool decreasingW;
56  int p, n, nEvals, gb;
57  double **x, *fit, s1, s2, w, c1, c2, *Gb, Gb_Fit, **pb, *pb_Fit, **v;
58 
59  callback_t fitnessFunction;
60 
61 public:
62  PSO(callback_t fitnessFunction, double s1, double s2, int p, int n, double w, double c1, double c2) ;
63  ~PSO();
64  void startup();
65  void next(int M);
66  int getBestPos(double *_x);
67  int getNEvals();
68  double getFitness();
69  unsigned int getRandomSeed();
70 };
71 
72 #endif /* PSO_HPP_ */
int getNEvals()
Get the number of fitness function evaluations performed up to the moment.
Definition: PSO.cpp:170
void next(int M)
Obtain the next improvement.
Definition: PSO.cpp:120
The interface that the search methods must to implement.
Definition: ISearch.hpp:40
Particle Swarm Optimization.
Definition: PSO.hpp:53
unsigned int getRandomSeed()
Get a random number to be used as seed for the random number generator.
Definition: PSO.cpp:191
void startup()
Startup the PSO.
Definition: PSO.cpp:96
double getFitness()
Get the best fitness value found up to the moment.
Definition: PSO.cpp:180
int getBestPos(double *_x)
Get the best result obtained up to the moment (global best).
Definition: PSO.cpp:160
PSO(callback_t fitnessFunction, double s1, double s2, int p, int n, double w, double c1, double c2)
A standard implementation of PSO.
Definition: PSO.cpp:45