+ test/t-doEstimatorNormalMulti

This commit is contained in:
Caner Candan 2010-09-10 01:09:16 +02:00
commit e70464630e
5 changed files with 228 additions and 24 deletions

View file

@ -39,6 +39,7 @@ def logger(level_name, filename='plot.log'):
def parser(parser=optparse.OptionParser()):
parser.add_option('-v', '--verbose', choices=LEVELS.keys(), default='warning', help='set a verbose level')
parser.add_option('-f', '--files', help='give some input sample files separated by comma (cf. gen1,gen2,...)', default='')
parser.add_option('-r', '--respop', help='define the population results containing folder', default='./ResPop')
parser.add_option('-o', '--output', help='give an output filename for logging', default='plot.log')
parser.add_option('-d', '--dimension', help='give a dimension size', default=2)
parser.add_option('-m', '--multiplot', action="store_true", help='plot all graphics in one window', dest="multiplot", default=True)
@ -252,6 +253,7 @@ def main():
n = int(options.dimension)
w = int(options.windowid)
r = options.respop
if options.multiplot:
g = Gnuplot.Gnuplot()
@ -270,56 +272,56 @@ def main():
g('set origin 0.0, 0.5')
if n >= 1:
plotXPointYFitness('./ResPop', state=gstate, g=g)
plotXPointYFitness(r, state=gstate, g=g)
g('set size 0.5, 0.5')
g('set origin 0.0, 0.0')
if n >= 2:
plotXPointYFitness('./ResPop', '4:1', state=gstate, g=g)
plotXPointYFitness(r, '4:1', state=gstate, g=g)
g('set size 0.5, 0.5')
g('set origin 0.5, 0.5')
if n >= 2:
plotXYPointZFitness('./ResPop', state=gstate, g=g)
plotXYPointZFitness(r, state=gstate, g=g)
g('set size 0.5, 0.5')
g('set origin 0.5, 0.0')
if n >= 2:
plotXYPoint('./ResPop', state=gstate, g=g)
plotXYPoint(r, state=gstate, g=g)
elif n >= 3:
plotXYZPoint('./ResPop', state=gstate, g=g)
plotXYZPoint(r, state=gstate, g=g)
g('set nomultiplot')
else:
if n >= 1 and w in [0, 1]:
plotXPointYFitness('./ResPop', state=gstate)
plotXPointYFitness(r, state=gstate)
if n >= 2 and w in [0, 2]:
plotXPointYFitness('./ResPop', '4:1', state=gstate)
plotXPointYFitness(r, '4:1', state=gstate)
if n >= 2 and w in [0, 3]:
plotXYPointZFitness('./ResPop', state=gstate)
plotXYPointZFitness(r, state=gstate)
if n >= 3 and w in [0, 4]:
plotXYZPoint('./ResPop', state=gstate)
plotXYZPoint(r, state=gstate)
if n >= 2 and w in [0, 5]:
plotXYPoint('./ResPop', state=gstate)
plotXYPoint(r, state=gstate)
# if n >= 1:
# plotParams('./ResParams.txt', state=gstate)
# if n >= 2:
# plot2DRectFromFiles('./ResBounds', state=gstate)
# plotXYPoint('./ResPop', state=gstate)
# plotXYPoint(r, state=gstate)
# g = plot2DRectFromFiles('./ResBounds', state=gstate, plot=False)
# plotXYPoint('./ResPop', g=g)
# plotXYPoint(r, g=g)
wait(prompt='Press return to end the plot.\n')

View file

@ -154,7 +154,7 @@ int main(int ac, char** av)
//-----------------------------------------------------------------------------
// general output
// population output
//-----------------------------------------------------------------------------
eoCheckPoint< EOT >& pop_continue = do_make_checkpoint(parser, state, eval, eo_continue);
@ -171,14 +171,6 @@ int main(int ac, char** av)
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// population output
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// distribution output
//-----------------------------------------------------------------------------

View file

@ -9,7 +9,7 @@
#define _doEstimatorNormalMulti_h
#include "doEstimator.h"
#include "doUniform.h"
#include "doNormalMulti.h"
template < typename EOT >
class doEstimatorNormalMulti : public doEstimator< doNormalMulti< EOT > >

View file

@ -23,13 +23,24 @@
### 3) Define your targets and link the librairies
######################################################################################
FIND_PACKAGE(Boost 1.33.0)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/application/eda_sa)
SET(SOURCES
t-doEstimatorNormalMulti
)
FOREACH(current ${SOURCES})
ADD_EXECUTABLE(${current} ${current}.cpp)
TARGET_LINK_LIBRARIES(${current} ${PROJECT_NAME} ${EO_LIBRARIES})
ADD_CURRENT(${current} ${current})
ADD_TEST(${current} ${current})
TARGET_LINK_LIBRARIES(${current} do doutils ${EO_LIBRARIES} ${MO_LIBRARIES} ${Boost_LIBRARIES})
INSTALL(TARGETS ${current} RUNTIME DESTINATION share/do/test COMPONENT test)
ENDFOREACH()
######################################################################################

