eoCounter?

eoEasyEA -- made it copyable again
eoEvalFunc -- added specialized eoEvalFuncCounter
eoEvolutionStrategy -- nothing much
eoGenContinue -- nothing
eoPop -- fixed nth_element_fitness
eoBitOp -- fixed error in xover
eoFileMonitor -- now appends always
eoParam -- worked around memory leak in MSC's strstream
eoParser -- changed -pconfig_file to @config_file
eoParser -- added messages instead of exception when required param is missing
eoStat -- added eoDistanceStat
t-eoFunctor -- don't know
This commit is contained in:
mac 2000-08-23 12:03:01 +00:00
commit ff108477c3
16 changed files with 222 additions and 83 deletions

View file

@ -8,34 +8,37 @@
using namespace std;
void eoFileMonitor::printHeader(std::ostream& os)
{
iterator it = vec.begin();
os << (*it)->longName();
++it;
for (; it != vec.end(); ++it)
{
os << ',' << (*it)->longName();
}
os << '\n';
}
void eoFileMonitor::printHeader()
{
// create file
ofstream os(filename.c_str());
if (!os)
{
string str = "eoFileMonitor: Could not open " + filename;
throw runtime_error(str);
}
printHeader(os);
}
eoMonitor& eoFileMonitor::operator()(void)
{
if (firsttime)
{
firsttime = false;
// create file
ofstream os(filename.c_str());
if (!os)
{
string str = "eoFileMonitor: Could not open " + filename;
throw runtime_error(str);
}
iterator it = vec.begin();
os << (*it)->longName();
++it;
for (; it != vec.end(); ++it)
{
os << ',' << (*it)->longName();
}
}
// ok, now the real saving. append to file
ofstream os(filename.c_str(), ios_base::app);
if (!os)
@ -44,15 +47,21 @@ eoMonitor& eoFileMonitor::operator()(void)
throw runtime_error(str);
}
iterator it = vec.begin();
os << '\n' << (*it)->getValue();
return operator()(os);
}
eoMonitor& eoFileMonitor::operator()(std::ostream& os)
{
iterator it = vec.begin();
os << (*it)->getValue();
for(++it; it != vec.end(); ++it)
{
os << ',' << (*it)->getValue();
}
os << '\n';
return *this;
}

View file

@ -38,13 +38,17 @@
class eoFileMonitor : public eoMonitor
{
public :
eoFileMonitor(std::string _filename, std::string _delim = ",") : filename(_filename), delim(_delim), firsttime(true) {}
eoFileMonitor(std::string _filename, std::string _delim = ",") : filename(_filename), delim(_delim) {}
eoMonitor& operator()(void);
eoMonitor& operator()(std::ostream& os);
void printHeader(void);
virtual void printHeader(std::ostream& os);
private :
std::string filename;
std::string delim;
bool firsttime;
std::string filename;
};
#endif

View file

