Skip to content
Snippets Groups Projects
Commit 529d40c5 authored by Peter Frank Perroni's avatar Peter Frank Perroni
Browse files

Updated project files

parent 48a2c9da
Branches
No related tags found
No related merge requests found
Makefile 0 → 100644
.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
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)
/*
* 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;
}
FONTES=../cpp/PSO.cpp ../cpp/CSMOn.cpp CSMOn_wrapper.cpp FONTS=CSMOn_wrapper.cpp
OBJECTS=$(FONTES:.cpp=.o) DEPENDENCIES=../cpp/PSO.o ../cpp/CSMOn.o
OBJECTS=$(FONTS:.cpp=.o)
FLAGS=-Wall -pedantic-errors -fPIC FLAGS=-Wall -pedantic-errors -fPIC
.PHONY: all clean .PHONY: all clean
...@@ -7,10 +8,11 @@ FLAGS=-Wall -pedantic-errors -fPIC ...@@ -7,10 +8,11 @@ FLAGS=-Wall -pedantic-errors -fPIC
all: CSMOn_wrapper all: CSMOn_wrapper
CSMOn_wrapper: $(OBJECTS) CSMOn_wrapper: $(OBJECTS)
g++ -o $@.so $^ $(FLAGS) -shared g++ -o $@.so $^ $(DEPENDENCIES) $(FLAGS) -shared
%.o: %.cpp %.o: %.cpp
g++ -I../cpp $< -c -o $@ $(FLAGS) g++ -I../cpp $< -c -o $@ $(FLAGS)
clean: clean:
rm $(OBJECTS) CSMOn.pyc fitnessFunction.pyc rm $(OBJECTS) CSMOn.pyc fitnessFunction.pyc
...@@ -41,7 +41,7 @@ s1=Param("s1", d=-5.12) ...@@ -41,7 +41,7 @@ s1=Param("s1", d=-5.12)
s2=Param("s2", d=5.12) s2=Param("s2", d=5.12)
w=Param("w", d=-0.5) w=Param("w", d=-0.5)
c1=Param("c1", d=0.2) c1=Param("c1", d=0.2)
c2=Param("c2", d=0.2) c2=Param("c2", d=0.35)
n=Param("n", i=100) n=Param("n", i=100)
p=Param("p", i=30) p=Param("p", i=30)
M=Param("M", i=50000) M=Param("M", i=50000)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment