CSMOn
Convergence Stabilization Modeling operating in Online mode
CSMOn_wrapper.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 
49 typedef struct _Param {
50  char *name;
51  char c;
52  int i;
53  long l;
54  float f;
55  double d;
56 } Param;
57 
69 extern "C" void search(char* method, Param *inParam, Param *outParam, double *outPos, callback_t fitnessFunction){
70 
71  // Replicate (and adapt) this block of code for each different search method you want to develop.
72  // Begin template.
73  if(strcmp(method, "pso") == 0){
74 
75  double w, c1, c2, s1, s2, R;
76  int i, n, p, M;
77 
78  // Receive from Python caller any parameter you want.
79  for(i=0; i < 9; i++){
80  if(strcmp(inParam[i].name, "w") == 0) w = inParam[i].d;
81  else if(strcmp(inParam[i].name, "c1") == 0) c1 = inParam[i].d;
82  else if(strcmp(inParam[i].name, "c2") == 0) c2 = inParam[i].d;
83  else if(strcmp(inParam[i].name, "n") == 0) n = inParam[i].i;
84  else if(strcmp(inParam[i].name, "p") == 0) p = inParam[i].i;
85  else if(strcmp(inParam[i].name, "s1") == 0) s1 = inParam[i].d;
86  else if(strcmp(inParam[i].name, "s2") == 0) s2 = inParam[i].d;
87  else if(strcmp(inParam[i].name, "M") == 0) M = inParam[i].i;
88  else if(strcmp(inParam[i].name, "R") == 0) R = inParam[i].d;
89  }
90  cout << "[CSMOn] Called search method PSO with parameters: s1=" << s1 << ", s2=" << s2 << ", w=" << w
91  << ", c1=" << c1 << ", c2=" << c2 << ", n=" << n << ", p=" << p << ", M=" << M << ", R=" << R << endl;
92 
93  // Search method instantiation.
94  PSO *pso = new PSO(fitnessFunction, s1, s2, p, n, w, c1, c2);
95 
96  // CSMOn instantiation and execution.
97  CSMOn *csmon = new CSMOn(pso, M, R, 0);
98  csmon->run();
99 
100  // Read the final results.
101  double *bestPos = new double[n];
102  csmon->getBestPos(bestPos);
103  for(i=0; i < n; i++){ // Send the final result back to Python caller.
104  outPos[i] = bestPos[i];
105  }
106 
107  // Send any parameter you want back to Python caller.
108  int evals = csmon->getNEvals();
109  double fit = csmon->getFitness();
110  for(i=0; i < 2; i++){
111  if(strcmp(outParam[i].name, "nEvals") == 0) outParam[i].i = evals;
112  else if(strcmp(outParam[i].name, "fit") == 0) outParam[i].d = fit;
113  }
114 
115  // Remind to clear any object that is not owned by Python caller.
116  delete bestPos;
117  delete pso;
118  delete csmon;
119  }
120  // End template.
121  else printf("Search method %s not supported yet.", method);
122 
123 }
struct _Param Param
Contains a parameter received from / sent to the Python caller.
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
Contains a parameter received from / sent to the Python caller.
double getFitness()
Get the final fitness value.
Definition: CSMOn.cpp:178
void search(char *method, Param *inParam, Param *outParam, double *outPos, callback_t fitnessFunction)
The wrapper function for Python calls.
int getNEvals()
Get the actual number of evaluations executed.
Definition: CSMOn.cpp:188