Replaced stupid eoRateParam by eoValueParam<eoHowMnay>

Had to transform eoHowMany into an eoPersistent ...
This commit is contained in:
evomarc 2001-05-07 07:13:36 +00:00
commit 0aa6a235ef
7 changed files with 88 additions and 129 deletions

View file

@ -26,20 +26,22 @@
#ifndef eoHowMany_h
#define eoHowMany_h
// to be used in selection / replacement procedures to indicate whether
// the argument (rate, a double) shoudl be treated as a rate (number=rate*popSize)
// or as an absolute integer (number=rate regardless of popsize).
// the default value shoudl ALWAYS be true (eo_as_a_rate).
//
// this construct is mandatory because in some cases you might not know the
// population size that will enter the replacement for instance - so you
// cannot simply have a pre-computed (double) rate of 1/popSize if you want 1 guy
/**
* to be used in selection / replacement procedures to indicate whether
* the argument (rate, a double) shoudl be treated as a rate (number=rate*popSize)
* or as an absolute integer (number=rate regardless of popsize).
* the default value shoudl ALWAYS be true (eo_as_a_rate).
*
* this construct is mandatory because in some cases you might not know the
* population size that will enter the replacement for instance - so you
* cannot simply have a pre-computed (double) rate of 1/popSize if you want 1 guy
*/
class eoHowMany
class eoHowMany : public eoPersistent
{
public:
eoHowMany(double _rate, bool _interpret_as_rate = true):
/** Original Ctor from direct rate + bool */
eoHowMany(double _rate = 0.0, bool _interpret_as_rate = true):
rate(0), combien(0)
{
if (_interpret_as_rate)
@ -56,17 +58,61 @@ public:
}
}
/// Virtual dtor. They are needed in virtual class hierarchies.
virtual ~eoHowMany() {}
unsigned int operator()(unsigned int _size)
{
if (combien == 0)
{
if (rate == 0.0)
return 0;
else
return (unsigned int) (rate * _size);
return (unsigned int) (rate * _size);
}
return combien;
}
virtual void printOn(ostream& _os) const
{
if (combien == 0)
_os << 100*rate << "% " << std::ends;
else
_os << combien << " " << std::ends;
return;
}
virtual void readFrom(istream& _is)
{
string value;
_is >> value;
readFrom(value);
return;
}
void readFrom(std::string _value)
{
// check for %
bool interpret_as_rate = false; // == no %
size_t pos = _value.find('%');
if (pos < _value.size()) // found a %
{
interpret_as_rate = true;
_value.resize(pos); // get rid of %
}
std::istrstream is(_value.c_str());
is >> rate;
// now store
if (interpret_as_rate)
{
combien = 0;
rate /= 100.0;
}
else
combien = unsigned(rate); // and rate will not be used
// minimal check
if ( (combien <= 0) && (rate <= 0.0) )
throw runtime_error("Invalid parameters read in eoHowMany::readFrom");
}
private :
double rate;

View file

@ -297,97 +297,6 @@ void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(std::string _valu
std::copy(std::istream_iterator<eoMinimizingFitness>(is), std::istream_iterator<eoMinimizingFitness>(), repValue.begin());
}
/* ABANDONNED - See class eoRateType below
///////////////////////////////////////
Specialization for "rate_or_absolute-number"
typedef std::pair<double,bool> eoRateType;
template <>
std::string eoValueParam<eoRateType>::getValue(void) const
{
std::ostrstream os;
if (repValue.second)
os << 100*repValue.first << "% " << std::ends;
else
os << repValue.first << " " << std::ends;
return os.str();
}
template <>
void eoValueParam<eoRateType>::setValue(std::string _value)
{
double rate;
// check for %
bool interpret_as_rate = false; // == no %
size_t pos = _value.find('%');
if (pos < _value.size()) // found a %
{
interpret_as_rate = true;
_value.resize(pos); // get rid of %
}
std::istrstream is(_value.c_str());
is >> rate;
repValue.first = (interpret_as_rate ? rate/100 : floor(rate));
repValue.second = interpret_as_rate;
}
*/
/**
* A helper class for the parsing of parameters that can be either a rate
* or an absolute value (integer)
* See eoHowMany.h
*/
class eoRateParamType : public std::pair<double,bool>
{
public:
eoRateParamType(std::pair<double,bool> _p) : std::pair<double,bool>(_p) {}
eoRateParamType(std::string _value)
{
readFrom(_value);
}
ostream & printOn(ostream & _os) const
{
if (second)
_os << 100*first << "% " << std::ends;
else
_os << first << " " << std::ends;
return _os;
}
istream & readFrom(istream & _is)
{
string value;
_is >> value;
readFrom(value);
return _is;
}
void readFrom(std::string _value)
{
double rate;
// check for %
bool interpret_as_rate = false; // == no %
size_t pos = _value.find('%');
if (pos < _value.size()) // found a %
{
interpret_as_rate = true;
_value.resize(pos); // get rid of %
}
std::istrstream is(_value.c_str());
is >> rate;
first = (interpret_as_rate ? rate/100 : floor(rate));
second = interpret_as_rate;
}
};
// at the moment, the following are defined in eoParser.cpp
ostream & operator<<(ostream & _os, const eoRateParamType & _rate);
istream & operator>>(istream & _is, eoRateParamType & _rate);
/*template <class ContainerType>
class eoContainerParam : public eoParam
{

View file

@ -353,18 +353,6 @@ bool eoParser::userNeedsHelp(void)
}
///////////////// I put these here at the moment
ostream & operator<<(ostream & _os, const eoRateParamType & _rate)
{
_rate.printOn(_os);
return _os;
}
istream & operator>>(istream & _is, eoRateParamType & _rate)
{
_rate.readFrom(_is);
return _is;
}
ostream & operator<<(ostream & _os, const eoParamParamType & _rate)
{
_rate.printOn(_os);