+ lib utils

This commit is contained in:
Caner Candan 2010-08-24 10:26:36 +02:00
commit 5b3f42d277
15 changed files with 33 additions and 14 deletions

View file

@ -10,3 +10,12 @@ FILE(GLOB SOURCES *.cpp)
SET(SAMPLE_SRCS ${SOURCES} PARENT_SCOPE)
######################################################################################
######################################################################################
### 2) Where must cmake go now ?
######################################################################################
ADD_SUBDIRECTORY(utils)
######################################################################################

View file

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

14
src/do
View file

@ -41,14 +41,14 @@
#include "doBounderRng.h"
#include "doContinue.h"
#include "doCheckPoint.h"
#include "utils/doCheckPoint.h"
#include "doStat.h"
#include "doStatUniform.h"
#include "doStatNormalMono.h"
#include "doStatNormalMulti.h"
#include "utils/doStat.h"
#include "utils/doStatUniform.h"
#include "utils/doStatNormalMono.h"
#include "utils/doStatNormalMulti.h"
#include "doFileSnapshot.h"
#include "doPopStat.h"
#include "utils/doFileSnapshot.h"
#include "utils/doPopStat.h"
#endif // !_do_

View file

@ -9,10 +9,9 @@
#define _doContinue_h
#include <eoFunctor.h>
#include <eoPop.h>
#include <eoPersistent.h>
//! eoContinue< EOT > classe fitted to Distribution Object library
//! doContinue< EOT > classe fitted to Distribution Object library
template < typename D >
class doContinue : public eoUF< const D&, bool >, public eoPersistent
@ -36,7 +35,7 @@ class doDummyContinue : public doContinue< D >
{
bool operator()(const D&){ return true; }
virtual std::string className() const { return "doNoContinue"; }
virtual std::string className() const { return "doDummyContinue"; }
};
#endif // !_doContinue_h

View file

@ -17,8 +17,6 @@
#include "doEstimator.h"
#include "doModifierMass.h"
#include "doSampler.h"
#include "doHyperVolume.h"
#include "doStat.h"
#include "doContinue.h"
template < typename D >

14
src/utils/CMakeLists.txt Normal file
View file

@ -0,0 +1,14 @@
######################################################################################
### 1) Set all needed source files for the project
######################################################################################
FILE(GLOB SOURCES *.cpp)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
ADD_LIBRARY(doutils ${SOURCES})
INSTALL(TARGETS doutils ARCHIVE DESTINATION lib COMPONENT libraries)
FILE(GLOB HDRS *.h utils)
INSTALL(FILES ${HDRS} DESTINATION include/do/utils COMPONENT headers)
######################################################################################

126
src/utils/doCheckPoint.h Normal file
View file

