* cma-sa name has been replace by eda-sa in the project
This commit is contained in:
parent
a70a083735
commit
a84417d349
9 changed files with 17 additions and 17 deletions
|
|
@ -2,6 +2,6 @@
|
||||||
### 1) Where do we go now ?!?
|
### 1) Where do we go now ?!?
|
||||||
######################################################################################
|
######################################################################################
|
||||||
|
|
||||||
ADD_SUBDIRECTORY(cma_sa)
|
ADD_SUBDIRECTORY(eda_sa)
|
||||||
|
|
||||||
######################################################################################
|
######################################################################################
|
||||||
|
|
|
||||||
28
application/eda_sa/CMakeLists.txt
Normal file
28
application/eda_sa/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
PROJECT(eda_sa)
|
||||||
|
|
||||||
|
FIND_PACKAGE(Boost 1.33.0)
|
||||||
|
|
||||||
|
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
|
||||||
|
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
|
||||||
|
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
|
||||||
|
|
||||||
|
SET(RESOURCES
|
||||||
|
eda_sa.param
|
||||||
|
plot.py
|
||||||
|
)
|
||||||
|
|
||||||
|
FOREACH(file ${RESOURCES})
|
||||||
|
EXECUTE_PROCESS(
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/${file}
|
||||||
|
${DO_BINARY_DIR}/${file}
|
||||||
|
)
|
||||||
|
ENDFOREACH(file)
|
||||||
|
|
||||||
|
FILE(GLOB SOURCES *.cpp)
|
||||||
|
|
||||||
|
SET(EXECUTABLE_OUTPUT_PATH ${DO_BINARY_DIR})
|
||||||
|
|
||||||
|
ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES})
|
||||||
|
TARGET_LINK_LIBRARIES(${PROJECT_NAME} do ${EO_LIBRARIES} ${MO_LIBRARIES} ${Boost_LIBRARIES})
|
||||||
42
application/eda_sa/Rosenbrock.h
Normal file
42
application/eda_sa/Rosenbrock.h
Normal 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
|
||||||
42
application/eda_sa/Sphere.h
Normal file
42
application/eda_sa/Sphere.h
Normal 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
|
||||||
7
application/eda_sa/eda_sa.param
Normal file
7
application/eda_sa/eda_sa.param
Normal 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
|
||||||
243
application/eda_sa/main.cpp
Normal file
243
application/eda_sa/main.cpp
Normal file
|
|
@ -0,0 +1,243 @@
|
||||||
|
#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 <do>
|
||||||
|
|
||||||
|
#include "Rosenbrock.h"
|
||||||
|
#include "Sphere.h"
|
||||||
|
|
||||||
|
typedef eoReal<eoMinimizingFitness> EOT;
|
||||||
|
|
||||||
|
//typedef doUniform< EOT > Distrib;
|
||||||
|
//typedef doNormalMono< EOT > Distrib;
|
||||||
|
typedef doNormalMulti< 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 EDASA algorithm
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
eoSelect< EOT >* selector = new eoDetSelect< EOT >(0.5);
|
||||||
|
state.storeFunctor(selector);
|
||||||
|
|
||||||
|
doEstimator< Distrib >* estimator =
|
||||||
|
//new doEstimatorUniform< EOT >();
|
||||||
|
//new doEstimatorNormalMono< EOT >();
|
||||||
|
new doEstimatorNormalMulti< EOT >();
|
||||||
|
state.storeFunctor(estimator);
|
||||||
|
|
||||||
|
eoSelectOne< EOT >* selectone = new eoDetTournamentSelect< EOT >( 2 );
|
||||||
|
state.storeFunctor(selectone);
|
||||||
|
|
||||||
|
doModifierMass< Distrib >* modifier =
|
||||||
|
//new doUniformCenter< EOT >();
|
||||||
|
//new doNormalMonoCenter< EOT >();
|
||||||
|
new doNormalMultiCenter< EOT >();
|
||||||
|
state.storeFunctor(modifier);
|
||||||
|
|
||||||
|
eoEvalFunc< EOT >* plainEval =
|
||||||
|
//new BopoRosenbrock< EOT, typename EOT::AtomType, const EOT& >();
|
||||||
|
new Sphere< EOT >();
|
||||||
|
state.storeFunctor(plainEval);
|
||||||
|
|
||||||
|
unsigned long max_eval = parser.getORcreateParam((unsigned long)0, "maxEval", "Maximum number of evaluations (0 = none)", 'E', "Stopping criterion").value(); // E
|
||||||
|
eoEvalFuncCounter< EOT > eval(*plainEval, max_eval);
|
||||||
|
|
||||||
|
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< Distrib >* sampler =
|
||||||
|
//new doSamplerUniform< EOT >();
|
||||||
|
//new doSamplerNormalMono< EOT >( *bounder );
|
||||||
|
new doSamplerNormalMulti< EOT >( *bounder );
|
||||||
|
state.storeFunctor(sampler);
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int rho = parser.createParam((unsigned int)0, "rho", "Rho: metropolis sample size", 'p', section).value(); // p
|
||||||
|
|
||||||
|
moGenSolContinue< EOT >* sa_continue = new moGenSolContinue< EOT >(rho);
|
||||||
|
state.storeFunctor(sa_continue);
|
||||||
|
|
||||||
|
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 >& eo_continue = do_make_continue(parser, state, eval);
|
||||||
|
|
||||||
|
// output
|
||||||
|
|
||||||
|
eoCheckPoint< EOT >& monitoring_continue = do_make_checkpoint(parser, state, eval, eo_continue);
|
||||||
|
|
||||||
|
// eoPopStat< EOT >* popStat = new eoPopStat<EOT>;
|
||||||
|
// state.storeFunctor(popStat);
|
||||||
|
|
||||||
|
// checkpoint.add(*popStat);
|
||||||
|
|
||||||
|
// eoGnuplot1DMonitor* gnuplot = new eoGnuplot1DMonitor("gnuplot.txt");
|
||||||
|
// state.storeFunctor(gnuplot);
|
||||||
|
|
||||||
|
// gnuplot->add(eval);
|
||||||
|
// gnuplot->add(*popStat);
|
||||||
|
|
||||||
|
//gnuplot->gnuplotCommand("set yrange [0:500]");
|
||||||
|
|
||||||
|
// checkpoint.add(*gnuplot);
|
||||||
|
|
||||||
|
// eoMonitor* fileSnapshot = new doFileSnapshot< std::vector< std::string > >("ResPop");
|
||||||
|
// state.storeFunctor(fileSnapshot);
|
||||||
|
|
||||||
|
// fileSnapshot->add(*popStat);
|
||||||
|
// checkpoint.add(*fileSnapshot);
|
||||||
|
|
||||||
|
|
||||||
|
doDummyContinue< Distrib >* dummy_continue = new doDummyContinue< Distrib >();
|
||||||
|
state.storeFunctor(dummy_continue);
|
||||||
|
|
||||||
|
doCheckPoint< Distrib >* distribution_continue = new doCheckPoint< Distrib >( *dummy_continue );
|
||||||
|
state.storeFunctor(distribution_continue);
|
||||||
|
|
||||||
|
doDistribStat< Distrib >* distrib_stat = new doStatNormalMulti< EOT >();
|
||||||
|
state.storeFunctor(distrib_stat);
|
||||||
|
|
||||||
|
distribution_continue->add( *distrib_stat );
|
||||||
|
|
||||||
|
// eoMonitor* stdout_monitor = new eoStdoutMonitor();
|
||||||
|
// state.storeFunctor(stdout_monitor);
|
||||||
|
// stdout_monitor->add(*distrib_stat);
|
||||||
|
// distribution_continue->add( *stdout_monitor );
|
||||||
|
|
||||||
|
eoFileMonitor* file_monitor = new eoFileMonitor("distribution_bounds.txt");
|
||||||
|
state.storeFunctor(file_monitor);
|
||||||
|
file_monitor->add(*distrib_stat);
|
||||||
|
distribution_continue->add( *file_monitor );
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// EDASA algorithm configuration
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
doAlgo< Distrib >* algo = new doEDASA< Distrib >
|
||||||
|
(*selector, *estimator, *selectone, *modifier, *sampler,
|
||||||
|
monitoring_continue, *distribution_continue,
|
||||||
|
eval, *sa_continue, *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 (eoReachedThresholdException& e)
|
||||||
|
{
|
||||||
|
eo::log << eo::warnings << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
eo::log << eo::errors << "exception: " << e.what() << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
232
application/eda_sa/plot.py
Normal file
232
application/eda_sa/plot.py
Normal file
|
|
@ -0,0 +1,232 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""plot.py -- Plot EDA-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]))
|
||||||
2
src/do
2
src/do
|
|
@ -9,7 +9,7 @@
|
||||||
#define _do_
|
#define _do_
|
||||||
|
|
||||||
#include "doAlgo.h"
|
#include "doAlgo.h"
|
||||||
#include "doCMASA.h"
|
#include "doEDASA.h"
|
||||||
|
|
||||||
#include "doDistrib.h"
|
#include "doDistrib.h"
|
||||||
#include "doUniform.h"
|
#include "doUniform.h"
|
||||||
|
|
|
||||||
445
src/doEDASA.h
Normal file
445
src/doEDASA.h
Normal file
|
|
@ -0,0 +1,445 @@
|
||||||
|
// (c) Thales group, 2010
|
||||||
|
/*
|
||||||
|
Authors:
|
||||||
|
Johann Dreo <johann.dreo@thalesgroup.com>
|
||||||
|
Caner Candan <caner.candan@thalesgroup.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _doEDASA_h
|
||||||
|
#define _doEDASA_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Temporary solution to store populations state at each iteration for plotting.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// system header inclusion
|
||||||
|
#include <cstdlib>
|
||||||
|
// end system header inclusion
|
||||||
|
|
||||||
|
// mkdir headers inclusion
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
// end mkdir headers inclusion
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
#include <boost/numeric/ublas/vector.hpp>
|
||||||
|
#include <boost/numeric/ublas/io.hpp>
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <eo>
|
||||||
|
#include <mo>
|
||||||
|
|
||||||
|
#include <utils/eoRNG.h>
|
||||||
|
|
||||||
|
#include "doAlgo.h"
|
||||||
|
#include "doEstimator.h"
|
||||||
|
#include "doModifierMass.h"
|
||||||
|
#include "doSampler.h"
|
||||||
|
#include "doHyperVolume.h"
|
||||||
|
#include "doStat.h"
|
||||||
|
#include "doContinue.h"
|
||||||
|
|
||||||
|
using namespace boost::numeric::ublas;
|
||||||
|
|
||||||
|
template < typename D >
|
||||||
|
class doEDASA : public doAlgo< D >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//! Alias for the type EOT
|
||||||
|
typedef typename D::EOType EOT;
|
||||||
|
|
||||||
|
//! Alias for the atom type
|
||||||
|
typedef typename EOT::AtomType AtomType;
|
||||||
|
|
||||||
|
//! Alias for the fitness
|
||||||
|
typedef typename EOT::Fitness Fitness;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! doEDASA constructor
|
||||||
|
/*!
|
||||||
|
All the boxes used by a EDASA need to be given.
|
||||||
|
|
||||||
|
\param selector The EOT selector
|
||||||
|
\param estomator The EOT selector
|
||||||
|
\param selectone SelectOne
|
||||||
|
\param modifier The D modifier
|
||||||
|
\param sampler The D sampler
|
||||||
|
\param evaluation The evaluation function.
|
||||||
|
\param continue The stopping criterion.
|
||||||
|
\param cooling_schedule The cooling schedule, describes how the temperature is modified.
|
||||||
|
\param initial_temperature The initial temperature.
|
||||||
|
\param replacor The EOT replacor
|
||||||
|
*/
|
||||||
|
doEDASA (eoSelect< EOT > & selector,
|
||||||
|
doEstimator< D > & estimator,
|
||||||
|
eoSelectOne< EOT > & selectone,
|
||||||
|
doModifierMass< D > & modifier,
|
||||||
|
doSampler< D > & sampler,
|
||||||
|
eoContinue< EOT > & monitoring_continue,
|
||||||
|
doContinue< D > & distribution_continue,
|
||||||
|
eoEvalFunc < EOT > & evaluation,
|
||||||
|
moSolContinue < EOT > & sa_continue,
|
||||||
|
moCoolingSchedule & cooling_schedule,
|
||||||
|
double initial_temperature,
|
||||||
|
eoReplacement< EOT > & replacor
|
||||||
|
)
|
||||||
|
: _selector(selector),
|
||||||
|
_estimator(estimator),
|
||||||
|
_selectone(selectone),
|
||||||
|
_modifier(modifier),
|
||||||
|
_sampler(sampler),
|
||||||
|
_monitoring_continue(monitoring_continue),
|
||||||
|
_distribution_continue(distribution_continue),
|
||||||
|
_evaluation(evaluation),
|
||||||
|
_sa_continue(sa_continue),
|
||||||
|
_cooling_schedule(cooling_schedule),
|
||||||
|
_initial_temperature(initial_temperature),
|
||||||
|
_replacor(replacor),
|
||||||
|
|
||||||
|
_pop_results_destination("ResPop")//,
|
||||||
|
|
||||||
|
// directory where populations state are going to be stored.
|
||||||
|
// _ofs_params("ResParams.txt"),
|
||||||
|
// _ofs_params_var("ResVars.txt"),
|
||||||
|
|
||||||
|
// _bounds_results_destination("ResBounds")
|
||||||
|
{
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution to store populations state at each
|
||||||
|
// iteration for plotting.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
{
|
||||||
|
std::stringstream os;
|
||||||
|
os << "rm -rf " << _pop_results_destination;
|
||||||
|
::system(os.str().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
::mkdir(_pop_results_destination.c_str(), 0755); // create a first time
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution to store bounds values for each distribution.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
// {
|
||||||
|
// std::stringstream os;
|
||||||
|
// os << "rm -rf " << _bounds_results_destination;
|
||||||
|
// ::system(os.str().c_str());
|
||||||
|
// }
|
||||||
|
|
||||||
|
//::mkdir(_bounds_results_destination.c_str(), 0755); // create once directory
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//! function that launches the EDASA algorithm.
|
||||||
|
/*!
|
||||||
|
As a moTS or a moHC, the EDASA can be used for HYBRIDATION in an evolutionary algorithm.
|
||||||
|
|
||||||
|
\param pop A population to improve.
|
||||||
|
\return TRUE.
|
||||||
|
*/
|
||||||
|
//bool operator ()(eoPop< EOT > & pop)
|
||||||
|
void operator ()(eoPop< EOT > & pop)
|
||||||
|
{
|
||||||
|
assert(pop.size() > 0);
|
||||||
|
|
||||||
|
double temperature = _initial_temperature;
|
||||||
|
|
||||||
|
eoPop< EOT > current_pop = pop;
|
||||||
|
|
||||||
|
eoPop< EOT > selected_pop;
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution used by plot to enumerate iterations in
|
||||||
|
// several files.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
int number_of_iterations = 0;
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Estimating a first time the distribution parameter thanks
|
||||||
|
// to population.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
D distrib = _estimator(pop);
|
||||||
|
|
||||||
|
double size = distrib.size();
|
||||||
|
assert(size > 0);
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// {
|
||||||
|
// D distrib = _estimator(pop);
|
||||||
|
|
||||||
|
// double size = distrib.size();
|
||||||
|
// assert(size > 0);
|
||||||
|
|
||||||
|
// doHyperVolume< EOT > hv;
|
||||||
|
|
||||||
|
// for (int i = 0; i < size; ++i)
|
||||||
|
// {
|
||||||
|
// //hv.update( distrib.varcovar()[i] );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // _ofs_params_var << hv << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (pop != current_pop)
|
||||||
|
{
|
||||||
|
_replacor(pop, current_pop);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_pop.clear();
|
||||||
|
selected_pop.clear();
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// (3) Selection of the best points in the population
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
_selector(pop, selected_pop);
|
||||||
|
|
||||||
|
assert( selected_pop.size() > 0 );
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
_sa_continue.init();
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// (4) Estimation of the distribution parameters
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
distrib = _estimator(selected_pop);
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: utiliser selected_pop ou pop ???
|
||||||
|
|
||||||
|
assert(selected_pop.size() > 0);
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Init of a variable contening a point with the bestest fitnesses
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
EOT current_solution = _selectone(selected_pop);
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Fit the current solution with the distribution parameters (bounds)
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
// FIXME: si besoin de modifier la dispersion de la distribution
|
||||||
|
// _modifier_dispersion(distribution, selected_pop);
|
||||||
|
_modifier(distrib, current_solution);
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Building of the sampler in current_pop
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
EOT candidate_solution = _sampler(distrib);
|
||||||
|
|
||||||
|
EOT& e1 = candidate_solution;
|
||||||
|
_evaluation( e1 );
|
||||||
|
EOT& e2 = current_solution;
|
||||||
|
_evaluation( e2 );
|
||||||
|
|
||||||
|
// TODO: verifier le critere d'acceptation
|
||||||
|
if ( e1.fitness() < e2.fitness() ||
|
||||||
|
rng.uniform() < exp( ::fabs(e1.fitness() - e2.fitness()) / temperature ) )
|
||||||
|
{
|
||||||
|
current_pop.push_back(candidate_solution);
|
||||||
|
current_solution = candidate_solution;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while ( _sa_continue( current_solution) );
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution to store populations state
|
||||||
|
// at each iteration for plotting.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
{
|
||||||
|
std::ostringstream os;
|
||||||
|
os << _pop_results_destination << "/" << number_of_iterations;
|
||||||
|
std::ofstream ofs(os.str().c_str());
|
||||||
|
ofs << current_pop;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution used by plot to enumerate iterations in
|
||||||
|
// several files.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
// {
|
||||||
|
// double size = distrib.size();
|
||||||
|
|
||||||
|
// assert(size > 0);
|
||||||
|
|
||||||
|
// std::stringstream os;
|
||||||
|
// os << _bounds_results_destination << "/" << number_of_iterations;
|
||||||
|
// std::ofstream ofs(os.str().c_str());
|
||||||
|
|
||||||
|
// ofs << size << " ";
|
||||||
|
//ublas::vector< AtomType > mean = distrib.mean();
|
||||||
|
//std::copy(mean.begin(), mean.end(), std::ostream_iterator< double >(ofs, " "));
|
||||||
|
//std::copy(distrib.varcovar().begin(), distrib.varcovar().end(), std::ostream_iterator< double >(ofs, " "));
|
||||||
|
// ofs << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary saving to get a proof for distribution bounds
|
||||||
|
// dicreasement
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
// {
|
||||||
|
// double size = distrib.size();
|
||||||
|
// assert(size > 0);
|
||||||
|
|
||||||
|
// vector< double > vmin(size);
|
||||||
|
// vector< double > vmax(size);
|
||||||
|
|
||||||
|
// std::copy(distrib.param(0).begin(), distrib.param(0).end(), vmin.begin());
|
||||||
|
// std::copy(distrib.param(1).begin(), distrib.param(1).end(), vmax.begin());
|
||||||
|
|
||||||
|
// vector< double > vrange = vmax - vmin;
|
||||||
|
|
||||||
|
// doHyperVolume hv;
|
||||||
|
|
||||||
|
// for (int i = 0, size = vrange.size(); i < size; ++i)
|
||||||
|
// {
|
||||||
|
// hv.update( vrange(i) );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ofs_params << hv << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary saving to get a proof for distribution bounds
|
||||||
|
// dicreasement
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
// {
|
||||||
|
// double size = distrib.size();
|
||||||
|
// assert(size > 0);
|
||||||
|
|
||||||
|
// doHyperVolume< EOT > hv;
|
||||||
|
|
||||||
|
// for (int i = 0; i < size; ++i)
|
||||||
|
// {
|
||||||
|
// //hv.update( distrib.varcovar()[i] );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //_ofs_params_var << hv << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
++number_of_iterations;
|
||||||
|
|
||||||
|
}
|
||||||
|
while ( _cooling_schedule( temperature ) &&
|
||||||
|
_distribution_continue( distrib ) &&
|
||||||
|
_monitoring_continue( selected_pop )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
//! A EOT selector
|
||||||
|
eoSelect < EOT > & _selector;
|
||||||
|
|
||||||
|
//! A EOT estimator. It is going to estimate distribution parameters.
|
||||||
|
doEstimator< D > & _estimator;
|
||||||
|
|
||||||
|
//! SelectOne
|
||||||
|
eoSelectOne< EOT > & _selectone;
|
||||||
|
|
||||||
|
//! A D modifier
|
||||||
|
doModifierMass< D > & _modifier;
|
||||||
|
|
||||||
|
//! A D sampler
|
||||||
|
doSampler< D > & _sampler;
|
||||||
|
|
||||||
|
//! A EOT monitoring continuator
|
||||||
|
eoContinue < EOT > & _monitoring_continue;
|
||||||
|
|
||||||
|
//! A D continuator
|
||||||
|
doContinue < D > & _distribution_continue;
|
||||||
|
|
||||||
|
//! A full evaluation function.
|
||||||
|
eoEvalFunc < EOT > & _evaluation;
|
||||||
|
|
||||||
|
//! Stopping criterion before temperature update
|
||||||
|
moSolContinue < EOT > & _sa_continue;
|
||||||
|
|
||||||
|
//! The cooling schedule
|
||||||
|
moCoolingSchedule & _cooling_schedule;
|
||||||
|
|
||||||
|
//! Initial temperature
|
||||||
|
double _initial_temperature;
|
||||||
|
|
||||||
|
//! A EOT replacor
|
||||||
|
eoReplacement < EOT > & _replacor;
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution to store populations state at each
|
||||||
|
// iteration for plotting.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
std::string _pop_results_destination;
|
||||||
|
// std::ofstream _ofs_params;
|
||||||
|
// std::ofstream _ofs_params_var;
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
// Temporary solution to store bounds values for each distribution.
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
// std::string _bounds_results_destination;
|
||||||
|
|
||||||
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // !_doEDASA_h
|
||||||
Reference in a new issue