Provide eoParser::setORcreateParam to set a paramter in the parser to

a specific value.
This commit is contained in:
kuepper 2005-08-29 07:50:50 +00:00
commit f0fd15f20c
3 changed files with 421 additions and 399 deletions

View file

@ -3,25 +3,25 @@
//-----------------------------------------------------------------------------
// eoParam.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
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 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.
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
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
*/
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef eoParam_h
@ -43,154 +43,177 @@
#include <vector>
#include <iterator> // for GCC 3.2
#include <stdexcept>
#include <stdexcept>
#include <eoScalarFitness.h> // for specializations
/**
eoParam: Base class for monitoring and parsing parameters
eoParam: Base class for monitoring and parsing parameters
*/
class eoParam
class eoParam
{
public:
/** Empty constructor - called from outside any parser
*/
eoParam ()
: repLongName(""), repDefault(""), repDescription(""),
repShortHand(0), repRequired(false){}
/**
* Construct a Param.
* @param _longName Long name of the argument
* @param _default The default value
* @param _description Description of the parameter. What is useful for.
* @param _shortName Short name of the argument (Optional)
* @param _required If it is a necessary parameter or not
*/
eoParam (std::string _longName, std::string _default,
std::string _description, char _shortName = 0, bool _required = false)
: repLongName(_longName), repDefault(_default),
repDescription(_description ),
repShortHand(_shortName), repRequired( _required) {}
/**
* Virtual destructor is needed.
*/
virtual ~eoParam () {};
/**
* Pure virtual function to get the value out.
*/
virtual std::string getValue ( void ) const = 0;
/** Empty constructor - called from outside any parser */
eoParam ()
: repLongName(""), repDefault(""), repDescription(""),
repShortHand(0), repRequired(false)
{}
/**
* Pure virtual function to set the value
*/
virtual void setValue(std::string _value) = 0 ;
/** Construct a Param.
*
* @param _longName Long name of the argument
* @param _default The default value
* @param _description Description of the parameter. What is useful for.
* @param _shortName Short name of the argument (Optional)
* @param _required If it is a necessary parameter or not
*/
eoParam (std::string _longName, std::string _default,
std::string _description, char _shortName = 0, bool _required = false)
: repLongName(_longName), repDefault(_default),
repDescription(_description ),
repShortHand(_shortName), repRequired( _required)
{}
/**
* Returns the short name.
*/
char shortName ( void ) const { return repShortHand; };
/**
* Returns the long name.
*/
const std::string& longName ( void ) const { return repLongName; };
/**
* Returns the description of the argument
*/
const std::string& description ( void ) const { return repDescription; };
/**
* Returns the default value of the argument
*/
const std::string& defValue ( void ) const { return repDefault; };
/**
* Sets the default value of the argument,
*/
void defValue ( std::string str ) { repDefault = str; };
/**
* ALlows to change the name (see the prefix in eoParser.h)
*/
void setLongName(std::string _longName) { repLongName = _longName;}
/**
* Virtual destructor is needed.
*/
virtual ~eoParam () {};
/**
* Pure virtual function to get the value out.
*/
virtual std::string getValue () const = 0;
/**
* Pure virtual function to set the value
*/
virtual void setValue(const std::string& _value) = 0 ;
/**
* Returns the short name.
*/
char shortName() const { return repShortHand; };
/**
* Returns the long name.
*/
const std::string& longName() const { return repLongName; };
/**
* Returns the description of the argument
*/
const std::string& description() const { return repDescription; };
/**
* Returns the default value of the argument
*/
const std::string& defValue() const { return repDefault; };
/**
* Sets the default value of the argument,
*/
void defValue( const std::string& str ) { repDefault = str; };
/**
* ALlows to change the name (see the prefix in eoParser.h)
*/
void setLongName(std::string _longName) { repLongName = _longName;}
/**
* Returns if required or not.
*/
bool required() const { return repRequired; };
/**
* Returns if required or not.
*/
bool required ( void ) const { return repRequired; };
private:
std::string repLongName;
std::string repDefault;
std::string repDescription;
char repShortHand;
bool repRequired;
char repShortHand;
bool repRequired;
};
/**
eoValueParam<ValueType>: templatized derivation of eoParam. Can be used to contain
any scalar value type. It makes use of std::strstream to get and set values. This
should be changed to std::stringstream when that class is available in g++.
eoValueParam<ValueType>: templatized derivation of eoParam. Can be used to contain
any scalar value type. It makes use of std::strstream to get and set values. This
should be changed to std::stringstream when that class is available in g++.
Note also that there is a template specialization for std::pair<double, double> and
for std::vector<double>. These stream their contents delimited with whitespace.
Note also that there is a template specialization for std::pair<double, double> and
for std::vector<double>. These stream their contents delimited with whitespace.
*/
template <class ValueType>
class eoValueParam : public eoParam
{
public :
/** Construct a Param. */
eoValueParam(void) : eoParam() {}
/**
* Construct a Param.
* @param _defaultValue The default value
* @param _longName Long name of the argument
* @param _description Description of the parameter. What is useful for.
* @param _shortName Short name of the argument (Optional)
* @param _required If it is a necessary parameter or not
*/
eoValueParam (ValueType _defaultValue,
std::string _longName,
std::string _description = "No description",
char _shortHand = 0,
bool _required = false)
: eoParam(_longName, "", _description, _shortHand, _required), repValue(_defaultValue)
{
eoParam::defValue(getValue());
}
/** Construct a Param.
*
* @param _defaultValue The default value
* @param _longName Long name of the argument
* @param _description Description of the parameter. What is useful for.
* @param _shortName Short name of the argument (Optional)
* @param _required If it is a necessary parameter or not
*/
eoValueParam(ValueType _defaultValue,
std::string _longName,
std::string _description = "No description",
char _shortHand = 0,
bool _required = false)
: eoParam(_longName, "", _description, _shortHand, _required),
repValue(_defaultValue)
{
eoParam::defValue(getValue());
}
/** Parameter value
@return parameter value
*/
ValueType& value()
{ return repValue; }
/** Parameter value
@overload
@return parameter value
*/
const ValueType& value() const
{ return repValue; }
ValueType& value() { return repValue; }
ValueType value() const { return repValue; }
std::string getValue(void) const
{
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
os << repValue;
std::ostringstream os;
os << repValue;
#else
char buf[1024];
std::ostrstream os(buf, 1023);
os << repValue;
os << std::ends;
char buf[1024];
std::ostrstream os(buf, 1023);
os << repValue;
os << std::ends;
#endif
return os.str();
}
return os.str();
}
void setValue(std::string _value)
{
void setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
std::istrstream is(_value.c_str());
#endif
is >> repValue;
}
is >> repValue;
}
protected:
//private :
ValueType repValue;
};
@ -198,14 +221,14 @@ public :
Specialization for std::string
*/
template <>
inline std::string eoValueParam<std::string>::getValue(void) const
inline std::string eoValueParam<std::string>::getValue() const
{
return repValue;
return repValue;
}
template <>
inline void eoValueParam<bool>::setValue(std::string _value)
inline void eoValueParam<bool>::setValue(const std::string& _value)
{
if (_value.empty())
{
@ -239,7 +262,7 @@ inline std::string eoValueParam<std::pair<double, double> >::getValue(void) cons
/// Because MSVC does not support partial specialization, the std::pair is a double, not a T
template <>
inline void eoValueParam<std::pair<double, double> >::setValue(std::string _value)
inline void eoValueParam<std::pair<double, double> >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
@ -264,8 +287,8 @@ inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(vo
os << repValue.size() << ' ';
for (unsigned i = 0; i < repValue.size(); ++i)
{
os << repValue[i].size() << ' ';
std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
os << repValue[i].size() << ' ';
std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
}
#ifndef HAVE_SSTREAM
@ -276,7 +299,7 @@ inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(vo
/// Because MSVC does not support partial specialization, the std::vector is a std::vector of doubles, not a T
template <>
inline void eoValueParam<std::vector<std::vector<double> > >::setValue(std::string _value)
inline void eoValueParam<std::vector<std::vector<double> > >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
@ -289,13 +312,13 @@ inline void eoValueParam<std::vector<std::vector<double> > >::setValue(std::stri
for (i = 0; i < repValue.size(); ++i)
{
unsigned sz2;
is >> sz2;
repValue[i].resize(sz2);
for (j = 0; j < sz2; ++j)
{
is >> repValue[i][j];
}
unsigned sz2;
is >> sz2;
repValue[i].resize(sz2);
for (j = 0; j < sz2; ++j)
{
is >> repValue[i][j];
}
}
}
@ -320,7 +343,7 @@ inline std::string eoValueParam<std::vector<double> >::getValue(void) const
/// Because MSVC does not support partial specialization, the std::vector is a double, not a T
template <>
inline void eoValueParam<std::vector<double> >::setValue(std::string _value)
inline void eoValueParam<std::vector<double> >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
@ -355,7 +378,7 @@ inline std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(voi
/// Because MSVC does not support partial specialization, the std::vector is a eoMinimizingFitness, not a T
// NOTE: g++ doesn support it either!!!
template <>
inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(std::string _value)
inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
@ -373,41 +396,41 @@ inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(std::strin
template <>
inline std::string eoValueParam<std::vector<void*> >::getValue(void) const
{
throw std::runtime_error("I cannot getValue for a std::vector<EOT*>");
return std::string("");
throw std::runtime_error("I cannot getValue for a std::vector<EOT*>");
return std::string("");
}
template <>
inline void eoValueParam<std::vector<void*> >::setValue(std::string)
inline void eoValueParam<std::vector<void*> >::setValue(const std::string&)
{
throw std::runtime_error("I cannot setValue for a std::vector<EOT*>");
return;
throw std::runtime_error("I cannot setValue for a std::vector<EOT*>");
return;
}
/*template <class ContainerType>
class eoContainerParam : public eoParam
{
public :
eoContainerParam (ContainerType& value, std::string _shortName, std::string _longName,
std::string _default,
std::string _description,
bool _required,
bool _change )
: value(_value), eoParam(_shortName, _longName, _description, _default, _required, _change)
{}
class eoContainerParam : public eoParam
{
public :
eoContainerParam (ContainerType& value, std::string _shortName, std::string _longName,
std::string _default,
std::string _description,
bool _required,
bool _change )
: value(_value), eoParam(_shortName, _longName, _description, _default, _required, _change)
{}
// void setValue(const std::string & _value)
// {
// std::istd::stringstream is(_value);
// copy(std::istream_iterator<Container::value_type>(is), std::istream_iterator<Container::value_type>(), back_inserter(value));
// }
// {
// std::istd::stringstream is(_value);
// copy(std::istream_iterator<Container::value_type>(is), std::istream_iterator<Container::value_type>(), back_inserter(value));
// }
private :
ContainerType& value;
};*/
private :
ContainerType& value;
};*/
/**
/**
* Another helper class for parsing parameters like
* Keyword(arg1, arg2, ...)
*
@ -420,65 +443,65 @@ private :
class eoParamParamType : public std::pair<std::string,std::vector<std::string> >
{
public:
eoParamParamType(std::string _value)
{
readFrom(_value);
}
eoParamParamType(std::string _value)
{
readFrom(_value);
}
std::ostream & printOn(std::ostream & _os) const
{
_os << first;
unsigned narg = second.size();
if (!narg)
return _os;
std::ostream & printOn(std::ostream & _os) const
{
_os << first;
unsigned narg = second.size();
if (!narg)
return _os;
// Here, we do have args
_os << "(";
if (narg == 1) // 1 arg only
{
_os << second[0] << ")" ;
return _os;
}
// and here more than 1 arg
for (unsigned i=0; i<narg-1; i++)
_os << second[i] << "," ;
_os << second[narg-1] << ")";
return _os;
}
// Here, we do have args
_os << "(";
if (narg == 1) // 1 arg only
{
_os << second[0] << ")" ;
return _os;
}
// and here more than 1 arg
for (unsigned i=0; i<narg-1; i++)
_os << second[i] << "," ;
_os << second[narg-1] << ")";
return _os;
}
std::istream & readFrom(std::istream & _is)
{
std::string value;
_is >> value;
readFrom(value);
return _is;
}
std::istream & readFrom(std::istream & _is)
{
std::string value;
_is >> value;
readFrom(value);
return _is;
}
void readFrom(std::string & _value)
{
second.resize(0); // just in case
size_t pos = _value.find('(');
if (pos >= _value.size()) // no arguments
{
first = _value;
return;
}
// so here we do have arguments
std::string t = _value.substr(pos+1);// the arguments
_value.resize(pos);
first = _value; // done for the keyword (NOTE: may be empty std::string!)
void readFrom(std::string & _value)
{
second.resize(0); // just in case
size_t pos = _value.find('(');
if (pos >= _value.size()) // no arguments
{
first = _value;
return;
}
// so here we do have arguments
std::string t = _value.substr(pos+1);// the arguments
_value.resize(pos);
first = _value; // done for the keyword (NOTE: may be empty std::string!)
// now all arguments
std::string delim(" (),");
while ( (pos=t.find_first_not_of(delim)) < t.size())
{
size_t posEnd = t.find_first_of(delim, pos);
std::string u = t.substr(pos,posEnd);//(t, pos);
/*u.resize(posEnd - pos);*/
second.push_back(u);
t = t.substr(posEnd+1);
}
}
// now all arguments
std::string delim(" (),");
while ( (pos=t.find_first_not_of(delim)) < t.size())
{
size_t posEnd = t.find_first_of(delim, pos);
std::string u = t.substr(pos,posEnd);//(t, pos);
/*u.resize(posEnd - pos);*/
second.push_back(u);
t = t.substr(posEnd+1);
}
}
};
// at the moment, the following are defined in eoParser.cpp