Add a param in fileExport method in moSampling to be able to write at the end of the file

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1939 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
marieeleonore 2010-09-24 09:56:12 +00:00
commit e91516ba50

View file

@ -30,7 +30,7 @@
ParadisEO WebSite : http://paradiseo.gforge.inria.fr ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr Contact: paradiseo-help@lists.gforge.inria.fr
*/ */
#ifndef moSampling_h #ifndef moSampling_h
#define moSampling_h #define moSampling_h
@ -38,6 +38,8 @@
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <sstream>
#include <cstdlib>
#include <eoFunctor.h> #include <eoFunctor.h>
#include <utils/eoMonitor.h> #include <utils/eoMonitor.h>
#include <continuator/moStat.h> #include <continuator/moStat.h>
@ -58,150 +60,159 @@ template <class Neighbor>
class moSampling : public eoF<void> class moSampling : public eoF<void>
{ {
public: public:
typedef typename Neighbor::EOT EOT ; typedef typename Neighbor::EOT EOT ;
/** /**
* Constructor * Constructor
* @param _init initialisation method of the solution * @param _init initialisation method of the solution
* @param _localSearch local search to sample the search space * @param _localSearch local search to sample the search space
* @param _stat statistic to compute during the search * @param _stat statistic to compute during the search
* @param _monitoring the statistic is saved into the monitor if true * @param _monitoring the statistic is saved into the monitor if true
*/ */
template <class ValueType> template <class ValueType>
moSampling(eoInit<EOT> & _init, moLocalSearch<Neighbor> & _localSearch, moStat<EOT,ValueType> & _stat, bool _monitoring = true) : init(_init), localSearch(&_localSearch), continuator(_localSearch.getContinuator()) moSampling(eoInit<EOT> & _init, moLocalSearch<Neighbor> & _localSearch, moStat<EOT,ValueType> & _stat, bool _monitoring = true) : init(_init), localSearch(&_localSearch), continuator(_localSearch.getContinuator())
{ {
checkpoint = new moCheckpoint<Neighbor>(*continuator); checkpoint = new moCheckpoint<Neighbor>(*continuator);
add(_stat, _monitoring); add(_stat, _monitoring);
} }
/** /**
* default destructor * default destructor
*/ */
~moSampling() { ~moSampling() {
// delete all monitors // delete all monitors
for (unsigned i = 0; i < monitorVec.size(); i++) for (unsigned i = 0; i < monitorVec.size(); i++)
delete monitorVec[i]; delete monitorVec[i];
// delete the checkpoint // delete the checkpoint
delete checkpoint ; delete checkpoint ;
} }
/** /**
* Add a statistic * Add a statistic
* @param _stat another statistic to compute during the search * @param _stat another statistic to compute during the search
* @param _monitoring the statistic is saved into the monitor if true * @param _monitoring the statistic is saved into the monitor if true
*/ */
template< class ValueType > template< class ValueType >
void add(moStat<EOT, ValueType> & _stat, bool _monitoring = true) { void add(moStat<EOT, ValueType> & _stat, bool _monitoring = true) {
checkpoint->add(_stat); checkpoint->add(_stat);
if (_monitoring) { if (_monitoring) {
moVectorMonitor<EOT> * monitor = new moVectorMonitor<EOT>(_stat); moVectorMonitor<EOT> * monitor = new moVectorMonitor<EOT>(_stat);
monitorVec.push_back(monitor); monitorVec.push_back(monitor);
checkpoint->add(*monitor); checkpoint->add(*monitor);
} }
} }
/** /**
* To sample the search and get the statistics which are stored in the moVectorMonitor vector * To sample the search and get the statistics which are stored in the moVectorMonitor vector
*/ */
void operator()(void) { void operator()(void) {
// clear all statistic vectors // clear all statistic vectors
for (unsigned i = 0; i < monitorVec.size(); i++) for (unsigned i = 0; i < monitorVec.size(); i++)
monitorVec[i]->clear(); monitorVec[i]->clear();
// change the checkpoint to compute the statistics // change the checkpoint to compute the statistics
localSearch->setContinuator(*checkpoint); localSearch->setContinuator(*checkpoint);
// the initial solution // the initial solution
EOT solution; EOT solution;
// initialisation of the solution // initialisation of the solution
init(solution); init(solution);
// compute the sampling // compute the sampling
(*localSearch)(solution); (*localSearch)(solution);
// set back to initial continuator // set back to initial continuator
localSearch->setContinuator(*continuator); localSearch->setContinuator(*continuator);
} }
/** /**
* to export the vectors of values into one file * to export the vectors of values into one file
* @param _filename file name * @param _filename file name
* @param _delim delimiter between statistics * @param _delim delimiter between statistics
*/ * @param _openFile to specify if it writes at the following of the file
void fileExport(std::string _filename, std::string _delim = " ") { */
// create file void fileExport(std::string _filename, std::string _delim = " ", bool _openFile=false) {
std::ofstream os(_filename.c_str()); // create file
std::ofstream os;
if (!os) { if(! _openFile)
std::string str = "moSampling: Could not open " + _filename; os.open(_filename.c_str());
throw std::runtime_error(str);
}
// all vector have the same size else
unsigned vecSize = monitorVec[0]->size(); os.open(_filename.c_str(),std::ios::app);
for (unsigned int i = 0; i < vecSize; i++) { if (!os) {
os << monitorVec[0]->getValue(i); std::string str = "moSampling: Could not open " + _filename;
throw std::runtime_error(str);
}
for (unsigned int j = 1; j < monitorVec.size(); j++) { // all vector have the same size
os << _delim.c_str() << monitorVec[j]->getValue(i); unsigned vecSize = monitorVec[0]->size();
}
os << std::endl ; for (unsigned int i = 0; i < vecSize; i++) {
} os << monitorVec[0]->getValue(i);
} for (unsigned int j = 1; j < monitorVec.size(); j++) {
os << _delim.c_str() << monitorVec[j]->getValue(i);
}
/** os << std::endl ;
* to export one vector of values into a file }
* @param _col number of vector to print into file
* @param _filename file name
*/
void fileExport(unsigned int _col, std::string _filename) {
if (_col >= monitorVec.size()) {
std::string str = "moSampling: Could not export into file the vector. The index does not exists (too large)";
throw std::runtime_error(str);
}
monitorVec[_col]->fileExport(_filename); }
}
/** /**
* to get one vector of values * to export one vector of values into a file
* @param _numStat number of statistics to get (in the order of creation) * @param _col number of vector to print into file
* @return the vector of value (all values are converted in double) * @param _filename file name
*/ * @param _openFile to specify if it writes at the following of the file
const std::vector<double> & getValues(unsigned int _numStat) { */
return monitorVec[_numStat]->getValues(); void fileExport(unsigned int _col, std::string _filename, bool _openFile=false) {
} if (_col >= monitorVec.size()) {
std::string str = "moSampling: Could not export into file the vector. The index does not exists (too large)";
throw std::runtime_error(str);
}
/** monitorVec[_col]->fileExport(_filename, _openFile);
* to get one vector of solutions values }
* @param _numStat number of statistics to get (in the order of creation)
* @return the vector of value (all values are converted in double)
*/
const std::vector<EOT> & getSolutions(unsigned int _numStat) {
return monitorVec[_numStat]->getSolutions();
}
/**
* @return name of the class /**
*/ * to get one vector of values
virtual std::string className(void) const { * @param _numStat number of statistics to get (in the order of creation)
return "moSampling"; * @return the vector of value (all values are converted in double)
} */
const std::vector<double> & getValues(unsigned int _numStat) {
return monitorVec[_numStat]->getValues();
}
/**
* to get one vector of solutions values
* @param _numStat number of statistics to get (in the order of creation)
* @return the vector of value (all values are converted in double)
*/
const std::vector<EOT> & getSolutions(unsigned int _numStat) {
return monitorVec[_numStat]->getSolutions();
}
/**
* @return name of the class
*/
virtual std::string className(void) const {
return "moSampling";
}
protected: protected:
eoInit<EOT> & init; eoInit<EOT> & init;
moLocalSearch<Neighbor> * localSearch; moLocalSearch<Neighbor> * localSearch;
moContinuator<Neighbor> * continuator; moContinuator<Neighbor> * continuator;
moCheckpoint<Neighbor> * checkpoint; moCheckpoint<Neighbor> * checkpoint;
std::vector< moVectorMonitor<EOT> *> monitorVec; std::vector< moVectorMonitor<EOT> *> monitorVec;
}; };