+ cma_sa application

This commit is contained in:
Caner Candan 2010-07-05 19:06:34 +02:00
commit 8ba39921fa
6 changed files with 591 additions and 0 deletions

View file

@ -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}
)

View file

@ -0,0 +1,42 @@
#ifndef _Rosenbrock_h
#define _Rosenbrock_h
#include <eo>
#include <es.h>
#include <es/eoRealInitBounded.h>
#include <es/eoRealOp.h>
#include <es/eoEsChromInit.h>
#include <es/eoRealOp.h>
#include <es/make_real.h>
#include <apply.h>
#include <eoProportionalCombinedOp.h>
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

View file

@ -0,0 +1,42 @@
#ifndef _Sphere_h
#define _Sphere_h
#include <eo>
#include <es.h>
#include <es/eoRealInitBounded.h>
#include <es/eoRealOp.h>
#include <es/eoEsChromInit.h>
#include <es/eoRealOp.h>
#include <es/make_real.h>
#include <apply.h>
#include <eoProportionalCombinedOp.h>
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

View file

@ -0,0 +1,7 @@
--rho=0 # -p : <etropolis sample size
--alpha=0 # -a : Temperature dicrease rate
--threshold=0.1 # -t : Temperature threshold stopping criteria
--sample-size=10 # -P : Sample size
--dimension-size=10 # -d : Dimension size
--temperature=100 # -T : Initial temperature
#--verbose # Enable verbose mode

233
application/cma_sa/main.cpp Normal file
View file

@ -0,0 +1,233 @@
// #include <boost/numeric/ublas/matrix.hpp>
// #include <boost/numeric/ublas/io.hpp>
#ifndef HAVE_GNUPLOT
// FIXME: temporary define to force use of gnuplot without compiling
// again EO.
# define HAVE_GNUPLOT
#endif
#include <eo>
#include <mo>
#include <utils/eoLogger.h>
#include <utils/eoParserLogger.h>
#include <do/make_pop.h>
#include <do/make_run.h>
#include <do/make_continue.h>
#include <do/make_checkpoint.h>
//#include "BopoRosenbrock.h"
#include "Rosenbrock.h"
#include "Sphere.h"
#include <do>
typedef eoReal<eoMinimizingFitness> 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<bool>& plotPopParam = parser.createParam(false, "plotPop", "Plot sorted pop. every gen.", 0, "Graphical Output");
// if (plotPopParam.value()) // we do want plot dump
// {
// eoScalarFitnessStat<EOT>* fitStat = new eoScalarFitnessStat<EOT>;
// 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<EOT>;
// 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;
}

232
application/cma_sa/plot.py Executable file
View file

@ -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]))