From 8ba39921faeb2ae75817a427c7ef1256775bbf06 Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Mon, 5 Jul 2010 19:06:34 +0200 Subject: [PATCH] + cma_sa application --- application/cma_sa/CMakeLists.txt | 35 +++++ application/cma_sa/Rosenbrock.h | 42 ++++++ application/cma_sa/Sphere.h | 42 ++++++ application/cma_sa/cma_sa.param | 7 + application/cma_sa/main.cpp | 233 ++++++++++++++++++++++++++++++ application/cma_sa/plot.py | 232 +++++++++++++++++++++++++++++ 6 files changed, 591 insertions(+) create mode 100644 application/cma_sa/CMakeLists.txt create mode 100644 application/cma_sa/Rosenbrock.h create mode 100644 application/cma_sa/Sphere.h create mode 100644 application/cma_sa/cma_sa.param create mode 100644 application/cma_sa/main.cpp create mode 100755 application/cma_sa/plot.py diff --git a/application/cma_sa/CMakeLists.txt b/application/cma_sa/CMakeLists.txt new file mode 100644 index 00000000..a913882b --- /dev/null +++ b/application/cma_sa/CMakeLists.txt @@ -0,0 +1,35 @@ +FIND_PACKAGE(Boost 1.33.0 REQUIRED) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +SET(RESOURCES + cma_sa.param + plot.py + ) + +FOREACH(file ${RESOURCES}) + EXECUTE_PROCESS( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/${file} + ${CMAKE_CURRENT_BINARY_DIR}/${file} + ) +ENDFOREACH(file) + +ADD_EXECUTABLE(cma_sa main.cpp) + +TARGET_LINK_LIBRARIES(cma_sa + ${Boost_LIBRARIES} + BOPO + eoutils + pthread + moeo + eo + peo + rmc_mpi + eometah + nklandscapes + BOPO + #${MPICH2_LIBRARIES} + ${LIBXML2_LIBRARIES} + ${MPI_LIBRARIES} + ) diff --git a/application/cma_sa/Rosenbrock.h b/application/cma_sa/Rosenbrock.h new file mode 100644 index 00000000..c21a8cce --- /dev/null +++ b/application/cma_sa/Rosenbrock.h @@ -0,0 +1,42 @@ +#ifndef _Rosenbrock_h +#define _Rosenbrock_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template < typename EOT > +class Rosenbrock : public eoEvalFunc< EOT > +{ +public: + typedef typename EOT::AtomType AtomType; + + virtual void operator()( EOT& p ) + { + if (!p.invalid()) + return; + + p.fitness( _evaluate( p ) ); + } + +private: + AtomType _evaluate( EOT& p ) + { + AtomType r = 0.0; + + for (unsigned int i = 0; i < p.size() - 1; ++i) + { + r += p[i] * p[i]; + } + + return r; + } +}; + +#endif // !_Rosenbrock_h diff --git a/application/cma_sa/Sphere.h b/application/cma_sa/Sphere.h new file mode 100644 index 00000000..1e73b3ea --- /dev/null +++ b/application/cma_sa/Sphere.h @@ -0,0 +1,42 @@ +#ifndef _Sphere_h +#define _Sphere_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template < typename EOT > +class Sphere : public eoEvalFunc< EOT > +{ +public: + typedef typename EOT::AtomType AtomType; + + virtual void operator()( EOT& p ) + { + if (!p.invalid()) + return; + + p.fitness( _evaluate( p ) ); + } + +private: + AtomType _evaluate( EOT& p ) + { + AtomType r = 0.0; + + for (unsigned int i = 0; i < p.size() - 1; ++i) + { + r += p[i] * p[i]; + } + + return r; + } +}; + +#endif // !_Sphere_h diff --git a/application/cma_sa/cma_sa.param b/application/cma_sa/cma_sa.param new file mode 100644 index 00000000..9aceb230 --- /dev/null +++ b/application/cma_sa/cma_sa.param @@ -0,0 +1,7 @@ +--rho=0 # -p : +// #include + +#ifndef HAVE_GNUPLOT +// FIXME: temporary define to force use of gnuplot without compiling +// again EO. +# define HAVE_GNUPLOT +#endif + +#include +#include + +#include +#include + +#include +#include +#include +#include + +//#include "BopoRosenbrock.h" +#include "Rosenbrock.h" +#include "Sphere.h" + +#include + +typedef eoReal EOT; +//typedef doUniform< EOT > Distrib; +//typedef doNormal< EOT > Distrib; + +int main(int ac, char** av) +{ + eoParserLogger parser(ac, av); + + // Letters used by the following declarations : + // a d i p t + + std::string section("Algorithm parameters"); + + // FIXME: a verifier la valeur par defaut + double initial_temperature = parser.createParam((double)10e5, "temperature", "Initial temperature", 'i', section).value(); // i + + eoState state; + + //----------------------------------------------------------------------------- + // Instantiate all need parameters for CMASA algorithm + //----------------------------------------------------------------------------- + + eoSelect< EOT >* selector = new eoDetSelect< EOT >(0.1); + state.storeFunctor(selector); + + //doEstimator< doUniform< EOT > >* estimator = new doEstimatorUniform< EOT >(); + doEstimator< doNormal< EOT > >* estimator = new doEstimatorNormal< EOT >(); + state.storeFunctor(estimator); + + eoSelectOne< EOT >* selectone = new eoDetTournamentSelect< EOT >(); + state.storeFunctor(selectone); + + //doModifierMass< doUniform< EOT > >* modifier = new doUniformCenter< EOT >(); + doModifierMass< doNormal< EOT > >* modifier = new doNormalCenter< EOT >(); + state.storeFunctor(modifier); + + // EOT min(2, 42); + // EOT max(2, 32); + + //eoEvalFunc< EOT >* plainEval = new BopoRosenbrock< EOT, double, const EOT& >(); + eoEvalFunc< EOT >* plainEval = new Sphere< EOT >(); + state.storeFunctor(plainEval); + + eoEvalFuncCounter< EOT > eval(*plainEval); + + eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5); + //eoRndGenerator< double >* gen = new eoNormalGenerator< double >(0, 1); + state.storeFunctor(gen); + + + unsigned int dimension_size = parser.createParam((unsigned int)10, "dimension-size", "Dimension size", 'd', section).value(); // d + + eoInitFixedLength< EOT >* init = new eoInitFixedLength< EOT >( dimension_size, *gen ); + state.storeFunctor(init); + + + //----------------------------------------------------------------------------- + + + //----------------------------------------------------------------------------- + // (1) Population init and sampler + //----------------------------------------------------------------------------- + + // Generation of population from do_make_pop (creates parameter, manages persistance and so on...) + // ... and creates the parameter letters: L P r S + + // this first sampler creates a uniform distribution independently of our distribution (it doesnot use doUniform). + + eoPop< EOT >& pop = do_make_pop(parser, state, *init); + + //----------------------------------------------------------------------------- + + + //----------------------------------------------------------------------------- + // (2) First evaluation before starting the research algorithm + //----------------------------------------------------------------------------- + + apply(eval, pop); + + //----------------------------------------------------------------------------- + + + //doBounder< EOT >* bounder = new doBounderNo< EOT >(); + doBounder< EOT >* bounder = new doBounderRng< EOT >(EOT(pop[0].size(), -5), + EOT(pop[0].size(), 5), + *gen); + state.storeFunctor(bounder); + + //doSampler< doUniform< EOT > >* sampler = new doSamplerUniform< EOT >(); + doSampler< doNormal< EOT > >* sampler = new doSamplerNormal< EOT >( *bounder ); + state.storeFunctor(sampler); + + + unsigned int rho = parser.createParam((unsigned int)0, "rho", "Rho: metropolis sample size", 'p', section).value(); // p + + moGenSolContinue< EOT >* continuator = new moGenSolContinue< EOT >(rho); + state.storeFunctor(continuator); + + double threshold = parser.createParam((double)0.1, "threshold", "Threshold: temperature threshold stopping criteria", 't', section).value(); // t + double alpha = parser.createParam((double)0.1, "alpha", "Alpha: temperature dicrease rate", 'a', section).value(); // a + + moCoolingSchedule* cooling_schedule = new moGeometricCoolingSchedule(threshold, alpha); + state.storeFunctor(cooling_schedule); + + // stopping criteria + // ... and creates the parameter letters: C E g G s T + + eoContinue< EOT >& monitoringContinue = do_make_continue(parser, state, eval); + + // output + + eoCheckPoint< EOT >& checkpoint = do_make_checkpoint(parser, state, eval, monitoringContinue); + + // appends some missing code to checkpoint + + // eoValueParam& plotPopParam = parser.createParam(false, "plotPop", "Plot sorted pop. every gen.", 0, "Graphical Output"); + + // if (plotPopParam.value()) // we do want plot dump + // { + // eoScalarFitnessStat* fitStat = new eoScalarFitnessStat; + // state.storeFunctor(fitStat); + + // checkpoint.add(*fitStat); + + // eoFileSnapshot* snapshot = new eoFileSnapshot("ResPop"); + // state.storeFunctor(snapshot); + + // snapshot->add(*fitStat); + + // checkpoint.add(*snapshot); + // } + + // -------------------------- + + // eoPopStat< EOT >* popStat = new eoPopStat; + // state.storeFunctor(popStat); + + // checkpoint.add(*popStat); + + // eoMonitor* fileSnapshot = new doFileSnapshot< std::vector< std::string > >("ResPop"); + // state.storeFunctor(fileSnapshot); + + // fileSnapshot->add(*popStat); + // checkpoint.add(*fileSnapshot); + + + //----------------------------------------------------------------------------- + // eoEPRemplacement causes the using of the current and previous + // sample for sampling. + //----------------------------------------------------------------------------- + + eoReplacement< EOT >* replacor = new eoEPReplacement< EOT >(pop.size()); + + // Below, use eoGenerationalReplacement to sample only on the current sample + + //eoReplacement< EOT >* replacor = new eoGenerationalReplacement< EOT >(); // FIXME: to define the size + + state.storeFunctor(replacor); + + //----------------------------------------------------------------------------- + + + //----------------------------------------------------------------------------- + // CMASA algorithm configuration + //----------------------------------------------------------------------------- + + //doAlgo< doUniform< EOT > >* algo = new doCMASA< doUniform< EOT > > + doAlgo< doNormal< EOT > >* algo = new doCMASA< doNormal< EOT > > + (*selector, *estimator, *selectone, *modifier, *sampler, + checkpoint, eval, *continuator, *cooling_schedule, + initial_temperature, *replacor); + + //----------------------------------------------------------------------------- + + + // state.storeFunctor(algo); + + if (parser.userNeedsHelp()) + { + parser.printHelp(std::cout); + exit(1); + } + + // Help + Verbose routines + + make_verbose(parser); + make_help(parser); + + + //----------------------------------------------------------------------------- + // Beginning of the algorithm call + //----------------------------------------------------------------------------- + + try + { + do_run(*algo, pop); + } + catch (std::exception& e) + { + eo::log << eo::errors << "exception: " << e.what() << std::endl; + exit(EXIT_FAILURE); + } + + //----------------------------------------------------------------------------- + + return 0; +} diff --git a/application/cma_sa/plot.py b/application/cma_sa/plot.py new file mode 100755 index 00000000..4fc5924c --- /dev/null +++ b/application/cma_sa/plot.py @@ -0,0 +1,232 @@ +#!/usr/bin/env python + +"""plot.py -- Plot CMA-SA results file""" + +import os, time, math, tempfile +import numpy + +try: + import Gnuplot, Gnuplot.PlotItems, Gnuplot.funcutils +except ImportError: + # kludge in case Gnuplot hasn't been installed as a module yet: + import __init__ + Gnuplot = __init__ + import PlotItems + Gnuplot.PlotItems = PlotItems + import funcutils + Gnuplot.funcutils = funcutils + +def wait(str=None, prompt='Press return to show results...\n'): + if str is not None: + print str + raw_input(prompt) + +def draw2DRect(min=(0,0), max=(1,1), color='black', state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + xmin, ymin = min + xmax, ymax = max + + cmd = 'set arrow from %s,%s to %s,%s nohead lc rgb "%s"' + + g(cmd % (xmin, ymin, xmin, ymax, color)) + g(cmd % (xmin, ymax, xmax, ymax, color)) + g(cmd % (xmax, ymax, xmax, ymin, color)) + g(cmd % (xmax, ymin, xmin, ymin, color)) + + return g + +def draw3DRect(min=(0,0,0), max=(1,1,1), state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + # TODO + + return g + +def getSortedFiles(path): + assert path != None + + filelist = os.listdir(path) + filelist.sort() + + return filelist + +def plotXPointYFitness(path, fields='3:1', state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + g.title('Fitness observation') + g.xlabel('Coordinates') + g.ylabel('Fitness (Quality)') + + files=[] + for filename in getSortedFiles(path): + files.append(Gnuplot.File(path + '/' + filename, using=fields, + with_='points', + title='distribution \'' + filename + '\'')) + + g.plot(*files) + + return g + +def plotXYPointZFitness(path, fields='4:3:1', state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + g.title('Fitness observation in 3-D') + g.xlabel('x-axes') + g.ylabel('y-axes') + g.zlabel('Fitness (Quality)') + + files=[] + for filename in getSortedFiles(path): + files.append(Gnuplot.File(path + '/' + filename, using=fields, + with_='points', + title='distribution \'' + filename + '\'')) + + g.splot(*files) + + return g + +def plotXYPoint(path, fields='3:4', state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + g.title('Points observation in 2-D') + g.xlabel('x-axes') + g.ylabel('y-axes') + + files=[] + for filename in getSortedFiles(path): + files.append(Gnuplot.File(path + '/' + filename, using=fields, + with_='points', + title='distribution \'' + filename + '\'')) + + g.plot(*files) + + return g + +def plotXYZPoint(path, fields='3:4:5', state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + g.title('Points observation in 3-D') + g.xlabel('x-axes') + g.ylabel('y-axes') + g.zlabel('z-axes') + + files=[] + for filename in getSortedFiles(path): + files.append(Gnuplot.File(path + '/' + filename, using=fields, + with_='points', + title='distribution \'' + filename + '\'')) + + g.splot(*files) + + return g + +def plotParams(path, field='1', state=None, g=None): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + g.title('Hyper-volume comparaison through all dimensions') + g.xlabel('Iterations') + g.ylabel('Hyper-volume') + + g.plot(Gnuplot.File(path, with_='lines', using=field, + title='multivariate distribution narrowing')) + + return g + +def plot2DRectFromFiles(path, state=None, g=None, plot=True): + if g == None: g = Gnuplot.Gnuplot() + if state != None: state.append(g) + + g.title('Rectangle drawing observation') + g.xlabel('x-axes') + g.ylabel('y-axes') + + x1,x2,y1,y2 = 0,0,0,0 + + colors = ['red', 'orange', 'blue', 'green', 'gold', 'yellow', 'gray'] + #colors = open('rgb.txt', 'r').readlines() + colors_size = len(colors) + i = 0 # for color + + for filename in getSortedFiles(path): + line = open(path + '/' + filename, 'r').readline() + + fields = line.split(' ') + + if not fields[0] == '2': + print 'plot2DRectFromFiles: higher than 2 dimensions not possible to draw' + return + + xmin,ymin,xmax,ymax = fields[1:5] + #print xmin,ymin,xmax,ymax + + cur_color = colors[i % colors_size] + + draw2DRect((xmin,ymin), (xmax,ymax), cur_color, g=g) + + g('set obj rect from %s,%s to %s,%s back lw 1.0 fc rgb "%s" fillstyle solid 1.00 border -1' + % (xmin,ymin,xmax,ymax,cur_color) + ) + + if plot: + if float(xmin) < x1: x1 = float(xmin) + if float(ymin) < y1: y1 = float(ymin) + if float(xmax) > x2: x2 = float(xmax) + if float(ymax) > y2: y2 = float(ymax) + + #print x1,y1,x2,y2 + + i += 1 + + #print x1,y1,x2,y2 + + if plot: + g.plot('[%s:%s][%s:%s] -9999 notitle' % (x1, x2, y1, y2)) + + return g + +def main(n): + gstate = [] + + if n >= 1: + plotXPointYFitness('./ResPop', state=gstate) + + if n >= 2: + plotXPointYFitness('./ResPop', '4:1', state=gstate) + + if n >= 2: + plotXYPointZFitness('./ResPop', state=gstate) + + if n >= 3: + plotXYZPoint('./ResPop', state=gstate) + + if n >= 1: + plotParams('./ResParams.txt', state=gstate) + + if n >= 2: + plot2DRectFromFiles('./ResBounds', state=gstate) + plotXYPoint('./ResPop', state=gstate) + + g = plot2DRectFromFiles('./ResBounds', state=gstate, plot=False) + plotXYPoint('./ResPop', g=g) + + wait(prompt='Press return to end the plot.\n') + + pass + +# when executed, just run main(): +if __name__ == '__main__': + from sys import argv, exit + + if len(argv) < 2: + print 'Usage: plot [dimension]' + exit() + + main(int(argv[1]))