@ -0,0 +1,126 @@
// (c) Thales group, 2010
/*
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef _doCheckPoint_h
#define _doCheckPoint_h
#include <utils/eoUpdater.h>
#include <utils/eoMonitor.h>
#include "doContinue.h"
#include "doStat.h"
//! eoCheckPoint< EOT > classe fitted to Distribution Object library
template < typename D >
class doCheckPoint : public doContinue< D >
{
public:
typedef typename D::EOType EOType;
doCheckPoint(doContinue< D >& _cont)
{
_continuators.push_back( &_cont );
}
bool operator()(const D& distrib)
{
for ( unsigned int i = 0, size = _stats.size(); i < size; ++i )
{
(*_stats[i])( distrib );
}
for ( unsigned int i = 0, size = _updaters.size(); i < size; ++i )
{
(*_updaters[i])();
}
for ( unsigned int i = 0, size = _monitors.size(); i < size; ++i )
{
(*_monitors[i])();
}
bool bContinue = true;
for ( unsigned int i = 0, size = _continuators.size(); i < size; ++i )
{
if ( !(*_continuators[i])( distrib ) )
{
bContinue = false;
}
}
if ( !bContinue )
{
for ( unsigned int i = 0, size = _stats.size(); i < size; ++i )
{
_stats[i]->lastCall( distrib );
}
for ( unsigned int i = 0, size = _updaters.size(); i < size; ++i )
{
_updaters[i]->lastCall();
}
for ( unsigned int i = 0, size = _monitors.size(); i < size; ++i )
{
_monitors[i]->lastCall();
}
}
return bContinue;
}
void add(doContinue< D >& cont) { _continuators.push_back( &cont ); }
void add(doStatBase< D >& stat) { _stats.push_back( &stat ); }
void add(eoMonitor& mon) { _monitors.push_back( &mon ); }
void add(eoUpdater& upd) { _updaters.push_back( &upd ); }
virtual std::string className(void) const { return "doCheckPoint"; }
std::string allClassNames() const
{
std::string s("\n" + className() + "\n");
s += "Stats\n";
for ( unsigned int i = 0, size = _stats.size(); i < size; ++i )
{
s += _stats[i]->className() + "\n";
}
s += "\n";
s += "Updaters\n";
for ( unsigned int i = 0; i < _updaters.size(); ++i )
{
s += _updaters[i]->className() + "\n";
}
s += "\n";
s += "Monitors\n";
for ( unsigned int i = 0; i < _monitors.size(); ++i )
{
s += _monitors[i]->className() + "\n";
}
s += "\n";
s += "Continuators\n";
for ( unsigned int i = 0, size = _continuators.size(); i < size; ++i )
{
s += _continuators[i]->className() + "\n";
}
s += "\n";
return s;
}
private:
std::vector< doContinue< D >* > _continuators;
std::vector< doStatBase< D >* > _stats;
std::vector< eoMonitor* > _monitors;
std::vector< eoUpdater* > _updaters;
};
#endif // !_doCheckPoint_h

View file

@ -0,0 +1,121 @@
//-----------------------------------------------------------------------------
// doFileSnapshot.cpp
// (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
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
//-----------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <utils/doFileSnapshot.h>
#include <utils/compatibility.h>
#include <utils/eoParam.h>
doFileSnapshot::doFileSnapshot(std::string dirname,
unsigned int frequency /*= 1*/,
std::string filename /*= "gen"*/,
std::string delim /*= " "*/,
unsigned int counter /*= 0*/,
bool rmFiles /*= true*/)
: _dirname(dirname), _frequency(frequency),
_filename(filename), _delim(delim),
_counter(counter), _boolChanged(true)
{
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");
}
// now make sure there is a dir without any genXXX file in it
if (res) // no dir present
{
s = std::string("mkdir ") + _dirname;
}
else if (!res && rmFiles)
{
s = std::string("/bin/rm ") + _dirname+ "/" + _filename + "*";
}
else
{
s = " ";
}
int dummy;
dummy = system(s.c_str());
// all done
}
void doFileSnapshot::setCurrentFileName()
{
std::ostringstream oscount;
oscount << _counter;
_currentFileName = _dirname + "/" + _filename + oscount.str();
}
eoMonitor& doFileSnapshot::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 = "doFileSnapshot: Could not open " + _currentFileName;
throw std::runtime_error(str);
}
return operator()(os);
}
eoMonitor& doFileSnapshot::operator()(std::ostream& os)
{
iterator it = vec.begin();
os << (*it)->getValue();
for ( ++it; it != vec.end(); ++it )
{
os << _delim.c_str() << (*it)->getValue();
}
os << '\n';
return *this;
}

View file

@ -0,0 +1,69 @@
//-----------------------------------------------------------------------------
// doFileSnapshot.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
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
//-----------------------------------------------------------------------------
#ifndef _doFileSnapshot_h
#define _doFileSnapshot_h
#include <string>
#include <fstream>
#include <stdexcept>
#include "utils/eoMonitor.h"
class doFileSnapshot : public eoMonitor
{
public:
doFileSnapshot(std::string dirname,
unsigned int frequency = 1,
std::string filename = "gen",
std::string delim = " ",
unsigned int counter = 0,
bool rmFiles = true);
virtual bool hasChanged() {return _boolChanged;}
virtual std::string getDirName() { return _dirname; }
virtual unsigned int getCounter() { return _counter; }
virtual const std::string baseFileName() { return _filename;}
std::string getFileName() {return _currentFileName;}
void setCurrentFileName();
virtual eoMonitor& operator()(void);
virtual eoMonitor& operator()(std::ostream& os);
private :
std::string _dirname;
unsigned int _frequency;
std::string _filename;
std::string _delim;
std::string _currentFileName;
unsigned int _counter;
bool _boolChanged;
};
#endif // !_doFileSnapshot

32
src/utils/doHyperVolume.h Normal file
View file

@ -0,0 +1,32 @@
// (c) Thales group, 2010
/*
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef _doHyperVolume_h
#define _doHyperVolume_h
template < typename EOT >
class doHyperVolume
{
public:
typedef typename EOT::AtomType AtomType;
doHyperVolume() : _hv(1) {}
void update(AtomType v)
{
_hv *= ::sqrt( v );
assert( _hv <= std::numeric_limits< AtomType >::max() );
}
AtomType get_hypervolume() const { return _hv; }
protected:
AtomType _hv;
};
#endif // !_doHyperVolume_h

75
src/utils/doPopStat.h Normal file
View file

@ -0,0 +1,75 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// doPopStat.h
// (c) Maarten Keijzer, Marc Schoenauer 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
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
//-----------------------------------------------------------------------------
/** WARNING: this file contains 2 classes:
eoPopString and eoSortedPopString
that transform the population into a std::string
that can be used to dump to the screen
*/
#ifndef _doPopStat_h
#define _doPopStat_h
#include <utils/eoStat.h>
/** Thanks to MS/VC++, eoParam mechanism is unable to handle std::vectors of stats.
This snippet is a workaround:
This class will "print" a whole population into a std::string - that you can later
send to any stream
This is the plain version - see eoPopString for the Sorted version
Note: this Stat should probably be used only within eoStdOutMonitor, and not
inside an eoFileMonitor, as the eoState construct will work much better there.
*/
template <class EOT>
class doPopStat : public eoStat<EOT, std::string>
{
public:
using eoStat<EOT, std::string>::value;
/** default Ctor, void std::string by default, as it appears
on the description line once at beginning of evolution. and
is meaningless there. _howMany defaults to 0, that is, the whole
population*/
doPopStat(std::string _desc ="")
: eoStat<EOT, std::string>("", _desc) {}
/** Fills the value() of the eoParam with the dump of the population. */
void operator()(const eoPop<EOT>& _pop)
{
std::ostringstream os;
os << _pop;
value() = os.str();
}
};
#endif // !_doPopStat_h

