diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..2f77c4e42e7c44f2f771b8a6b60c4f504489f079 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +.PHONY: all clean + +all: CSMOn CSMOn_wrapper cpp_example + +CSMOn: + $(MAKE) -C cpp + +CSMOn_wrapper: + $(MAKE) -C python + +cpp_example: cpp/pso_example.cpp + g++ -o cpp/pso_example cpp/pso_example.cpp cpp/PSO.o cpp/CSMOn.o -Wall -pedantic-errors + +clean: + $(MAKE) -i -C cpp clean + $(MAKE) -i -C python clean + diff --git a/cpp/Makefile b/cpp/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..18ec81985b10f2c03a3c8c3e9617e025817a073e --- /dev/null +++ b/cpp/Makefile @@ -0,0 +1,13 @@ +FONTS=PSO.cpp CSMOn.cpp +OBJECTS=$(FONTS:.cpp=.o) +FLAGS=-Wall -pedantic-errors + +.PHONY: all clean + +all: $(OBJECTS) + +%.o: %.cpp + g++ $< -c -o $@ $(FLAGS) -fPIC + +clean: + rm $(OBJECTS) diff --git a/cpp/pso_example.cpp b/cpp/pso_example.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5f309b42eec58ea79bcedc85ffe1f445f67a6e5 --- /dev/null +++ b/cpp/pso_example.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2017, Peter Frank Perroni (pfperroni@gmail.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * Additionally, if used for any scientific purpose, the following reference + * must be cited: + * + * Peter Frank Perroni, Daniel Weingaertner, and Myriam Regattieri Delgado. + * 2017. Estimating Stop Conditions of Swarm Based Stochastic Metaheuristic + * Algorithms. In Proceedings of GECCO '17, Berlin, Germany, July 15-19, 2017, + * pg 43-50. DOI: http://dx.doi.org/10.1145/3071178.3071326 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * + * This file provides an implementation example to call CSMOn. + * + * @date 02/Jul/2017 + * @author Peter Frank Perroni (pfperroni@gmail.com) + */ + +#include <stdio.h> +#include <string.h> +#include <iostream> +#include "PSO.hpp" +#include "CSMOn.hpp" + +using namespace std; + +double fitnessFunction(double *x, int n); + +int main(int argc, char *argv[]){ + // Replicate (and adapt) this block of code for each different search method you want to develop. + // Begin template. + if(argc >= 11 && strcmp(argv[1], "pso") == 0){ + + double w, c1, c2, s1, s2, R; + int i, n, p, M; + + // Read input parameters to be sent to the search method. + for(i=2; i < 19; i+=2){ + if(strcmp(argv[i], "-w") == 0) w = atof(argv[i+1]); + else if(strcmp(argv[i], "-c1") == 0) c1 = atof(argv[i+1]); + else if(strcmp(argv[i], "-c2") == 0) c2 = atof(argv[i+1]); + else if(strcmp(argv[i], "-n") == 0) n = atoi(argv[i+1]); + else if(strcmp(argv[i], "-p") == 0) p = atoi(argv[i+1]); + else if(strcmp(argv[i], "-s1") == 0) s1 = atof(argv[i+1]); + else if(strcmp(argv[i], "-s2") == 0) s2 = atof(argv[i+1]); + else if(strcmp(argv[i], "-M") == 0) M = atoi(argv[i+1]); + else if(strcmp(argv[i], "-R") == 0) R = atof(argv[i+1]); + } + cout << "[CSMOn] Called search method PSO with parameters: s1=" << s1 << ", s2=" << s2 << ", w=" << w + << ", c1=" << c1 << ", c2=" << c2 << ", n=" << n << ", p=" << p << ", M=" << M << ", R=" << R << endl; + + // Search method instantiation. + PSO *pso = new PSO(fitnessFunction, s1, s2, p, n, w, c1, c2); + + // CSMOn instantiation and execution. + CSMOn *csmon = new CSMOn(pso, M, R, 0); + csmon->run(); + + // Read the final results. + double *bestPos = new double[n]; + csmon->getBestPos(bestPos); + cout << endl << "[ "; + for(i=0; i < n; i++){ // Send the final result back to Python caller. + cout << bestPos[i] << ", "; + } + cout << " ]" << endl; + + // Send any parameter you want back to Python caller. + cout << "nEvals: " << csmon->getNEvals() << ", Fitness: " << csmon->getFitness() << endl; + + // Remind to clear any object that is not owned by Python caller. + delete bestPos; + delete pso; + delete csmon; + } + // End template. + else{ + cout << "Wrong Parameters !" << endl; + cout << "Options:" << endl; + 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; + } + + return 0; +} + +/** + * @brief Fitness function implementation. + * + * Put your fitness function here. + */ +double fitnessFunction(double *x, int n){ + double s = 0; + for(int i=0; i < n; i++){ + s += (x[i]+2) * (x[i]+2) - 10 * cos(2 * PI * (x[i]+2)); + } + return 10 * n + s; +} diff --git a/python/Makefile b/python/Makefile index e3a36bb8524dfd9995290b7254bf74d2252e033a..c7290b407e1ec4678c41a01c59747b302192c118 100644 --- a/python/Makefile +++ b/python/Makefile @@ -1,5 +1,6 @@ -FONTES=../cpp/PSO.cpp ../cpp/CSMOn.cpp CSMOn_wrapper.cpp -OBJECTS=$(FONTES:.cpp=.o) +FONTS=CSMOn_wrapper.cpp +DEPENDENCIES=../cpp/PSO.o ../cpp/CSMOn.o +OBJECTS=$(FONTS:.cpp=.o) FLAGS=-Wall -pedantic-errors -fPIC .PHONY: all clean @@ -7,10 +8,11 @@ FLAGS=-Wall -pedantic-errors -fPIC all: CSMOn_wrapper CSMOn_wrapper: $(OBJECTS) - g++ -o $@.so $^ $(FLAGS) -shared + g++ -o $@.so $^ $(DEPENDENCIES) $(FLAGS) -shared %.o: %.cpp g++ -I../cpp $< -c -o $@ $(FLAGS) clean: rm $(OBJECTS) CSMOn.pyc fitnessFunction.pyc + diff --git a/python/pso_example.py b/python/pso_example.py index c613c4e16ac675e01017f286137216f032920ecf..44292c629ed735388ae13764245c24baa4b1c385 100644 --- a/python/pso_example.py +++ b/python/pso_example.py @@ -41,7 +41,7 @@ s1=Param("s1", d=-5.12) s2=Param("s2", d=5.12) w=Param("w", d=-0.5) c1=Param("c1", d=0.2) -c2=Param("c2", d=0.2) +c2=Param("c2", d=0.35) n=Param("n", i=100) p=Param("p", i=30) M=Param("M", i=50000)