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

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

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 moVectorMonitor_h #ifndef moVectorMonitor_h
#define moVectorMonitor_h #define moVectorMonitor_h
@ -49,158 +49,166 @@ class moVectorMonitor : public eoMonitor
{ {
public: public:
/** /**
* Constructor * Constructor
* @param _param the parameter of type double to save in the vector * @param _param the parameter of type double to save in the vector
*/ */
moVectorMonitor(eoValueParam<double> & _param) : doubleParam(&_param), intParam(NULL), eotParam(NULL) moVectorMonitor(eoValueParam<double> & _param) : doubleParam(&_param), intParam(NULL), eotParam(NULL)
{ } { }
/** /**
* Default Constructor * Default Constructor
* @param _param the parameter of type unsigned int to save in the vector * @param _param the parameter of type unsigned int to save in the vector
*/ */
moVectorMonitor(eoValueParam<unsigned int> & _param) : doubleParam(NULL), intParam(&_param), eotParam(NULL) moVectorMonitor(eoValueParam<unsigned int> & _param) : doubleParam(NULL), intParam(&_param), eotParam(NULL)
{ } { }
/** /**
* Default Constructor * Default Constructor
* @param _param the parameter of type EOT to save in the vector * @param _param the parameter of type EOT to save in the vector
*/ */
moVectorMonitor(eoValueParam<EOT> & _param) : doubleParam(NULL), intParam(NULL), eotParam(&_param) moVectorMonitor(eoValueParam<EOT> & _param) : doubleParam(NULL), intParam(NULL), eotParam(&_param)
{ } { }
/** /**
* Default Constructor * Default Constructor
* @param _param the parameter of type eoScalarFitness to save in the vector * @param _param the parameter of type eoScalarFitness to save in the vector
*/ */
template <class ScalarType, class Compare> template <class ScalarType, class Compare>
moVectorMonitor(eoValueParam<eoScalarFitness<ScalarType, Compare> > & _param) : doubleParam( & (eoValueParam<double>&)_param), intParam(NULL), eotParam(NULL) moVectorMonitor(eoValueParam<eoScalarFitness<ScalarType, Compare> > & _param) : doubleParam( & (eoValueParam<double>&)_param), intParam(NULL), eotParam(NULL)
{ } { }
/** /**
* Default Constructor * Default Constructor
* @param _param unvalid Parameter * @param _param unvalid Parameter
*/ */
template <class T> template <class T>
moVectorMonitor(eoValueParam<T> & _param) : doubleParam(NULL), intParam(NULL), eotParam(NULL) moVectorMonitor(eoValueParam<T> & _param) : doubleParam(NULL), intParam(NULL), eotParam(NULL)
{ {
std::cerr << "Sorry the type can not be in a vector of moVectorMonitor" << std::endl; std::cerr << "Sorry the type can not be in a vector of moVectorMonitor" << std::endl;
} }
/** /**
* To test if the value are basic type (double or unsigned int), or EOT type * To test if the value are basic type (double or unsigned int), or EOT type
* *
* @return true if the type is a EOT type * @return true if the type is a EOT type
*/ */
bool solutionType() { bool solutionType() {
return eotParam != NULL; return eotParam != NULL;
} }
/** /**
* To "print" the value of the parameter in the vector * To "print" the value of the parameter in the vector
* *
* @return this monitor (sorry I don't why, but it is like this in EO) * @return this monitor (sorry I don't why, but it is like this in EO)
*/ */
eoMonitor& operator()(void) { eoMonitor& operator()(void) {
if (doubleParam != NULL) if (doubleParam != NULL)
valueVec.push_back(doubleParam->value()); valueVec.push_back(doubleParam->value());
else else
if (intParam != NULL) if (intParam != NULL)
valueVec.push_back((double) intParam->value()); valueVec.push_back((double) intParam->value());
else else
eotVec.push_back(eotParam->value()); eotVec.push_back(eotParam->value());
return *this ; return *this ;
} }
/** /**
* To have all the values * To have all the values
* *
* @return the vector of values * @return the vector of values
*/ */
const std::vector<double>& getValues() const { const std::vector<double>& getValues() const {
return valueVec; return valueVec;
} }
/** /**
* To have all the solutions * To have all the solutions
* *
* @return the vector of solutions * @return the vector of solutions
*/ */
const std::vector<EOT>& getSolutions() const { const std::vector<EOT>& getSolutions() const {
return eotVec; return eotVec;
} }
/** /**
* to get the value out. * to get the value out.
* @return the string of the value * @return the string of the value
*/ */
std::string getValue(unsigned int i) const { std::string getValue(unsigned int i) const {
std::ostringstream os; std::ostringstream os;
if (eotParam == NULL) if (eotParam == NULL)
os << (valueVec[i]) ; os << (valueVec[i]) ;
else else
os << (eotVec[i]) ; os << (eotVec[i]) ;
return os.str(); return os.str();
} }
/** /**
* clear the vector * clear the vector
*/ */
void clear() { void clear() {
valueVec.clear(); valueVec.clear();
eotVec.clear(); eotVec.clear();
} }
/** /**
* number of value * number of value
* @return size of the vector * @return size of the vector
*/ */
unsigned int size() { unsigned int size() {
if (eotParam == NULL) if (eotParam == NULL)
return valueVec.size(); return valueVec.size();
else else
return eotVec.size(); return eotVec.size();
} }
/** /**
* to export the vector of values into one file * to export the vector of values into one file
* @param _filename file name * @param _filename file name
*/ * @param _openFile to specify if it writes at the following of the file
void fileExport(std::string _filename) { */
// create file void fileExport(std::string _filename, bool _openFile=false) {
std::ofstream os(_filename.c_str()); // create file
std::ofstream os;
if (!os) { if(! _openFile)
std::string str = "moVectorMonitor: Could not open " + _filename; os.open(_filename.c_str());
throw std::runtime_error(str);
}
for (unsigned int i = 0; i < size(); i++) { else
os << getValue(i); os.open(_filename.c_str(),std::ios::app);
os << std::endl ;
}
} if (!os) {
std::string str = "moVectorMonitor: Could not open " + _filename;
throw std::runtime_error(str);
}
/** for (unsigned int i = 0; i < size(); i++) {
* @return name of the class os << getValue(i);
*/
virtual std::string className(void) const { os << std::endl ;
return "moVectorMonitor"; }
}
}
/**
* @return name of the class
*/
virtual std::string className(void) const {
return "moVectorMonitor";
}
protected: protected:
eoValueParam<double> * doubleParam ; eoValueParam<double> * doubleParam ;
eoValueParam<unsigned int> * intParam ; eoValueParam<unsigned int> * intParam ;
eoValueParam<EOT> * eotParam ; eoValueParam<EOT> * eotParam ;
std::vector<double> valueVec; std::vector<double> valueVec;
std::vector<EOT> eotVec; std::vector<EOT> eotVec;
}; };