CSMOn
Convergence Stabilization Modeling operating in Online mode
PSO.cpp
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 #include "PSO.hpp"
32 
45 PSO::PSO(callback_t fitnessFunction, double s1, double s2, int p, int n, double w, double c1, double c2){
46  this->fitnessFunction = fitnessFunction;
47  this->s1 = s1;
48  this->s2 = s2;
49  this->p = p;
50  this->n = n;
51  this->c1 = c1;
52  this->c2 = c2;
53  if(w < 0){
54  decreasingW = true;
55  this->w = abs(w);
56  }
57  else{
58  decreasingW = false;
59  this->w = w;
60  }
61 
62  Gb = new double[n];
63  pb = new double*[p];
64  pb_Fit = new double[p];
65  x = new double*[p];
66  fit = new double[p];
67  v = new double*[p];
68  for(int i=0; i < p; i++){
69  pb[i] = new double[n];
70  x[i] = new double[n];
71  v[i] = new double[n];
72  }
73  seed = 1;
74  nEvals = 0;
75  Gb_Fit = DBL_MAX;
76  gb = -1;
77 }
78 
79 PSO::~PSO() {
80  for(int i=0; i < p; i++){
81  delete pb[i];
82  delete x[i];
83  delete v[i];
84  }
85  delete Gb;
86  delete pb;
87  delete pb_Fit;
88  delete x;
89  delete fit;
90  delete v;
91 }
92 
96 void PSO::startup(){
97  seed = getRandomSeed();
98  nEvals = 0;
99  Gb_Fit = DBL_MAX;
100  gb = -1;
101 
102  for(int j, i=0; i < p; i++){
103  for(j=0; j < n; j++) v[i][j] = RAND_DOUBLE(seed, s1, s2)/10;
104  for(j=0; j < n; j++) x[i][j] = RAND_DOUBLE(seed, s1, s2);
105  COPY_ARR(x[i], pb[i], n);
106  pb_Fit[i] = fit[i] = fitnessFunction(x[i], n);
107  if(pb_Fit[i] < Gb_Fit){
108  COPY_ARR(pb[i], Gb, n);
109  Gb_Fit = pb_Fit[i];
110  gb = i;
111  }
112  }
113 }
114 
120 void PSO::next(int M) {
121  bool found = false;
122  int i, j;
123  double currW = w - (w / M) * nEvals;
124  while(!found && nEvals < M){
125  for(i=0; i < p; i++){
126  for(j=0; j < n; j++){
127  v[i][j] = currW * v[i][j] + c1 * RAND_DOUBLE(seed, 0, 1) * (pb[i][j] - x[i][j])
128  + c2 * RAND_DOUBLE(seed, 0, 1) * (Gb[j] - x[i][j]);
129  x[i][j] += v[i][j];
130  if(x[i][j] < s1)
131  x[i][j] = s1;
132  else if(x[i][j] > s2)
133  x[i][j] = s2;
134  }
135  fit[i] = fitnessFunction(x[i], n);
136  nEvals++;
137  }
138  for(i=0; i < p; i++){
139  if(fit[i] < pb_Fit[i]){
140  COPY_ARR(x[i], pb[i], n);
141  pb_Fit[i] = fit[i];
142  if(pb_Fit[i] < Gb_Fit){
143  found = true;
144  COPY_ARR(pb[i], Gb, n);
145  Gb_Fit = pb_Fit[i];
146  gb = i;
147  }
148  }
149  }
150  if(decreasingW) currW -= w / M;
151  }
152 }
153 
160 int PSO::getBestPos(double *_x){
161  COPY_ARR(pb[gb], _x, n);
162  return gb;
163 }
164 
171  return nEvals;
172 }
173 
180 double PSO::getFitness() {
181  return Gb_Fit;
182 }
183 
191 unsigned int PSO::getRandomSeed(){
192  struct timeval tv;
193  gettimeofday(&tv,NULL);
194  return tv.tv_usec;
195 }
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
unsigned int getRandomSeed()
Get a random number to be used as seed for the random number generator.
Definition: PSO.cpp:191
double fitnessFunction(double *x, int n)
Fitness function implementation.
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