+ do files
This commit is contained in:
parent
d55182a8b0
commit
a038586edb
27 changed files with 1439 additions and 0 deletions
35
src/CMakeLists.txt
Normal file
35
src/CMakeLists.txt
Normal 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}
|
||||
)
|
||||
36
src/do
Normal file
36
src/do
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef _do_
|
||||
#define _do_
|
||||
|
||||
#include "doAlgo.h"
|
||||
#include "doCMASA.h"
|
||||
|
||||
#include "doDistrib.h"
|
||||
#include "doUniform.h"
|
||||
#include "doNormal.h"
|
||||
|
||||
#include "doEstimator.h"
|
||||
#include "doEstimatorUniform.h"
|
||||
#include "doEstimatorNormal.h"
|
||||
|
||||
#include "doModifier.h"
|
||||
#include "doModifierDispersion.h"
|
||||
#include "doModifierMass.h"
|
||||
#include "doUniformCenter.h"
|
||||
#include "doNormalCenter.h"
|
||||
|
||||
#include "doSampler.h"
|
||||
#include "doSamplerUniform.h"
|
||||
#include "doSamplerNormal.h"
|
||||
|
||||
#include "doDistribParams.h"
|
||||
#include "doVectorBounds.h"
|
||||
#include "doNormalParams.h"
|
||||
|
||||
#include "doBounder.h"
|
||||
#include "doBounderNo.h"
|
||||
#include "doBounderBound.h"
|
||||
#include "doBounderRng.h"
|
||||
|
||||
#include "doStats.h"
|
||||
|
||||
#endif // !_do_
|
||||
16
src/doAlgo.h
Normal file
16
src/doAlgo.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _doAlgo_h
|
||||
#define _doAlgo_h
|
||||
|
||||
#include <eoAlgo.h>
|
||||
|
||||
template < typename D >
|
||||
class doAlgo : public eoAlgo< typename D::EOType >
|
||||
{
|
||||
//! Alias for the type
|
||||
typedef typename D::EOType EOT;
|
||||
|
||||
public:
|
||||
virtual ~doAlgo(){}
|
||||
};
|
||||
|
||||
#endif // !_doAlgo_h
|
||||
27
src/doBounder.h
Normal file
27
src/doBounder.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef _doBounder_h
|
||||
#define _doBounder_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
|
||||
template < typename EOT >
|
||||
class doBounder : public eoUF< EOT&, void >
|
||||
{
|
||||
public:
|
||||
doBounder( EOT min = -5, EOT max = 5 )
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
assert(_min.size() > 0);
|
||||
assert(_min.size() == _max.size());
|
||||
}
|
||||
|
||||
// virtual void operator()( EOT& ) = 0 (provided by eoUF< A1, R >)
|
||||
|
||||
EOT& min(){return _min;}
|
||||
EOT& max(){return _max;}
|
||||
|
||||
private:
|
||||
EOT _min;
|
||||
EOT _max;
|
||||
};
|
||||
|
||||
#endif // !_doBounder_h
|
||||
35
src/doBounderBound.h
Normal file
35
src/doBounderBound.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef _doBounderBound_h
|
||||
#define _doBounderBound_h
|
||||
|
||||
#include "doBounder.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doBounderBound : public doBounder< EOT >
|
||||
{
|
||||
public:
|
||||
doBounderBound( EOT min, EOT max )
|
||||
: doBounder< EOT >( min, max )
|
||||
{}
|
||||
|
||||
void operator()( EOT& x )
|
||||
{
|
||||
unsigned int size = x.size();
|
||||
assert(size > 0);
|
||||
|
||||
for (unsigned int d = 0; d < size; ++d) // browse all dimensions
|
||||
{
|
||||
if (x[d] < this->min()[d])
|
||||
{
|
||||
x[d] = this->min()[d];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (x[d] > this->max()[d])
|
||||
{
|
||||
x[d] = this->max()[d];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doBounderBound_h
|
||||
14
src/doBounderNo.h
Normal file
14
src/doBounderNo.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef _doBounderNo_h
|
||||
#define _doBounderNo_h
|
||||
|
||||
#include "doBounder.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doBounderNo : public doBounder< EOT >
|
||||
{
|
||||
public:
|
||||
void operator()( EOT& x )
|
||||
{}
|
||||
};
|
||||
|
||||
#endif // !_doBounderNo_h
|
||||
35
src/doBounderRng.h
Normal file
35
src/doBounderRng.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef _doBounderRng_h
|
||||
#define _doBounderRng_h
|
||||
|
||||
#include "doBounder.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doBounderRng : public doBounder< EOT >
|
||||
{
|
||||
public:
|
||||
doBounderRng( EOT min, EOT max, eoRndGenerator< double > & rng )
|
||||
: doBounder< EOT >( min, max ), _rng(rng)
|
||||
{}
|
||||
|
||||
void operator()( EOT& x )
|
||||
{
|
||||
unsigned int size = x.size();
|
||||
assert(size > 0);
|
||||
|
||||
for (unsigned int d = 0; d < size; ++d) // browse all dimensions
|
||||
{
|
||||
|
||||
// FIXME: attention: les bornes RNG ont les memes bornes quelque soit les dimensions idealement on voudrait avoir des bornes differentes pour chaque dimensions.
|
||||
|
||||
if (x[d] < this->min()[d] || x[d] > this->max()[d])
|
||||
{
|
||||
x[d] = _rng();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
eoRndGenerator< double> & _rng;
|
||||
};
|
||||
|
||||
#endif // !_doBounderRng_h
|
||||
395
src/doCMASA.h
Normal file
395
src/doCMASA.h
Normal file
|
|
@ -0,0 +1,395 @@
|
|||
#ifndef _doCMASA_h
|
||||
#define _doCMASA_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"
|
||||
|
||||
using namespace boost::numeric::ublas;
|
||||
|
||||
template < typename D >
|
||||
class doCMASA : public doAlgo< D >
|
||||
{
|
||||
public:
|
||||
//! Alias for the type
|
||||
typedef typename D::EOType EOT;
|
||||
|
||||
//! Alias for the fitness
|
||||
typedef typename EOT::Fitness Fitness;
|
||||
|
||||
public:
|
||||
|
||||
//! doCMASA constructor
|
||||
/*!
|
||||
All the boxes used by a CMASA 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
|
||||
*/
|
||||
doCMASA (eoSelect< EOT > & selector,
|
||||
doEstimator< D > & estimator,
|
||||
eoSelectOne< EOT > & selectone,
|
||||
doModifierMass< D > & modifier,
|
||||
doSampler< D > & sampler,
|
||||
eoContinue< EOT > & monitoring_continue,
|
||||
eoEvalFunc < EOT > & evaluation,
|
||||
moSolContinue < EOT > & continuator,
|
||||
moCoolingSchedule & cooling_schedule,
|
||||
double initial_temperature,
|
||||
eoReplacement< EOT > & replacor
|
||||
)
|
||||
: _selector(selector),
|
||||
_estimator(estimator),
|
||||
_selectone(selectone),
|
||||
_modifier(modifier),
|
||||
_sampler(sampler),
|
||||
_monitoring_continue(monitoring_continue),
|
||||
_evaluation(evaluation),
|
||||
_continuator(continuator),
|
||||
_cooling_schedule(cooling_schedule),
|
||||
_initial_temperature(initial_temperature),
|
||||
_replacor(replacor)
|
||||
{}
|
||||
|
||||
//! function that launches the CMASA algorithm.
|
||||
/*!
|
||||
As a moTS or a moHC, the CMASA 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;
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Temporary solution to store populations state at each
|
||||
// iteration for plotting.
|
||||
//-------------------------------------------------------------
|
||||
|
||||
std::string pop_results_destination("ResPop");
|
||||
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "rm -rf " << pop_results_destination;
|
||||
::system(ss.str().c_str());
|
||||
}
|
||||
|
||||
::mkdir(pop_results_destination.c_str(), 0755); // create a first time the
|
||||
// directory where populations state are going to be stored.
|
||||
std::ofstream ofs_params("ResParams.txt");
|
||||
std::ofstream ofs_params_var("ResVars.txt");
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Temporary solution to store bounds valeur for each distribution.
|
||||
//-------------------------------------------------------------
|
||||
|
||||
std::string bounds_results_destination("ResBounds");
|
||||
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "rm -rf " << bounds_results_destination;
|
||||
::system(ss.str().c_str());
|
||||
}
|
||||
|
||||
::mkdir(bounds_results_destination.c_str(), 0755); // create a first time the
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
{
|
||||
D distrib = _estimator(pop);
|
||||
|
||||
double size = distrib.size();
|
||||
|
||||
assert(size > 0);
|
||||
|
||||
double vacc = 1;
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
vacc *= sqrt(distrib.variance()[i]);
|
||||
}
|
||||
|
||||
assert(vacc <= std::numeric_limits<double>::max());
|
||||
|
||||
ofs_params_var << vacc << 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);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
_continuator.init();
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// (4) Estimation of the distribution parameters
|
||||
//-------------------------------------------------------------
|
||||
|
||||
D 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 ( _continuator( current_solution) );
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Temporary solution to store populations state
|
||||
// at each iteration for plotting.
|
||||
//-------------------------------------------------------------
|
||||
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << pop_results_destination << "/" << number_of_iterations;
|
||||
std::ofstream ofs(ss.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 ss;
|
||||
ss << bounds_results_destination << "/" << number_of_iterations;
|
||||
std::ofstream ofs(ss.str().c_str());
|
||||
|
||||
ofs << size << " ";
|
||||
std::copy(distrib.param(0).begin(), distrib.param(0).end(), std::ostream_iterator< double >(ofs, " "));
|
||||
std::copy(distrib.param(1).begin(), distrib.param(1).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;
|
||||
|
||||
// double vacc = 1;
|
||||
|
||||
// for (int i = 0, size = vrange.size(); i < size; ++i)
|
||||
// {
|
||||
// vacc *= vrange(i);
|
||||
// }
|
||||
|
||||
// assert(vacc <= std::numeric_limits<double>::max());
|
||||
|
||||
// ofs_params << vacc << std::endl;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Temporary saving to get a proof for distribution bounds
|
||||
// dicreasement
|
||||
//-------------------------------------------------------------
|
||||
|
||||
{
|
||||
double size = distrib.size();
|
||||
|
||||
assert(size > 0);
|
||||
|
||||
double vacc = 1;
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
vacc *= sqrt(distrib.variance()[i]);
|
||||
}
|
||||
|
||||
assert(vacc <= std::numeric_limits<double>::max());
|
||||
|
||||
ofs_params_var << vacc << std::endl;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
++number_of_iterations;
|
||||
|
||||
}
|
||||
while ( _cooling_schedule( temperature ) &&
|
||||
_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 full evaluation function.
|
||||
eoEvalFunc < EOT > & _evaluation;
|
||||
|
||||
//! Stopping criterion before temperature update
|
||||
moSolContinue < EOT > & _continuator;
|
||||
|
||||
//! The cooling schedule
|
||||
moCoolingSchedule & _cooling_schedule;
|
||||
|
||||
//! Initial temperature
|
||||
double _initial_temperature;
|
||||
|
||||
//! A EOT replacor
|
||||
eoReplacement < EOT > & _replacor;
|
||||
};
|
||||
|
||||
#endif // !_doCMASA_h
|
||||
14
src/doDistrib.h
Normal file
14
src/doDistrib.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef _doDistrib_h
|
||||
#define _doDistrib_h
|
||||
|
||||
template < typename EOT >
|
||||
class doDistrib
|
||||
{
|
||||
public:
|
||||
//! Alias for the type
|
||||
typedef EOT EOType;
|
||||
|
||||
virtual ~doDistrib(){}
|
||||
};
|
||||
|
||||
#endif // !_doDistrib_h
|
||||
32
src/doDistribParams.h
Normal file
32
src/doDistribParams.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef _doDistribParams_h
|
||||
#define _doDistribParams_h
|
||||
|
||||
#include <vector>
|
||||
|
||||
template < typename EOT >
|
||||
class doDistribParams
|
||||
{
|
||||
public:
|
||||
doDistribParams(unsigned n = 2)
|
||||
: _params(n)
|
||||
{}
|
||||
|
||||
EOT& param(unsigned int i = 0){return _params[i];}
|
||||
|
||||
unsigned int param_size(){return _params.size();}
|
||||
|
||||
unsigned int size()
|
||||
{
|
||||
for (unsigned int i = 0, size = param_size(); i < size - 1; ++i)
|
||||
{
|
||||
assert(param(i).size() == param(i + 1).size());
|
||||
}
|
||||
|
||||
return param(0).size();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector< EOT > _params;
|
||||
};
|
||||
|
||||
#endif // !_doDistribParams_h
|
||||
16
src/doEstimator.h
Normal file
16
src/doEstimator.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _doEstimator_h
|
||||
#define _doEstimator_h
|
||||
|
||||
#include <eoPop.h>
|
||||
#include <eoFunctor.h>
|
||||
|
||||
template < typename D >
|
||||
class doEstimator : public eoUF< eoPop< typename D::EOType >&, D >
|
||||
{
|
||||
public:
|
||||
typedef typename D::EOType EOType;
|
||||
|
||||
// virtual D operator() ( eoPop< EOT >& )=0 (provided by eoUF< A1, R >)
|
||||
};
|
||||
|
||||
#endif // !_doEstimator_h
|
||||
46
src/doEstimatorNormal.h
Normal file
46
src/doEstimatorNormal.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#ifndef _doEstimatorNormal_h
|
||||
#define _doEstimatorNormal_h
|
||||
|
||||
#include "doEstimator.h"
|
||||
#include "doUniform.h"
|
||||
#include "doStats.h"
|
||||
|
||||
// TODO: calcule de la moyenne + covariance dans une classe derivee
|
||||
|
||||
template < typename EOT >
|
||||
class doEstimatorNormal : public doEstimator< doNormal< EOT > >
|
||||
{
|
||||
public:
|
||||
doNormal< EOT > operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
unsigned int popsize = pop.size();
|
||||
assert(popsize > 0);
|
||||
|
||||
unsigned int dimsize = pop[0].size();
|
||||
assert(dimsize > 0);
|
||||
|
||||
std::vector< Var > var(dimsize);
|
||||
|
||||
for (unsigned int i = 0; i < popsize; ++i)
|
||||
{
|
||||
for (unsigned int d = 0; d < dimsize; ++d)
|
||||
{
|
||||
var[d].update(pop[i][d]);
|
||||
}
|
||||
}
|
||||
|
||||
EOT mean(dimsize);
|
||||
EOT variance(dimsize);
|
||||
|
||||
for (unsigned int d = 0; d < dimsize; ++d)
|
||||
{
|
||||
mean[d] = var[d].get_mean();
|
||||
variance[d] = var[d].get_var();
|
||||
//variance[d] = var[d].get_std(); // perhaps I should use this !?!
|
||||
}
|
||||
|
||||
return doNormal< EOT >(mean, variance);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doEstimatorNormal_h
|
||||
44
src/doEstimatorUniform.h
Normal file
44
src/doEstimatorUniform.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#ifndef _doEstimatorUniform_h
|
||||
#define _doEstimatorUniform_h
|
||||
|
||||
#include "doEstimator.h"
|
||||
#include "doUniform.h"
|
||||
|
||||
// TODO: calcule de la moyenne + covariance dans une classe derivee
|
||||
|
||||
template < typename EOT >
|
||||
class doEstimatorUniform : public doEstimator< doUniform< EOT > >
|
||||
{
|
||||
public:
|
||||
doUniform< EOT > operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
unsigned int size = pop.size();
|
||||
|
||||
assert(size > 0);
|
||||
|
||||
EOT min = pop[0];
|
||||
EOT max = pop[0];
|
||||
|
||||
for (unsigned int i = 1; i < size; ++i)
|
||||
{
|
||||
unsigned int size = pop[i].size();
|
||||
|
||||
assert(size > 0);
|
||||
|
||||
// possibilité d'utiliser std::min_element et std::max_element mais exige 2 pass au lieu d'1.
|
||||
|
||||
for (unsigned int d = 0; d < size; ++d)
|
||||
{
|
||||
if (pop[i][d] < min[d])
|
||||
min[d] = pop[i][d];
|
||||
|
||||
if (pop[i][d] > max[d])
|
||||
max[d] = pop[i][d];
|
||||
}
|
||||
}
|
||||
|
||||
return doUniform< EOT >(min, max);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doEstimatorUniform_h
|
||||
227
src/doFileSnapshot.h
Normal file
227
src/doFileSnapshot.h
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// eoFileSnapshot.h
|
||||
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2001
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mkeijzer@dhi.dk
|
||||
Caner Candan caner@candan.fr
|
||||
Johann Dréo nojhan@gmail.com
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _doFileSnapshot_h
|
||||
#define _doFileSnapshot_h
|
||||
|
||||
#include <sys/stat.h> // for mkdir
|
||||
#include <sys/types.h> // for mkdir
|
||||
#include <unistd.h> // for unlink
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include <utils/eoParam.h>
|
||||
#include <utils/eoMonitor.h>
|
||||
#include <eoObject.h>
|
||||
|
||||
#include <utils/eoLogger.h>
|
||||
|
||||
/**
|
||||
Prints snapshots of fitnesses to a (new) file every N generations
|
||||
|
||||
Assumes that the parameters that are passed to the monitor
|
||||
(method add in eoMonitor.h) are eoValueParam<std::vector<double> > of same size.
|
||||
|
||||
A dir is created and one file per snapshot is created there -
|
||||
so you can later generate a movie!
|
||||
|
||||
TODO: The counter is handled internally, but this should be changed
|
||||
so that you can pass e.g. an evalcounter (minor)
|
||||
|
||||
I failed to templatize everything so that it can handle eoParam<std::vector<T> >
|
||||
for any type T, simply calling their getValue method ...
|
||||
*/
|
||||
|
||||
template < typename EOTParam >
|
||||
class doFileSnapshot : public eoMonitor
|
||||
{
|
||||
public :
|
||||
doFileSnapshot(std::string _dirname, unsigned _frequency = 1,
|
||||
std::string _filename = "gen",
|
||||
std::string _delim = " ", unsigned _counter = 0,
|
||||
bool _rmFiles = true):
|
||||
dirname(_dirname), frequency(_frequency),
|
||||
filename(_filename), delim(_delim), counter(_counter), boolChanged(true)
|
||||
{
|
||||
// FIXME START
|
||||
// TO REPLACE test command by somethink more gernerical
|
||||
|
||||
std::string s = "test -d " + dirname;
|
||||
|
||||
int res = system(s.c_str());
|
||||
// test for (unlikely) errors
|
||||
if ( (res==-1) || (res==127) )
|
||||
throw std::runtime_error("Problem executing test of dir in eoFileSnapshot");
|
||||
|
||||
// FIXME END
|
||||
|
||||
// now make sure there is a dir without any genXXX file in it
|
||||
if (res) // no dir present
|
||||
{
|
||||
//s = std::string("mkdir ")+dirname;
|
||||
::mkdir(dirname.c_str(), 0755);
|
||||
}
|
||||
else if (!res && _rmFiles)
|
||||
{
|
||||
//s = std::string("/bin/rm ")+dirname+ "/" + filename + "*";
|
||||
std::string s = dirname+ "/" + filename + "*";
|
||||
::unlink(s.c_str());
|
||||
}
|
||||
// else
|
||||
// s = " ";
|
||||
|
||||
//int nothing = system(s.c_str());
|
||||
// all done
|
||||
}
|
||||
|
||||
/** accessor: has something changed (for gnuplot subclass)
|
||||
*/
|
||||
virtual bool hasChanged() {return boolChanged;}
|
||||
|
||||
/** accessor to the counter: needed by the gnuplot subclass
|
||||
*/
|
||||
unsigned getCounter() {return counter;}
|
||||
|
||||
/** accessor to the current filename: needed by the gnuplot subclass
|
||||
*/
|
||||
std::string getFileName() {return currentFileName;}
|
||||
|
||||
/** sets the current filename depending on the counter
|
||||
*/
|
||||
void setCurrentFileName()
|
||||
{
|
||||
std::ostringstream oscount;
|
||||
oscount << counter;
|
||||
currentFileName = dirname + "/" + filename + oscount.str();
|
||||
}
|
||||
|
||||
/** The operator(void): opens the std::ostream and calls the write method
|
||||
*/
|
||||
eoMonitor& operator()(void)
|
||||
{
|
||||
if (counter % frequency)
|
||||
{
|
||||
boolChanged = false; // subclass with gnuplot will do nothing
|
||||
counter++;
|
||||
return (*this);
|
||||
}
|
||||
counter++;
|
||||
boolChanged = true;
|
||||
setCurrentFileName();
|
||||
std::ofstream os(currentFileName.c_str());
|
||||
|
||||
if (!os)
|
||||
{
|
||||
std::string str = "eoFileSnapshot: Could not open " + currentFileName;
|
||||
throw std::runtime_error(str);
|
||||
}
|
||||
|
||||
return operator()(os);
|
||||
}
|
||||
|
||||
/** The operator(): write on an std::ostream
|
||||
*/
|
||||
eoMonitor& operator()(std::ostream& _os)
|
||||
{
|
||||
//eo::log << eo::debug << "TOTO" << std::endl;
|
||||
|
||||
//_os << "TOTO" << "\n";
|
||||
|
||||
//_os << v[0] << "\n";
|
||||
|
||||
// const eoValueParam<std::vector<double> > * ptParam =
|
||||
// static_cast<const eoValueParam<std::vector<double> >* >(vec[0]);
|
||||
|
||||
// what's the size of vec ?!?
|
||||
|
||||
eo::log << eo::debug;
|
||||
|
||||
eo::log << "vec size: " << vec.size() << std::endl;
|
||||
|
||||
const eoValueParam< EOTParam > * ptParam =
|
||||
static_cast< const eoValueParam< EOTParam >* >(vec[0]);
|
||||
|
||||
//const std::vector<double> v = ptParam->value();
|
||||
EOTParam v(ptParam->value());
|
||||
if (vec.size() == 1) // only one std::vector: -> add number in front
|
||||
{
|
||||
eo::log << "I am here..." << std::endl;
|
||||
|
||||
//eo::log << *vec[0] << std::endl;
|
||||
|
||||
for (unsigned k=0; k<v.size(); k++)
|
||||
_os << k << " " << v[k] << "\n" ;
|
||||
}
|
||||
else // need to get all other std::vectors
|
||||
{
|
||||
//std::vector<std::vector<double> > vv(vec.size());
|
||||
std::vector< EOTParam > vv(vec.size());
|
||||
vv[0]=v;
|
||||
for (unsigned i=1; i<vec.size(); i++)
|
||||
{
|
||||
//ptParam = static_cast<const eoValueParam<std::vector<double> >* >(vec[1]);
|
||||
ptParam = static_cast< const eoValueParam< EOTParam >* >(vec[1]);
|
||||
vv[i] = ptParam->value();
|
||||
if (vv[i].size() != v.size())
|
||||
throw std::runtime_error("Dimension error in eoSnapshotMonitor");
|
||||
}
|
||||
for (unsigned k=0; k<v.size(); k++)
|
||||
{
|
||||
for (unsigned i=0; i<vec.size(); i++)
|
||||
_os << vv[i][k] << " " ;
|
||||
_os << "\n";
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual const std::string getDirName() // for eoGnuPlot
|
||||
{ return dirname;}
|
||||
virtual const std::string baseFileName() // the title for eoGnuPlot
|
||||
{ return filename;}
|
||||
|
||||
private :
|
||||
std::string dirname;
|
||||
unsigned frequency;
|
||||
std::string filename;
|
||||
std::string delim;
|
||||
unsigned int counter;
|
||||
std::string currentFileName;
|
||||
bool boolChanged;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// Local Variables:
|
||||
// coding: iso-8859-1
|
||||
// mode: C++
|
||||
// c-file-offsets: ((c . 0))
|
||||
// c-file-style: "Stroustrup"
|
||||
// fill-column: 80
|
||||
// End:
|
||||
13
src/doModifier.h
Normal file
13
src/doModifier.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef _doModifier_h
|
||||
#define _doModifier_h
|
||||
|
||||
template < typename D >
|
||||
class doModifier
|
||||
{
|
||||
public:
|
||||
virtual ~doModifier(){}
|
||||
|
||||
typedef typename D::EOType EOType;
|
||||
};
|
||||
|
||||
#endif // !_doModifier_h
|
||||
16
src/doModifierDispersion.h
Normal file
16
src/doModifierDispersion.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _doModifierDispersion_h
|
||||
#define _doModifierDispersion_h
|
||||
|
||||
#include <eoPop.h>
|
||||
#include <eoFunctor.h>
|
||||
|
||||
#include "doModifier.h"
|
||||
|
||||
template < typename D >
|
||||
class doModifierDispersion : public doModifier< D >, public eoBF< D&, eoPop< typename D::EOType >&, void >
|
||||
{
|
||||
public:
|
||||
// virtual void operator() ( D&, eoPop< D::EOType >& )=0 (provided by eoBF< A1, A2, R >)
|
||||
};
|
||||
|
||||
#endif // !_doModifierDispersion_h
|
||||
17
src/doModifierMass.h
Normal file
17
src/doModifierMass.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef _doModifierMass_h
|
||||
#define _doModifierMass_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
|
||||
#include "doModifier.h"
|
||||
|
||||
template < typename D >
|
||||
class doModifierMass : public doModifier< D >, public eoBF< D&, typename D::EOType&, void >
|
||||
{
|
||||
public:
|
||||
//typedef typename D::EOType::AtomType AtomType; // does not work !!!
|
||||
|
||||
// virtual void operator() ( D&, D::EOType& )=0 (provided by eoBF< A1, A2, R >)
|
||||
};
|
||||
|
||||
#endif // !_doModifierMass_h
|
||||
16
src/doNormal.h
Normal file
16
src/doNormal.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _doNormal_h
|
||||
#define _doNormal_h
|
||||
|
||||
#include "doDistrib.h"
|
||||
#include "doNormalParams.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doNormal : public doDistrib< EOT >, public doNormalParams< EOT >
|
||||
{
|
||||
public:
|
||||
doNormal(EOT mean, EOT variance)
|
||||
: doNormalParams< EOT >(mean, variance)
|
||||
{}
|
||||
};
|
||||
|
||||
#endif // !_doNormal_h
|
||||
19
src/doNormalCenter.h
Normal file
19
src/doNormalCenter.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef _doNormalCenter_h
|
||||
#define _doNormalCenter_h
|
||||
|
||||
#include "doModifierMass.h"
|
||||
#include "doNormal.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doNormalCenter : public doModifierMass< doNormal< EOT > >
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
void operator() ( doNormal< EOT >& distrib, EOT& mass )
|
||||
{
|
||||
distrib.mean() = mass; // vive les references!!!
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doNormalCenter_h
|
||||
24
src/doNormalParams.h
Normal file
24
src/doNormalParams.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef _doNormalParams_h
|
||||
#define _doNormalParams_h
|
||||
|
||||
#include "doDistribParams.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doNormalParams : public doDistribParams< EOT >
|
||||
{
|
||||
public:
|
||||
doNormalParams(EOT _mean, EOT _variance)
|
||||
: doDistribParams< EOT >(2)
|
||||
{
|
||||
assert(_mean.size() > 0);
|
||||
assert(_mean.size() == _variance.size());
|
||||
|
||||
mean() = _mean;
|
||||
variance() = _variance;
|
||||
}
|
||||
|
||||
EOT& mean(){return this->param(0);}
|
||||
EOT& variance(){return this->param(1);}
|
||||
};
|
||||
|
||||
#endif // !_doNormalParams_h
|
||||
58
src/doSampler.h
Normal file
58
src/doSampler.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#ifndef _doSampler_h
|
||||
#define _doSampler_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
|
||||
#include "doBounder.h"
|
||||
|
||||
template < typename D >
|
||||
class doSampler : public eoUF< D&, typename D::EOType >
|
||||
{
|
||||
public:
|
||||
typedef typename D::EOType EOType;
|
||||
|
||||
doSampler(doBounder< EOType > & bounder)
|
||||
: _bounder(bounder)
|
||||
{}
|
||||
|
||||
// virtual EOType operator()( D& ) = 0 (provided by eoUF< A1, R >)
|
||||
|
||||
virtual EOType sample( D& ) = 0;
|
||||
|
||||
EOType operator()( D& distrib )
|
||||
{
|
||||
unsigned int size = distrib.size();
|
||||
assert(size > 0);
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Point we want to sample to get higher a set of points
|
||||
// (coordinates in n dimension)
|
||||
// x = {x1, x2, ..., xn}
|
||||
// the sample method is implemented in the derivated class
|
||||
//-------------------------------------------------------------
|
||||
|
||||
EOType solution(sample(distrib));
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Now we are bounding the distribution thanks to min and max
|
||||
// parameters.
|
||||
//-------------------------------------------------------------
|
||||
|
||||
_bounder(solution);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
||||
private:
|
||||
//! Bounder functor
|
||||
doBounder< EOType > & _bounder;
|
||||
};
|
||||
|
||||
#endif // !_doSampler_h
|
||||
60
src/doSamplerNormal.h
Normal file
60
src/doSamplerNormal.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#ifndef _doSamplerNormal_h
|
||||
#define _doSamplerNormal_h
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include "doSampler.h"
|
||||
#include "doNormal.h"
|
||||
#include "doBounder.h"
|
||||
|
||||
/**
|
||||
* doSamplerNormal
|
||||
* This class uses the Normal distribution parameters (bounds) to return
|
||||
* a random position used for population sampling.
|
||||
*/
|
||||
template < typename EOT >
|
||||
class doSamplerNormal : public doSampler< doNormal< EOT > >
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
doSamplerNormal( doBounder< EOT > & bounder )
|
||||
: doSampler< doNormal< EOT > >( bounder )
|
||||
{}
|
||||
|
||||
EOT sample( doNormal< EOT >& distrib )
|
||||
{
|
||||
unsigned int size = distrib.size();
|
||||
|
||||
assert(size > 0);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Point we want to sample to get higher a set of points
|
||||
// (coordinates in n dimension)
|
||||
// x = {x1, x2, ..., xn}
|
||||
//-------------------------------------------------------------
|
||||
|
||||
EOT solution;
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Sampling all dimensions
|
||||
//-------------------------------------------------------------
|
||||
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
solution.push_back(
|
||||
rng.normal(distrib.mean()[i],
|
||||
distrib.variance()[i])
|
||||
);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
return solution;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doSamplerNormal_h
|
||||
55
src/doSamplerUniform.h
Normal file
55
src/doSamplerUniform.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#ifndef _doSamplerUniform_h
|
||||
#define _doSamplerUniform_h
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include "doSampler.h"
|
||||
#include "doUniform.h"
|
||||
|
||||
/**
|
||||
* doSamplerUniform
|
||||
* This class uses the Uniform distribution parameters (bounds) to return
|
||||
* a random position used for population sampling.
|
||||
*/
|
||||
template < typename EOT >
|
||||
class doSamplerUniform : public doSampler< doUniform< EOT > >
|
||||
{
|
||||
public:
|
||||
EOT sample( doUniform< EOT >& distrib )
|
||||
{
|
||||
unsigned int size = distrib.size();
|
||||
assert(size > 0);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Point we want to sample to get higher a population
|
||||
// x = {x1, x2, ..., xn}
|
||||
//-------------------------------------------------------------
|
||||
|
||||
EOT solution;
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Sampling all dimensions
|
||||
//-------------------------------------------------------------
|
||||
|
||||
for (unsigned int i = 0; i < size; ++i)
|
||||
{
|
||||
double min = distrib.min()[i];
|
||||
double max = distrib.max()[i];
|
||||
double random = rng.uniform(min, max);
|
||||
|
||||
assert(min <= random && random <= max);
|
||||
|
||||
solution.push_back(random);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
return solution;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doSamplerUniform_h
|
||||
121
src/doStats.h
Normal file
121
src/doStats.h
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (C) 2005 Maarten Keijzer
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
class Mean {
|
||||
|
||||
double n;
|
||||
double mean;
|
||||
|
||||
public:
|
||||
Mean() : n(0), mean(0) {}
|
||||
|
||||
void update(double v) {
|
||||
n++;
|
||||
double d = v - mean;
|
||||
mean += 1/n * d;
|
||||
}
|
||||
|
||||
double get_mean() const { return mean; }
|
||||
};
|
||||
|
||||
class Var {
|
||||
double n;
|
||||
double mean;
|
||||
double sumvar;
|
||||
|
||||
public:
|
||||
Var() : n(0), mean(0), sumvar(0) {}
|
||||
|
||||
void update(double v) {
|
||||
n++;
|
||||
double d = v - mean;
|
||||
mean += 1/n * d;
|
||||
sumvar += (n-1)/n * d * d;
|
||||
}
|
||||
|
||||
double get_mean() const { return mean; }
|
||||
double get_var() const { return sumvar / (n-1); }
|
||||
double get_std() const { return sqrt(get_var()); }
|
||||
};
|
||||
|
||||
/** Single covariance between two variates */
|
||||
class Cov {
|
||||
double n;
|
||||
double meana;
|
||||
double meanb;
|
||||
double sumcov;
|
||||
|
||||
public:
|
||||
Cov() : n(0), meana(0), meanb(0), sumcov(0) {}
|
||||
|
||||
void update(double a, double b) {
|
||||
++n;
|
||||
double da = a - meana;
|
||||
double db = b - meanb;
|
||||
|
||||
meana += 1/n * da;
|
||||
meanb += 1/n * db;
|
||||
|
||||
sumcov += (n-1)/n * da * db;
|
||||
}
|
||||
|
||||
double get_meana() const { return meana; }
|
||||
double get_meanb() const { return meanb; }
|
||||
double get_cov() const { return sumcov / (n-1); }
|
||||
};
|
||||
|
||||
class CovMatrix {
|
||||
double n;
|
||||
std::vector<double> mean;
|
||||
std::vector< std::vector<double> > sumcov;
|
||||
|
||||
public:
|
||||
CovMatrix(unsigned dim) : n(0), mean(dim), sumcov(dim , std::vector<double>(dim)) {}
|
||||
|
||||
void update(const std::vector<double>& v) {
|
||||
assert(v.size() == mean.size());
|
||||
|
||||
n++;
|
||||
|
||||
for (unsigned i = 0; i < v.size(); ++i) {
|
||||
double d = v[i] - mean[i];
|
||||
mean[i] += 1/n * d;
|
||||
|
||||
sumcov[i][i] += (n-1)/n * d * d;
|
||||
|
||||
for (unsigned j = i; j < v.size(); ++j) {
|
||||
double e = v[j] - mean[j]; // mean[j] is not updated yet
|
||||
|
||||
double upd = (n-1)/n * d * e;
|
||||
|
||||
sumcov[i][j] += upd;
|
||||
sumcov[j][i] += upd;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
double get_mean(int i) const { return mean[i]; }
|
||||
double get_var(int i ) const { return sumcov[i][i] / (n-1); }
|
||||
double get_std(int i) const { return sqrt(get_var(i)); }
|
||||
double get_cov(int i, int j) const { return sumcov[i][j] / (n-1); }
|
||||
|
||||
};
|
||||
16
src/doUniform.h
Normal file
16
src/doUniform.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _doUniform_h
|
||||
#define _doUniform_h
|
||||
|
||||
#include "doDistrib.h"
|
||||
#include "doVectorBounds.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doUniform : public doDistrib< EOT >, public doVectorBounds< EOT >
|
||||
{
|
||||
public:
|
||||
doUniform(EOT min, EOT max)
|
||||
: doVectorBounds< EOT >(min, max)
|
||||
{}
|
||||
};
|
||||
|
||||
#endif // !_doUniform_h
|
||||
28
src/doUniformCenter.h
Normal file
28
src/doUniformCenter.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef _doUniformCenter_h
|
||||
#define _doUniformCenter_h
|
||||
|
||||
#include "doModifierMass.h"
|
||||
#include "doUniform.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doUniformCenter : public doModifierMass< doUniform< EOT > >
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
void operator() ( doUniform< EOT >& distrib, EOT& mass )
|
||||
{
|
||||
for (unsigned int i = 0, n = mass.size(); i < n; ++i)
|
||||
{
|
||||
AtomType& min = distrib.min()[i];
|
||||
AtomType& max = distrib.max()[i];
|
||||
|
||||
AtomType range = (max - min) / 2;
|
||||
|
||||
min = mass[i] - range;
|
||||
max = mass[i] + range;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_doUniformCenter_h
|
||||
24
src/doVectorBounds.h
Normal file
24
src/doVectorBounds.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef _doVectorBounds_h
|
||||
#define _doVectorBounds_h
|
||||
|
||||
#include "doDistribParams.h"
|
||||
|
||||
template < typename EOT >
|
||||
class doVectorBounds : public doDistribParams< EOT >
|
||||
{
|
||||
public:
|
||||
doVectorBounds(EOT _min, EOT _max)
|
||||
: doDistribParams< EOT >(2)
|
||||
{
|
||||
assert(_min.size() > 0);
|
||||
assert(_min.size() == _max.size());
|
||||
|
||||
min() = _min;
|
||||
max() = _max;
|
||||
}
|
||||
|
||||
EOT& min(){return this->param(0);}
|
||||
EOT& max(){return this->param(1);}
|
||||
};
|
||||
|
||||
#endif // !_doVectorBounds_h
|
||||
Reference in a new issue