code formating
This commit is contained in:
parent
c7d060efc0
commit
37a2c68b69
4 changed files with 142 additions and 192 deletions
|
|
@ -30,7 +30,7 @@ Authors:
|
||||||
|
|
||||||
#include <eoFunctor.h>
|
#include <eoFunctor.h>
|
||||||
|
|
||||||
#include "edoBounder.h"
|
#include "edoRepairer.h"
|
||||||
#include "edoBounderNo.h"
|
#include "edoBounderNo.h"
|
||||||
|
|
||||||
//! edoSampler< D >
|
//! edoSampler< D >
|
||||||
|
|
@ -41,47 +41,34 @@ class edoSampler : public eoUF< D&, typename D::EOType >
|
||||||
public:
|
public:
|
||||||
typedef typename D::EOType EOType;
|
typedef typename D::EOType EOType;
|
||||||
|
|
||||||
edoSampler(edoBounder< EOType > & bounder)
|
edoSampler(edoRepairer< EOType > & repairer)
|
||||||
: /*_dummy_bounder(),*/ _bounder(bounder)
|
: _dummy_repairer(), _repairer(repairer)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/*
|
|
||||||
edoSampler()
|
edoSampler()
|
||||||
: _dummy_bounder(), _bounder( _dummy_bounder )
|
: _dummy_repairer(), _repairer( _dummy_repairer )
|
||||||
{}
|
{}
|
||||||
*/
|
|
||||||
|
|
||||||
// virtual EOType operator()( D& ) = 0 (provided by eoUF< A1, R >)
|
// virtual EOType operator()( D& ) = 0 (provided by eoUF< A1, R >)
|
||||||
|
|
||||||
EOType operator()( D& distrib )
|
EOType operator()( D& distrib )
|
||||||
{
|
{
|
||||||
unsigned int size = distrib.size();
|
unsigned int size = distrib.size();
|
||||||
assert(size > 0);
|
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
|
||||||
// Point we want to sample to get higher a set of points
|
// parameters.
|
||||||
// (coordinates in n dimension)
|
_repairer(solution);
|
||||||
// x = {x1, x2, ..., xn}
|
|
||||||
// the sample method is implemented in the derivated class
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
|
|
||||||
EOType solution(sample(distrib));
|
return solution;
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
// Now we are bounding the distribution thanks to min and max
|
|
||||||
// parameters.
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
|
|
||||||
_bounder(solution);
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
return solution;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
@ -89,10 +76,10 @@ protected:
|
||||||
virtual EOType sample( D& ) = 0;
|
virtual EOType sample( D& ) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//edoBounderNo<EOType> _dummy_bounder;
|
edoBounderNo<EOType> _dummy_repairer;
|
||||||
|
|
||||||
//! Bounder functor
|
//! repairer functor
|
||||||
edoBounder< EOType > & _bounder;
|
edoRepairer< EOType > & _repairer;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,46 +45,31 @@ class edoSamplerNormalMono : public edoSampler< edoNormalMono< EOT > >
|
||||||
public:
|
public:
|
||||||
typedef typename EOT::AtomType AtomType;
|
typedef typename EOT::AtomType AtomType;
|
||||||
|
|
||||||
edoSamplerNormalMono( edoBounder< EOT > & bounder )
|
edoSamplerNormalMono( edoRepairer<EOT> & repairer ) : edoSampler( repairer) {}
|
||||||
: edoSampler< edoNormalMono< EOT > >( bounder )
|
|
||||||
{}
|
|
||||||
|
|
||||||
EOT sample( edoNormalMono< EOT >& distrib )
|
EOT sample( edoNormalMono< EOT >& distrib )
|
||||||
{
|
{
|
||||||
unsigned int size = distrib.size();
|
unsigned int size = distrib.size();
|
||||||
assert(size > 0);
|
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
|
||||||
// Point we want to sample to get higher a set of points
|
for (unsigned int i = 0; i < size; ++i)
|
||||||
// (coordinates in n dimension)
|
{
|
||||||
// x = {x1, x2, ..., xn}
|
AtomType mean = distrib.mean()[i];
|
||||||
//-------------------------------------------------------------
|
AtomType variance = distrib.variance()[i];
|
||||||
|
AtomType random = rng.normal(mean, variance);
|
||||||
|
|
||||||
EOT solution;
|
assert(variance >= 0);
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
solution.push_back(random);
|
||||||
|
}
|
||||||
|
|
||||||
|
return solution;
|
||||||
//-------------------------------------------------------------
|
|
||||||
// Sampling all dimensions
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < size; ++i)
|
|
||||||
{
|
|
||||||
AtomType mean = distrib.mean()[i];
|
|
||||||
AtomType variance = distrib.variance()[i];
|
|
||||||
AtomType random = rng.normal(mean, variance);
|
|
||||||
|
|
||||||
assert(variance >= 0);
|
|
||||||
|
|
||||||
solution.push_back(random);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
return solution;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,136 +40,138 @@ class edoSamplerNormalMulti : public edoSampler< edoNormalMulti< EOT > >
|
||||||
public:
|
public:
|
||||||
typedef typename EOT::AtomType AtomType;
|
typedef typename EOT::AtomType AtomType;
|
||||||
|
|
||||||
|
edoSamplerNormalMulti( edoRepairer<EOT> & repairer ) : edoSampler( repairer) {}
|
||||||
|
|
||||||
class Cholesky
|
class Cholesky
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Cholesky( const ublas::symmetric_matrix< AtomType, ublas::lower >& V)
|
Cholesky( const ublas::symmetric_matrix< AtomType, ublas::lower >& V)
|
||||||
{
|
{
|
||||||
unsigned int Vl = V.size1();
|
unsigned int Vl = V.size1();
|
||||||
|
|
||||||
assert(Vl > 0);
|
assert(Vl > 0);
|
||||||
|
|
||||||
unsigned int Vc = V.size2();
|
unsigned int Vc = V.size2();
|
||||||
|
|
||||||
assert(Vc > 0);
|
assert(Vc > 0);
|
||||||
|
|
||||||
assert( Vl == Vc );
|
assert( Vl == Vc );
|
||||||
|
|
||||||
_L.resize(Vl);
|
_L.resize(Vl);
|
||||||
|
|
||||||
unsigned int i,j,k;
|
unsigned int i,j,k;
|
||||||
|
|
||||||
// first column
|
// first column
|
||||||
i=0;
|
i=0;
|
||||||
|
|
||||||
// diagonal
|
// diagonal
|
||||||
j=0;
|
j=0;
|
||||||
_L(0, 0) = sqrt( V(0, 0) );
|
_L(0, 0) = sqrt( V(0, 0) );
|
||||||
|
|
||||||
// end of the column
|
// end of the column
|
||||||
for ( j = 1; j < Vc; ++j )
|
for ( j = 1; j < Vc; ++j )
|
||||||
{
|
{
|
||||||
_L(j, 0) = V(0, j) / _L(0, 0);
|
_L(j, 0) = V(0, j) / _L(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// end of the matrix
|
// end of the matrix
|
||||||
for ( i = 1; i < Vl; ++i ) // each column
|
for ( i = 1; i < Vl; ++i ) // each column
|
||||||
{
|
{
|
||||||
|
|
||||||
// diagonal
|
// diagonal
|
||||||
double sum = 0.0;
|
double sum = 0.0;
|
||||||
|
|
||||||
for ( k = 0; k < i; ++k)
|
for ( k = 0; k < i; ++k)
|
||||||
{
|
{
|
||||||
sum += _L(i, k) * _L(i, k);
|
sum += _L(i, k) * _L(i, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
_L(i,i) = sqrt( fabs( V(i,i) - sum) );
|
_L(i,i) = sqrt( fabs( V(i,i) - sum) );
|
||||||
|
|
||||||
for ( j = i + 1; j < Vl; ++j ) // rows
|
for ( j = i + 1; j < Vl; ++j ) // rows
|
||||||
{
|
{
|
||||||
// one element
|
// one element
|
||||||
sum = 0.0;
|
sum = 0.0;
|
||||||
|
|
||||||
for ( k = 0; k < i; ++k )
|
for ( k = 0; k < i; ++k )
|
||||||
{
|
{
|
||||||
sum += _L(j, k) * _L(i, k);
|
sum += _L(j, k) * _L(i, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
_L(j, i) = (V(j, i) - sum) / _L(i, i);
|
_L(j, i) = (V(j, i) - sum) / _L(i, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ublas::symmetric_matrix< AtomType, ublas::lower >& get_L() const {return _L;}
|
const ublas::symmetric_matrix< AtomType, ublas::lower >& get_L() const {return _L;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ublas::symmetric_matrix< AtomType, ublas::lower > _L;
|
ublas::symmetric_matrix< AtomType, ublas::lower > _L;
|
||||||
};
|
};
|
||||||
|
|
||||||
edoSamplerNormalMulti( edoBounder< EOT > & bounder )
|
edoSamplerNormalMulti( edoBounder< EOT > & bounder )
|
||||||
: edoSampler< edoNormalMulti< EOT > >( bounder )
|
: edoSampler< edoNormalMulti< EOT > >( bounder )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
EOT sample( edoNormalMulti< EOT >& distrib )
|
EOT sample( edoNormalMulti< EOT >& distrib )
|
||||||
{
|
{
|
||||||
unsigned int size = distrib.size();
|
unsigned int size = distrib.size();
|
||||||
|
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
// Cholesky factorisation gererating matrix L from covariance
|
// Cholesky factorisation gererating matrix L from covariance
|
||||||
// matrix V.
|
// matrix V.
|
||||||
// We must use cholesky.get_L() to get the resulting matrix.
|
// We must use cholesky.get_L() to get the resulting matrix.
|
||||||
//
|
//
|
||||||
// L = cholesky decomposition of varcovar
|
// L = cholesky decomposition of varcovar
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
Cholesky cholesky( distrib.varcovar() );
|
Cholesky cholesky( distrib.varcovar() );
|
||||||
ublas::symmetric_matrix< AtomType, ublas::lower > L = cholesky.get_L();
|
ublas::symmetric_matrix< AtomType, ublas::lower > L = cholesky.get_L();
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
// T = vector of size elements drawn in N(0,1) rng.normal(1.0)
|
// T = vector of size elements drawn in N(0,1) rng.normal(1.0)
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
ublas::vector< AtomType > T( size );
|
ublas::vector< AtomType > T( size );
|
||||||
|
|
||||||
for ( unsigned int i = 0; i < size; ++i )
|
for ( unsigned int i = 0; i < size; ++i )
|
||||||
{
|
{
|
||||||
T( i ) = rng.normal( 1.0 );
|
T( i ) = rng.normal( 1.0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
// LT = prod( L, T )
|
// LT = prod( L, T )
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
ublas::vector< AtomType > LT = ublas::prod( L, T );
|
ublas::vector< AtomType > LT = ublas::prod( L, T );
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
// solution = means + LT
|
// solution = means + LT
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
ublas::vector< AtomType > mean = distrib.mean();
|
ublas::vector< AtomType > mean = distrib.mean();
|
||||||
|
|
||||||
ublas::vector< AtomType > ublas_solution = mean + LT;
|
ublas::vector< AtomType > ublas_solution = mean + LT;
|
||||||
|
|
||||||
EOT solution( size );
|
EOT solution( size );
|
||||||
|
|
||||||
std::copy( ublas_solution.begin(), ublas_solution.end(), solution.begin() );
|
std::copy( ublas_solution.begin(), ublas_solution.end(), solution.begin() );
|
||||||
|
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,52 +44,28 @@ class edoSamplerUniform : public edoSampler< edoUniform< EOT > >
|
||||||
public:
|
public:
|
||||||
typedef D Distrib;
|
typedef D Distrib;
|
||||||
|
|
||||||
edoSamplerUniform(edoBounder< EOT > & bounder)
|
edoSamplerUniform( edoRepairer<EOT> & repairer ) : edoSampler( repairer) {}
|
||||||
: edoSampler< edoUniform<EOT> >(bounder) // FIXME: Why D is not used here ?
|
|
||||||
{}
|
|
||||||
|
|
||||||
/*
|
|
||||||
edoSamplerUniform()
|
|
||||||
: edoSampler< edoUniform<EOT> >()
|
|
||||||
{}
|
|
||||||
*/
|
|
||||||
|
|
||||||
EOT sample( edoUniform< EOT >& distrib )
|
EOT sample( edoUniform< EOT >& distrib )
|
||||||
{
|
{
|
||||||
unsigned int size = distrib.size();
|
unsigned int size = distrib.size();
|
||||||
assert(size > 0);
|
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
|
||||||
// Point we want to sample to get higher a set of points
|
for (unsigned int i = 0; i < size; ++i)
|
||||||
// (coordinates in n dimension)
|
{
|
||||||
// x = {x1, x2, ..., xn}
|
double min = distrib.min()[i];
|
||||||
//-------------------------------------------------------------
|
double max = distrib.max()[i];
|
||||||
|
double random = rng.uniform(min, max);
|
||||||
|
solution.push_back(random);
|
||||||
|
}
|
||||||
|
|
||||||
EOT solution;
|
return 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;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue