CSMOn
Convergence Stabilization Modeling operating in Online mode
pso_example.cpp
Go to the documentation of this file.
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 
40 #include <stdio.h>
41 #include <string.h>
42 #include <iostream>
43 #include "PSO.hpp"
44 #include "CSMOn.hpp"
45 
46 using namespace std;
47 
48 double fitnessFunction(double *x, int n);
49 
50 int main(int argc, char *argv[]){
51  // Replicate (and adapt) this block of code for each different search method you want to develop.
52  // Begin template.
53  if(argc >= 11 && strcmp(argv[1], "pso") == 0){
54 
55  double w, c1, c2, s1, s2, R;
56  int i, n, p, M;
57 
58  // Read input parameters to be sent to the search method.
59  for(i=2; i < 19; i+=2){
60  if(strcmp(argv[i], "-w") == 0) w = atof(argv[i+1]);
61  else if(strcmp(argv[i], "-c1") == 0) c1 = atof(argv[i+1]);
62  else if(strcmp(argv[i], "-c2") == 0) c2 = atof(argv[i+1]);
63  else if(strcmp(argv[i], "-n") == 0) n = atoi(argv[i+1]);
64  else if(strcmp(argv[i], "-p") == 0) p = atoi(argv[i+1]);
65  else if(strcmp(argv[i], "-s1") == 0) s1 = atof(argv[i+1]);
66  else if(strcmp(argv[i], "-s2") == 0) s2 = atof(argv[i+1]);
67  else if(strcmp(argv[i], "-M") == 0) M = atoi(argv[i+1]);
68  else if(strcmp(argv[i], "-R") == 0) R = atof(argv[i+1]);
69  }
70  cout << "[CSMOn] Called search method PSO with parameters: s1=" << s1 << ", s2=" << s2 << ", w=" << w
71  << ", c1=" << c1 << ", c2=" << c2 << ", n=" << n << ", p=" << p << ", M=" << M << ", R=" << R << endl;
72 
73  // Search method instantiation.
74  PSO *pso = new PSO(fitnessFunction, s1, s2, p, n, w, c1, c2);
75 
76  // CSMOn instantiation and execution.
77  CSMOn *csmon = new CSMOn(pso, M, R, 0);
78  csmon->run();
79 
80  // Read the final results.
81  double *bestPos = new double[n];
82  csmon->getBestPos(bestPos);
83  cout << endl << "[ ";
84  for(i=0; i < n; i++){ // Send the final result back to Python caller.
85  cout << bestPos[i] << ", ";
86  }
87  cout << " ]" << endl;
88 
89  // Send any parameter you want back to Python caller.
90  cout << "nEvals: " << csmon->getNEvals() << ", Fitness: " << csmon->getFitness() << endl;
91 
92  // Remind to clear any object that is not owned by Python caller.
93  delete bestPos;
94  delete pso;
95  delete csmon;
96  }
97  // End template.
98  else{
99  cout << "Wrong Parameters !" << endl;
100  cout << "Options:" << endl;
101  cout << " pso -w <w-value> -c1 <c1-value> -c2 <c2-value> -n <n-dimensions> -p <n-particles> -s1 <lower_bound> -s2 <upper-bound> -M <max-evals> -R <relaxation>" << endl;
102  }
103 
104  return 0;
105 }
106 
112 double fitnessFunction(double *x, int n){
113  double s = 0;
114  for(int i=0; i < n; i++){
115  s += x[i] * x[i] - 10 * cos(2 * PI * x[i]);
116  }
117  return 10 * n + s;
118 }
Convergence Stabilization Modeling operating in Online Mode.
Definition: CSMOn.hpp:75
int getBestPos(double *x)
Get the final optimized result (position).
Definition: CSMOn.cpp:198
Particle Swarm Optimization.
Definition: PSO.hpp:53
double fitnessFunction(double *x, int n)
Fitness function implementation.
void run()
Call this method to execute the search.
Definition: CSMOn.cpp:68
double getFitness()
Get the final fitness value.
Definition: CSMOn.cpp:178
int getNEvals()
Get the actual number of evaluations executed.
Definition: CSMOn.cpp:188