test for stddevest

This commit is contained in:
LPTK 2013-06-13 16:06:14 +02:00
commit ef34af7493
3 changed files with 78 additions and 1 deletions

View file

@ -35,6 +35,7 @@
// general purpose
#include <utils/eoData.h>
#include <eoObject.h>
#include <eoOptional.h>
#include <eoPrintable.h>
#include <eoPersistent.h>
#include <eoScalarFitness.h>

View file

@ -0,0 +1,109 @@
#ifndef __moStdDevEstimator_h__
#define __moStdDevEstimator_h__
#include <eo>
#include <mo>
#include <continuator/moFitnessVarianceStat.h> // TODO rm
#include <iostream> // TODO rm
// TODO make tests
template< class EOT, class Neighbor >
class moStdDevEstimator : public eoUF<EOT&, double>
{
public:
/**
* General constructor for the estimator
* @param continuator a user-defined continuator
* @param neighborhood the neighborhood
* @param fullEval the full evaluation function
* @param eval neighbor's evaluation function
* @param walker a local search algorithm
*/
moStdDevEstimator<EOT,Neighbor> (
moContinuator<Neighbor>& continuator,
moNeighborhood < Neighbor > & neighborhood,
eoEvalFunc<EOT>& fullEval,
/* The following should be read:
moEval<Neighbor>& eval = _default_eval
* (which is not possible to achieve as is in C++) */
const eoOptional< moEval<Neighbor> >& eval = eoOptional< moEval<Neighbor> >::null,
const eoOptional< moLocalSearch<Neighbor> >& walker = eoOptional< moLocalSearch<Neighbor> >::null
)
: _default_eval ( fullEval ),
_eval(eval.hasValue()? eval.get(): _default_eval),
_default_continuator( 0 ),
_continuator( _continuator ),
_checkpoint( _continuator ),
_default_walker( neighborhood, fullEval, _eval, _checkpoint ),
_walker( walker.hasValue()? walker.get(): _default_walker )
{
_checkpoint.add( _varStat );
}
/**
* Simpler constructor for the estimator
* @param max_iters the number of steps the default moIterContinuator should perform
* @param neighborhood the neighborhood
* @param fullEval the full evaluation function
* @param eval neighbor's evaluation function
* @param walker a local search algorithm
*/
moStdDevEstimator<EOT,Neighbor> (
unsigned int max_iters,
moNeighborhood < Neighbor > & neighborhood,
eoEvalFunc<EOT>& fullEval,
const eoOptional< moEval<Neighbor> >& eval = eoOptional< moEval<Neighbor> >::null,
const eoOptional< moLocalSearch<Neighbor> >& walker = eoOptional< moLocalSearch<Neighbor> >::null
)
: _default_eval ( fullEval ),
_eval(eval.hasValue()? eval.get(): _default_eval),
_default_continuator( max_iters, false ),
_continuator( _default_continuator ),
_checkpoint( _continuator ),
_default_walker( neighborhood, fullEval, _eval, _checkpoint ),
_walker( walker.hasValue()? walker.get(): _default_walker )
{
_checkpoint.add( _varStat );
}
/**
* Evaluates the estimator with the walker algorithm and returns the standard deviation
* @param solution the solution from where to start the walk
*/
double operator()( EOT & solution )
{
_walker(solution);
return sqrt(_varStat.value());
}
/**
* @return the class name
*/
virtual std::string className(void) const {
return "moStdDevEstimator";
}
private:
moFullEvalByCopy <Neighbor> _default_eval;
moEval<Neighbor>& _eval;
moIterContinuator <Neighbor> _default_continuator;
moContinuator <Neighbor>& _continuator;
moCheckpoint <Neighbor> _checkpoint;
moRandomWalk <Neighbor> _default_walker;
moLocalSearch <Neighbor> _walker;
moFitnessVarianceStat<EOT> _varStat;
};
#endif // __moStdDevEstimator_h__

View file

@ -0,0 +1,76 @@
//-----------------------------------------------------------------------------
// t-moStdDevEstimator.cpp
//-----------------------------------------------------------------------------
#include <eo>
#include "es/eoReal.h"
#include "continuator/moFitnessVarianceStat.h"
#include "neighborhood/moRealNeighbor.h"
#include "neighborhood/moRealNeighborhood.h"
//Representation and initializer
#include <eoInt.h>
#include <eoInit.h>
#include <eoScalarFitness.h>
// fitness function
#include <eval/queenEval.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
//Neighbors and Neighborhoods
#include <problems/permutation/moShiftNeighbor.h>
#include <neighborhood/moRndWithReplNeighborhood.h>
//Algorithm and its components
#include <coolingSchedule/moCoolingSchedule.h>
#include <algo/moSA.h>
//comparator
#include <comparator/moSolNeighborComparator.h>
#include <sampling/moStdDevEstimator.h>
//-----------------------------------------------------------------------------
// Define types of the representation solution, different neighbors and neighborhoods
//-----------------------------------------------------------------------------
typedef eoInt<eoMinimizingFitness> Queen; //Permutation (Queen's problem representation)
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
typedef moRndWithReplNeighborhood<shiftNeighbor> rndShiftNeighborhood; //rnd shift Neighborhood (Indexed)
//-----------------------------------------------------------------------------
typedef eoReal< eoMinimizingFitness > EOT;
typedef moRealNeighbor< EOT > Neighbor;
int main(int ac, char** av)
{
unsigned vecSize = 8;
queenEval<Queen> fullEval;
eoInitPermutation<Queen> init(vecSize);
//moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval); /// by default
rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1));
Queen solution;
init(solution);
fullEval(solution);
moStdDevEstimator<Queen, shiftNeighbor> initTemp (500, rndShiftNH, fullEval);
std::cout << "temp: " << initTemp(solution) << std::endl;
//assert(stat.value() == 625);
}