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 "edoBounder.h"
|
||||
#include "edoRepairer.h"
|
||||
#include "edoBounderNo.h"
|
||||
|
||||
//! edoSampler< D >
|
||||
|
|
@ -41,47 +41,34 @@ class edoSampler : public eoUF< D&, typename D::EOType >
|
|||
public:
|
||||
typedef typename D::EOType EOType;
|
||||
|
||||
edoSampler(edoBounder< EOType > & bounder)
|
||||
: /*_dummy_bounder(),*/ _bounder(bounder)
|
||||
edoSampler(edoRepairer< EOType > & repairer)
|
||||
: _dummy_repairer(), _repairer(repairer)
|
||||
{}
|
||||
|
||||
/*
|
||||
|
||||
edoSampler()
|
||||
: _dummy_bounder(), _bounder( _dummy_bounder )
|
||||
: _dummy_repairer(), _repairer( _dummy_repairer )
|
||||
{}
|
||||
*/
|
||||
|
||||
|
||||
// virtual EOType operator()( D& ) = 0 (provided by eoUF< A1, R >)
|
||||
|
||||
EOType operator()( D& distrib )
|
||||
{
|
||||
unsigned int size = distrib.size();
|
||||
assert(size > 0);
|
||||
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));
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// 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
|
||||
//-------------------------------------------------------------
|
||||
// Now we are bounding the distribution thanks to min and max
|
||||
// parameters.
|
||||
_repairer(solution);
|
||||
|
||||
EOType solution(sample(distrib));
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Now we are bounding the distribution thanks to min and max
|
||||
// parameters.
|
||||
//-------------------------------------------------------------
|
||||
|
||||
_bounder(solution);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
return solution;
|
||||
return solution;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
@ -89,10 +76,10 @@ protected:
|
|||
virtual EOType sample( D& ) = 0;
|
||||
|
||||
private:
|
||||
//edoBounderNo<EOType> _dummy_bounder;
|
||||
edoBounderNo<EOType> _dummy_repairer;
|
||||
|
||||
//! Bounder functor
|
||||
edoBounder< EOType > & _bounder;
|
||||
//! repairer functor
|
||||
edoRepairer< EOType > & _repairer;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -45,46 +45,31 @@ class edoSamplerNormalMono : public edoSampler< edoNormalMono< EOT > >
|
|||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
edoSamplerNormalMono( edoBounder< EOT > & bounder )
|
||||
: edoSampler< edoNormalMono< EOT > >( bounder )
|
||||
{}
|
||||
edoSamplerNormalMono( edoRepairer<EOT> & repairer ) : edoSampler( repairer) {}
|
||||
|
||||
EOT sample( edoNormalMono< EOT >& distrib )
|
||||
{
|
||||
unsigned int size = distrib.size();
|
||||
assert(size > 0);
|
||||
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)
|
||||
{
|
||||
AtomType mean = distrib.mean()[i];
|
||||
AtomType variance = distrib.variance()[i];
|
||||
AtomType random = rng.normal(mean, variance);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Point we want to sample to get higher a set of points
|
||||
// (coordinates in n dimension)
|
||||
// x = {x1, x2, ..., xn}
|
||||
//-------------------------------------------------------------
|
||||
assert(variance >= 0);
|
||||
|
||||
EOT solution;
|
||||
solution.push_back(random);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// 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;
|
||||
return solution;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -40,136 +40,138 @@ class edoSamplerNormalMulti : public edoSampler< edoNormalMulti< EOT > >
|
|||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
edoSamplerNormalMulti( edoRepairer<EOT> & repairer ) : edoSampler( repairer) {}
|
||||
|
||||
class Cholesky
|
||||
{
|
||||
public:
|
||||
Cholesky( const ublas::symmetric_matrix< AtomType, ublas::lower >& V)
|
||||
{
|
||||
unsigned int Vl = V.size1();
|
||||
Cholesky( const ublas::symmetric_matrix< AtomType, ublas::lower >& V)
|
||||
{
|
||||
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
|
||||
i=0;
|
||||
// first column
|
||||
i=0;
|
||||
|
||||
// diagonal
|
||||
j=0;
|
||||
_L(0, 0) = sqrt( V(0, 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 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
|
||||
{
|
||||
// end of the matrix
|
||||
for ( i = 1; i < Vl; ++i ) // each column
|
||||
{
|
||||
|
||||
// diagonal
|
||||
double sum = 0.0;
|
||||
// diagonal
|
||||
double sum = 0.0;
|
||||
|
||||
for ( k = 0; k < i; ++k)
|
||||
{
|
||||
sum += _L(i, k) * _L(i, k);
|
||||
}
|
||||
for ( k = 0; k < 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
|
||||
{
|
||||
// one element
|
||||
sum = 0.0;
|
||||
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);
|
||||
}
|
||||
for ( k = 0; k < 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:
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > _L;
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > _L;
|
||||
};
|
||||
|
||||
edoSamplerNormalMulti( edoBounder< EOT > & bounder )
|
||||
: edoSampler< edoNormalMulti< EOT > >( bounder )
|
||||
: edoSampler< edoNormalMulti< EOT > >( bounder )
|
||||
{}
|
||||
|
||||
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
|
||||
// matrix V.
|
||||
// We must use cholesky.get_L() to get the resulting matrix.
|
||||
//
|
||||
// L = cholesky decomposition of varcovar
|
||||
//-------------------------------------------------------------
|
||||
//-------------------------------------------------------------
|
||||
// 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
|
||||
//-------------------------------------------------------------
|
||||
|
||||
Cholesky cholesky( distrib.varcovar() );
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > L = cholesky.get_L();
|
||||
Cholesky cholesky( distrib.varcovar() );
|
||||
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 )
|
||||
{
|
||||
T( i ) = rng.normal( 1.0 );
|
||||
}
|
||||
for ( unsigned int i = 0; i < size; ++i )
|
||||
{
|
||||
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:
|
||||
typedef D Distrib;
|
||||
|
||||
edoSamplerUniform(edoBounder< EOT > & bounder)
|
||||
: edoSampler< edoUniform<EOT> >(bounder) // FIXME: Why D is not used here ?
|
||||
{}
|
||||
|
||||
/*
|
||||
edoSamplerUniform()
|
||||
: edoSampler< edoUniform<EOT> >()
|
||||
{}
|
||||
*/
|
||||
edoSamplerUniform( edoRepairer<EOT> & repairer ) : edoSampler( repairer) {}
|
||||
|
||||
EOT sample( edoUniform< EOT >& distrib )
|
||||
{
|
||||
unsigned int size = distrib.size();
|
||||
assert(size > 0);
|
||||
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;
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Point we want to sample to get higher a set of points
|
||||
// (coordinates in n dimension)
|
||||
// x = {x1, x2, ..., xn}
|
||||
//-------------------------------------------------------------
|
||||
// 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);
|
||||
solution.push_back(random);
|
||||
}
|
||||
|
||||
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;
|
||||
return solution;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Reference in a new issue