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