@ -153,7 +153,8 @@ public :
std::string getValue(void) const
{
std::ostrstream os;
char buf[1024];
std::ostrstream os(buf, 1023);
os << repValue;
os << std::ends;
return os.str();

View file

@ -39,7 +39,6 @@ eoParameterLoader::~eoParameterLoader()
eoParser::eoParser ( int _argc, char **_argv , string _programDescription, string _lFileParamName, char _shortHand) :
programName( _argv[0]),
programDescription( _programDescription),
parameterFile("", _lFileParamName, "Load using a configuration file", _shortHand),
needHelp(false, "help", "Prints this message", 'h')
{
strstream stream;
@ -51,15 +50,7 @@ eoParser::eoParser ( int _argc, char **_argv , string _programDescription, strin
readFrom(stream);
processParam(parameterFile);
processParam(needHelp);
if (parameterFile.getValue() != parameterFile.defValue())
{
ifstream is (parameterFile.getValue().c_str());
readFrom(is);
}
}
void eoParser::processParam(eoParam& param, std::string section)
@ -72,7 +63,8 @@ void eoParser::doRegisterParam(eoParam& param) const
{
if (param.required() && !isItThere(param))
{
throw std::runtime_error("required parameter missing");
string msg = "required parameter: " + param.longName() + " missing";
messages.push_back(msg);
}
pair<bool, string> value = getValue(param);
@ -165,6 +157,24 @@ void eoParser::readFrom(istream& is)
shortNameMap[str[1]] = value;
}
}
if (str[0] == '@')
{ // read response file
string filename(str.begin() + 1, str.end());
ifstream ifs (filename.c_str());
ifs.peek(); // check if it exists
if (!ifs)
{
string msg = "Could not open response file: " + filename;
throw runtime_error(msg);
}
// read and overwrite
readFrom(ifs);
break; // stop reading command line
}
}
updateParameters();
@ -213,7 +223,14 @@ void eoParser::printOn(ostream& os) const
}
void eoParser::printHelp(ostream& os)
{
{
if (needHelp.value() == false && !messages.empty())
{
std::copy(messages.begin(), messages.end(), ostream_iterator<string>(os, "\n"));
messages.clear();
return;
}
// print program name and description
os << this->programName <<": "<< programDescription << "\n\n";
@ -253,6 +270,7 @@ void eoParser::printHelp(ostream& os)
os <<". By default: "<<p->second->defValue() << '\n';
} // for p
os << "\n@param_file \t defines a file where the parameters are stored\n";
os << '\n';
}

View file

@ -127,7 +127,7 @@ public:
std::string className(void) const { return "Parser"; }
/// true if the user made an error or asked for help
bool userNeedsHelp(void) const { return needHelp.value(); }
bool userNeedsHelp(void) const { return needHelp.value() || !messages.empty(); }
/**
* Prints an automatic help in the specified output using the information
@ -157,8 +157,9 @@ private:
map<char, string> shortNameMap;
map<string, string> longNameMap;
eoValueParam<string> parameterFile;
eoValueParam<bool> needHelp;
mutable std::vector<std::string> messages;
};

View file

@ -104,19 +104,76 @@ public :
};
template <class EOT>
class eoBestFitnessStat : public eoStat<EOT, typename EOT::Fitness >
class eoNthElementFitnessStat : public eoStat<EOT, typename EOT::Fitness >
{
public :
typedef typename EOT::Fitness Fitness;
eoBestFitnessStat(std::string _description = "Best Fitness") : eoStat<EOT, Fitness>(Fitness(), _description) {}
eoNthElementFitnessStat(int _which, std::string _description = "nth element fitness") : which(_which), eoStat<EOT, Fitness>(Fitness(), _description) {}
virtual void operator()(const eoPop<EOT>& _pop)
{
value() = _pop.nth_element_fitness(0);
if (which > _pop.size())
throw logic_error("fitness requested of element outside of pop");
value() = _pop.nth_element_fitness(which);
}
private :
unsigned which;
};
template <class EOT>
class eoBestFitnessStat : public eoNthElementFitnessStat<EOT>
{
public :
typedef typename EOT::Fitness Fitness;
eoBestFitnessStat(std::string _description = "Best Fitness") : eoNthElementFitnessStat<EOT>(0, _description) {}
};
template <class EOT>
class eoDistanceStat : public eoStat<EOT, double>
{
public :
eoDistanceStat(std::string _name = "distance") : eoStat<EOT, double>(0.0, _name) {}
template <class T>
double distance(T a, T b)
{
T res = a-b;
return res < 0? -res : res;
}
double distance(bool a, bool b)
{
return (a==b)? 0 : 1;
}
void operator()(const eoPop<EOT>& _pop)
{
double& v = value();
v = 0.0;
for (unsigned i = 0; i < _pop.size(); ++i)
{
for (unsigned j = 0; j < _pop.size(); ++j)
{
for (unsigned k = 0; k < _pop[i].size(); ++k)
{
v += distance(_pop[i][k], _pop[j][k]);
}
}
}
double sz = _pop.size();
v /= sz * sz * _pop[0].size();
}
};
/*
template <class EOT>
class eoStdevStat : public eoStat<EOT, double >