54
src/utils/doStat.h Normal file
View file

@ -0,0 +1,54 @@
// (c) Thales group, 2010
/*
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef _doStat_h
#define _doStat_h
#include <eoFunctor.h>
template < typename D >
class doStatBase : public eoUF< const D&, void >
{
public:
// virtual void operator()( const D& ) = 0 (provided by eoUF< A1, R >)
virtual void lastCall( const D& ) {}
virtual std::string className() const { return "doStatBase"; }
};
template < typename D > class doCheckPoint;
template < typename D, typename T >
class doStat : public eoValueParam< T >, public doStatBase< D >
{
public:
doStat(T value, std::string description)
: eoValueParam< T >(value, description)
{}
virtual std::string className(void) const { return "doStat"; }
doStat< D, T >& addTo(doCheckPoint< D >& cp) { cp.add(*this); return *this; }
// TODO: doStat< D, T >& addTo(eoMonitor& mon) { mon.add(*this); return *this; }
};
//! A parent class for any kind of distribution to dump parameter to std::string type
template < typename D >
class doDistribStat : public doStat< D, std::string >
{
public:
using doStat< D, std::string >::value;
doDistribStat(std::string desc)
: doStat< D, std::string >("", desc)
{}
};
#endif // !_doStat_h

View file

@ -0,0 +1,35 @@
// (c) Thales group, 2010
/*
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef _doStatNormalMono_h
#define _doStatNormalMono_h
#include "doStat.h"
#include "doNormalMono.h"
template < typename EOT >
class doStatNormalMono : public doDistribStat< doNormalMono< EOT > >
{
public:
using doDistribStat< doNormalMono< EOT > >::value;
doStatNormalMono( std::string desc = "" )
: doDistribStat< doNormalMono< EOT > >( desc )
{}
void operator()( const doNormalMono< EOT >& distrib )
{
value() = "\n# ====== mono normal distribution dump =====\n";
std::ostringstream os;
os << distrib.mean() << " " << distrib.variance() << std::endl;
value() += os.str();
}
};
#endif // !_doStatNormalMono_h

View file

@ -0,0 +1,48 @@
// (c) Thales group, 2010
/*
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef _doStatNormalMulti_h
#define _doStatNormalMulti_h
#include <boost/numeric/ublas/io.hpp>
#include "doStat.h"
#include "doNormalMulti.h"
template < typename EOT >
class doStatNormalMulti : public doDistribStat< doNormalMulti< EOT > >
{
public:
typedef typename EOT::AtomType AtomType;
using doDistribStat< doNormalMulti< EOT > >::value;
doStatNormalMulti( std::string desc = "" )
: doDistribStat< doNormalMulti< EOT > >( desc )
{}
void operator()( const doNormalMulti< EOT >& distrib )
{
value() = "\n# ====== multi normal distribution dump =====\n";
std::ostringstream os;
os << distrib.mean() << " " << distrib.varcovar() << std::endl;
// ublas::vector< AtomType > mean = distrib.mean();
// std::copy(mean.begin(), mean.end(), std::ostream_iterator< std::string >( os, " " ));
// ublas::symmetric_matrix< AtomType, ublas::lower > varcovar = distrib.varcovar();
// std::copy(varcovar.begin(), varcovar.end(), std::ostream_iterator< std::string >( os, " " ));
// os << std::endl;
value() += os.str();
}
};
#endif // !_doStatNormalMulti_h

35
src/utils/doStatUniform.h Normal file
View file

@ -0,0 +1,35 @@
// (c) Thales group, 2010
/*
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef _doStatUniform_h
#define _doStatUniform_h
#include "doStat.h"
#include "doUniform.h"
template < typename EOT >
class doStatUniform : public doDistribStat< doUniform< EOT > >
{
public:
using doDistribStat< doUniform< EOT > >::value;
doStatUniform( std::string desc = "" )
: doDistribStat< doUniform< EOT > >( desc )
{}
void operator()( const doUniform< EOT >& distrib )
{
value() = "\n# ====== uniform distribution dump =====\n";
std::ostringstream os;
os << distrib.min() << " " << distrib.max() << std::endl;
value() += os.str();
}
};
#endif // !_doStatUniform_h