This commit is contained in:
Caner Candan 2010-08-06 09:50:58 +02:00
commit 1653288301
16 changed files with 972 additions and 95 deletions

View file

@ -86,8 +86,51 @@ public:
_continuator(continuator),
_cooling_schedule(cooling_schedule),
_initial_temperature(initial_temperature),
_replacor(replacor)
{}
_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 ss;
ss << "rm -rf " << _pop_results_destination;
::system(ss.str().c_str());
}
::mkdir(_pop_results_destination.c_str(), 0755); // create a first time the
//-------------------------------------------------------------
//-------------------------------------------------------------
// Temporary solution to store bounds values for each distribution.
//-------------------------------------------------------------
{
std::stringstream ss;
ss << "rm -rf " << _bounds_results_destination;
::system(ss.str().c_str());
}
<<<<<<< HEAD
::mkdir(bounds_results_destination.c_str(), 0755); // create once directory
=======
::mkdir(_bounds_results_destination.c_str(), 0755); // create once directory
//-------------------------------------------------------------
}
//! function that launches the CMASA algorithm.
/*!
@ -114,44 +157,7 @@ public:
//-------------------------------------------------------------
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 values 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 once directory
>>>>>>> 36ec42d36204631eb4c25ae7b31a8728903697f8
//-------------------------------------------------------------
@ -166,10 +172,10 @@ public:
for (int i = 0; i < size; ++i)
{
hv.update( distrib.variance()[i] );
//hv.update( distrib.varcovar()[i] );
}
ofs_params_var << hv << std::endl;
_ofs_params_var << hv << std::endl;
}
do
@ -260,7 +266,7 @@ public:
{
std::stringstream ss;
ss << pop_results_destination << "/" << number_of_iterations;
ss << _pop_results_destination << "/" << number_of_iterations;
std::ofstream ofs(ss.str().c_str());
ofs << current_pop;
}
@ -279,12 +285,16 @@ public:
assert(size > 0);
std::stringstream ss;
ss << bounds_results_destination << "/" << number_of_iterations;
ss << _bounds_results_destination << "/" << number_of_iterations;
std::ofstream ofs(ss.str().c_str());
ofs << size << " ";
std::copy(distrib.mean().begin(), distrib.mean().end(), std::ostream_iterator< double >(ofs, " "));
<<<<<<< HEAD
std::copy(distrib.variance().begin(), distrib.variance().end(), std::ostream_iterator< double >(ofs, " "));
=======
//std::copy(distrib.varcovar().begin(), distrib.varcovar().end(), std::ostream_iterator< double >(ofs, " "));
>>>>>>> 36ec42d36204631eb4c25ae7b31a8728903697f8
ofs << std::endl;
}
@ -334,10 +344,10 @@ public:
for (int i = 0; i < size; ++i)
{
hv.update( distrib.variance()[i] );
//hv.update( distrib.varcovar()[i] );
}
ofs_params_var << hv << std::endl;
_ofs_params_var << hv << std::endl;
}
//-------------------------------------------------------------
@ -383,6 +393,27 @@ private:
//! 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 // !_doCMASA_h

View file

@ -9,6 +9,8 @@ template < typename EOT >
class doEstimatorNormal : public doEstimator< doNormal< EOT > >
{
public:
typedef typename EOT::AtomType AtomType;
doNormal< EOT > operator()(eoPop<EOT>& pop)
{
unsigned int popsize = pop.size();
@ -17,23 +19,12 @@ public:
unsigned int dimsize = pop[0].size();
assert(dimsize > 0);
doCovMatrix cov(dimsize);
//doCovMatrix cov(dimsize);
doUblasCovMatrix< EOT > cov;
for (unsigned int i = 0; i < popsize; ++i)
{
cov.update(pop[i]);
}
cov.update(pop);
EOT mean(dimsize);
EOT covariance(dimsize);
for (unsigned int d = 0; d < dimsize; ++d)
{
mean[d] = cov.get_mean(d);
covariance[d] = cov.get_var(d);
}
return doNormal< EOT >(mean, covariance);
return doNormal< EOT >( cov.get_mean(), cov.get_varcovar() );
}
};

View file

@ -8,8 +8,10 @@ template < typename EOT >
class doNormal : public doDistrib< EOT >, public doNormalParams< EOT >
{
public:
doNormal(EOT mean, EOT variance)
: doNormalParams< EOT >(mean, variance)
typedef typename EOT::AtomType AtomType;
doNormal( const ublas::vector< AtomType >& mean, const ublas::symmetric_matrix< AtomType, ublas::lower >& varcovar )
: doNormalParams< EOT >( mean, varcovar )
{}
};

View file

@ -12,7 +12,9 @@ public:
void operator() ( doNormal< EOT >& distrib, EOT& mass )
{
distrib.mean() = mass; // vive les references!!!
ublas::vector< AtomType > mean( distrib.size() );
std::copy( mass.begin(), mass.end(), mean.begin() );
distrib.mean() = mean;
}
};

View file

@ -1,10 +1,19 @@
#ifndef _doNormalParams_h
#define _doNormalParams_h
<<<<<<< HEAD
=======
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/lu.hpp>
namespace ublas = boost::numeric::ublas;
>>>>>>> 36ec42d36204631eb4c25ae7b31a8728903697f8
template < typename EOT >
class doNormalParams
{
public:
<<<<<<< HEAD
doNormalParams(EOT mean, EOT variance)
: _mean(mean), _variance(variance)
{
@ -18,12 +27,40 @@ public:
unsigned int size()
{
assert(_mean.size() == _variance.size());
=======
typedef typename EOT::AtomType AtomType;
doNormalParams
(
const ublas::vector< AtomType >& mean,
const ublas::symmetric_matrix< AtomType, ublas::lower >& varcovar
)
: _mean(mean), _varcovar(varcovar)
{
assert(_mean.size() > 0);
assert(_mean.size() == _varcovar.size1());
assert(_mean.size() == _varcovar.size2());
}
ublas::vector< AtomType >& mean(){return _mean;}
ublas::symmetric_matrix< AtomType, ublas::lower >& varcovar(){return _varcovar;}
unsigned int size()
{
assert(_mean.size() == _varcovar.size1());
assert(_mean.size() == _varcovar.size2());
>>>>>>> 36ec42d36204631eb4c25ae7b31a8728903697f8
return _mean.size();
}
private:
<<<<<<< HEAD
EOT _mean;
EOT _variance;
=======
ublas::vector< AtomType > _mean;
ublas::symmetric_matrix< AtomType, ublas::lower > _varcovar;
>>>>>>> 36ec42d36204631eb4c25ae7b31a8728903697f8
};
#endif // !_doNormalParams_h

View file

@ -6,6 +6,7 @@
#include "doSampler.h"
#include "doNormal.h"
#include "doBounder.h"
#include "doStats.h"
/**
* doSamplerNormal
@ -29,30 +30,61 @@ public:
assert(size > 0);
//-------------------------------------------------------------
// Point we want to sample to get higher a set of points
// (coordinates in n dimension)
// x = {x1, x2, ..., xn}
// Cholesky factorisation gererating matrix L from covariance
// matrix V.
// We must use cholesky.get_L() to get the resulting matrix.
//
// L = cholesky decomposition of varcovar
//-------------------------------------------------------------
EOT solution;
Cholesky< EOT > cholesky;
cholesky.update( distrib.varcovar() );
ublas::symmetric_matrix< AtomType, ublas::lower > L = cholesky.get_L();
//-------------------------------------------------------------
//-------------------------------------------------------------
// Sampling all dimensions
// T = vector of size elements drawn in N(0,1) rng.normal(1.0)
//-------------------------------------------------------------
for (unsigned int i = 0; i < size; ++i)
ublas::vector< AtomType > T( size );
for ( unsigned int i = 0; i < size; ++i )
{
solution.push_back(
rng.normal(distrib.mean()[i],
distrib.variance()[i])
);
T( i ) = rng.normal( 1.0 );
}
//-------------------------------------------------------------
//-------------------------------------------------------------
// LT = prod( L, trans(T) ) ?
// LT = prod( L, T )
//-------------------------------------------------------------
//ublas::symmetric_matrix< AtomType, ublas::lower > LT = ublas::prod( L, ublas::trans( T ) );
ublas::vector< AtomType > LT = ublas::prod( L, T );
//-------------------------------------------------------------
//-------------------------------------------------------------
// solution = means + trans( LT ) ?
// solution = means + LT
//-------------------------------------------------------------
ublas::vector< AtomType > mean = distrib.mean();
ublas::vector< AtomType > ublas_solution = mean + LT;
//ublas::vector< AtomType > ublas_solution = mean + ublas::trans( LT );
EOT solution( size );
std::copy( ublas_solution.begin(), ublas_solution.end(), solution.begin() );
//-------------------------------------------------------------
return solution;
}
};

View file

@ -20,8 +20,10 @@ public:
unsigned int size = distrib.size();
assert(size > 0);
//-------------------------------------------------------------
// Point we want to sample to get higher a population
// Point we want to sample to get higher a set of points
// (coordinates in n dimension)
// x = {x1, x2, ..., xn}
//-------------------------------------------------------------

View file

@ -18,7 +18,14 @@
#ifndef _doStats_h
#define _doStats_h
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <eoPrintable.h>
#include <eoPop.h>
namespace ublas = boost::numeric::ublas;
class doStats : public eoPrintable
{
@ -110,9 +117,159 @@ protected:
double _hv;
};
<<<<<<< HEAD
class doCholesky : public doStats
{
=======
template < typename EOT >
class doUblasCovMatrix : public doStats
{
public:
typedef typename EOT::AtomType AtomType;
virtual void update( const eoPop< EOT >& pop )
{
unsigned int p_size = pop.size(); // population size
assert(p_size > 0);
unsigned int s_size = pop[0].size(); // solution size
assert(s_size > 0);
ublas::matrix< AtomType > sample( p_size, s_size );
for (unsigned int i = 0; i < p_size; ++i)
{
for (unsigned int j = 0; j < s_size; ++j)
{
sample(i, j) = pop[i][j];
}
}
_varcovar.resize(s_size, s_size);
//-------------------------------------------------------------
// variance-covariance matrix are symmetric (and semi-definite
// positive), thus a triangular storage is sufficient
//-------------------------------------------------------------
ublas::symmetric_matrix< AtomType, ublas::lower > var(s_size, s_size);
//-------------------------------------------------------------
//-------------------------------------------------------------
// variance-covariance matrix computation : A * transpose(A)
//-------------------------------------------------------------
var = ublas::prod( sample, ublas::trans( sample ) );
//-------------------------------------------------------------
for (unsigned int i = 0; i < s_size; ++i)
{
// triangular LOWER matrix, thus j is not going further than i
for (unsigned int j = 0; j <= i; ++j)
{
// we want a reducted covariance matrix
_varcovar(i, j) = var(i, j) / p_size;
}
}
_mean.resize(s_size);
// unit vector
ublas::scalar_vector< AtomType > u( p_size, 1 );
// sum over columns
_mean = ublas::prod( ublas::trans( sample ), u );
// division by n
_mean /= p_size;
}
const ublas::symmetric_matrix< AtomType, ublas::lower >& get_varcovar() const {return _varcovar;}
const ublas::vector< AtomType >& get_mean() const {return _mean;}
private:
ublas::symmetric_matrix< AtomType, ublas::lower > _varcovar;
ublas::vector< AtomType > _mean;
};
template < typename EOT >
class Cholesky : public doStats
{
public:
typedef typename EOT::AtomType AtomType;
virtual void update( const ublas::symmetric_matrix< AtomType, ublas::lower >& V)
{
unsigned int Vl = V.size1();
assert(Vl > 0);
unsigned int Vc = V.size2();
assert(Vc > 0);
_L.resize(Vl, Vc);
unsigned int i,j,k;
// first column
i=0;
// diagonal
j=0;
_L(0, 0) = sqrt( V(0, 0) );
// end of the column
for ( j = 1; j < Vc; ++j )
{
_L(j, 0) = V(0, j) / _L(0, 0);
}
// end of the matrix
for ( i = 1; i < Vl; ++i ) // each column
{
// diagonal
double sum = 0.0;
for ( k = 0; k < i; ++k)
{
sum += _L(i, k) * _L(i, k);
}
assert( ( V(i, i) - sum ) > 0 );
_L(i, i) = sqrt( V(i, i) - sum );
for ( j = i + 1; j < Vl; ++j ) // rows
{
// one element
sum = 0.0;
for ( k = 0; k < i; ++k )
{
sum += _L(j, k) * _L(i, k);
}
_L(j, i) = (V(j, i) - sum) / _L(i, i);
}
}
}
const ublas::symmetric_matrix< AtomType, ublas::lower >& get_L() const {return _L;}
private:
ublas::symmetric_matrix< AtomType, ublas::lower > _L;
>>>>>>> 36ec42d36204631eb4c25ae7b31a8728903697f8
};
#endif // !_doStats_h

2
src/todo Normal file
View file

@ -0,0 +1,2 @@
* deplacer les ecritures pour gnuplot dans des classes type eoContinue (eoMonitor)
* integrer ACP