View file

@ -0,0 +1,199 @@
#include <eo>
#include <mo>
#include <utils/eoLogger.h>
#include <utils/eoParserLogger.h>
#include <do>
#include "Rosenbrock.h"
#include "Sphere.h"
typedef eoReal< eoMinimizingFitness > EOT;
typedef doNormalMulti< EOT > Distrib;
typedef EOT::AtomType AtomType;
int main(int ac, char** av)
{
//-----------------------------------------------------
// (0) parser + eo routines
//-----------------------------------------------------
eoParserLogger parser(ac, av);
std::string section("Algorithm parameters");
unsigned int p_size = parser.createParam((unsigned int)100, "popSize", "Population Size", 'P', section).value(); // P
unsigned int s_size = parser.createParam((unsigned int)2, "dimension-size", "Dimension size", 'd', section).value(); // d
AtomType mean_value = parser.createParam((AtomType)0, "mean", "Mean value", 'm', section).value(); // m
AtomType covar1_value = parser.createParam((AtomType)1, "covar1", "Covar value 1", '1', section).value();
AtomType covar2_value = parser.createParam((AtomType)0.5, "covar2", "Covar value 2", '2', section).value();
AtomType covar3_value = parser.createParam((AtomType)1, "covar3", "Covar value 3", '3', section).value();
if (parser.userNeedsHelp())
{
parser.printHelp(std::cout);
exit(1);
}
make_verbose(parser);
make_help(parser);
assert(p_size > 0);
assert(s_size > 0);
eoState state;
//-----------------------------------------------------
//-----------------------------------------------------
// (1) Population init and sampler
//-----------------------------------------------------
eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
state.storeFunctor(gen);
eoInitFixedLength< EOT >* init = new eoInitFixedLength< EOT >( s_size, *gen );
state.storeFunctor(init);
// create an empty pop and let the state handle the memory
// fill population thanks to eoInit instance
eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
//-----------------------------------------------------
//-----------------------------------------------------------------------------
// (2) distribution initial parameters
//-----------------------------------------------------------------------------
ublas::vector< AtomType > mean( s_size, mean_value );
ublas::symmetric_matrix< AtomType, ublas::lower > varcovar( s_size, s_size );
varcovar( 0, 0 ) = covar1_value;
varcovar( 0, 1 ) = covar2_value;
varcovar( 1, 1 ) = covar3_value;
Distrib distrib( mean, varcovar );
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// (3) distribution output
//-----------------------------------------------------------------------------
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 );
(*distribution_continue)( distrib );
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Prepare bounder class to set bounds of sampling.
// This is used by doSampler.
//-----------------------------------------------------------------------------
doBounder< EOT >* bounder = new doBounderRng< EOT >(EOT(pop[0].size(), -5),
EOT(pop[0].size(), 5),
*gen);
state.storeFunctor(bounder);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Prepare sampler class with a specific distribution
//-----------------------------------------------------------------------------
doSampler< Distrib >* sampler = new doSamplerNormalMulti< EOT >( *bounder );
state.storeFunctor(sampler);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// (4) sampling phase
//-----------------------------------------------------------------------------
pop.clear();
for (unsigned int i = 0; i < p_size; ++i)
{
EOT candidate_solution = (*sampler)( distrib );
pop.push_back( candidate_solution );
}
// pop.sort();
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// (5) population output
//-----------------------------------------------------------------------------
eoContinue< EOT >* cont = new eoGenContinue< EOT >( 2 ); // never reached fitness
state.storeFunctor(cont);
eoCheckPoint< EOT >* pop_continue = new eoCheckPoint< EOT >( *cont );
state.storeFunctor(pop_continue);
doPopStat< EOT >* popStat = new doPopStat<EOT>;
state.storeFunctor(popStat);
pop_continue->add(*popStat);
doFileSnapshot* fileSnapshot = new doFileSnapshot("TestResPop");
state.storeFunctor(fileSnapshot);
fileSnapshot->add(*popStat);
pop_continue->add(*fileSnapshot);
(*pop_continue)( pop );
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// (6) estimation phase
//-----------------------------------------------------------------------------
doEstimator< Distrib >* estimator = new doEstimatorNormalMulti< EOT >();
state.storeFunctor(estimator);
distrib = (*estimator)( pop );
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// (7) distribution output
//-----------------------------------------------------------------------------
(*distribution_continue)( distrib );
//-----------------------------------------------------------------------------
return 0;
}