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

@ -109,6 +109,8 @@
// aliens
//#include <eoNonUniform.h>
#include <eoCounter.h>
#include <utils/eoParser.h>
//-----------------------------------------------------------------------------
// to be continued ...

View file

@ -55,9 +55,9 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
eoReplacement<EOT>& _replace
) : continuator(_continuator),
eval(_eval),
selectTransform(0),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(0),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
@ -70,9 +70,9 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
eoReduce<EOT>& _reduce
) : continuator(_continuator),
eval(_eval),
selectTransform(0),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(new eoMergeReduce<EOT>(_merge, _reduce)),
mergeReduce(_merge, _reduce),
replace(mergeReduce)
{}
@ -85,9 +85,9 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
eoReplacement<EOT>& _replace
) : continuator(_continuator),
eval(_eval),
selectTransform(new eoSelectTransform<EOT>(_select, _transform)),
selectTransform(_select, _transform),
breed(selectTransform),
mergeReduce(0),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
@ -101,17 +101,15 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
eoReduce<EOT>& _reduce
) : continuator(_continuator),
eval(_eval),
selectTransform(new eoSelectTransform<EOT>(_select, _transform)),
breed(*selectTransform),
mergeReduce(new eoMergeReduce<EOT>(_merge, _reduce)),
replace(*mergeReduce)
selectTransform(_select, _transform),
breed(selectTransform),
mergeReduce(_merge, _reduce),
replace(mergeReduce)
{}
~eoEasyEA() { delete selectTransform; delete mergeReduce; }
/// Apply a few generation of evolution to the population.
virtual void operator()(eoPop<EOT>& _pop)
{
@ -146,16 +144,27 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
private:
/// dissallow copying cuz of pointer stuff
eoEasyEA(const eoEasyEA&);
/// dissallow copying cuz of pointer stuff
const eoEasyEA& operator=(const eoEasyEA&);
// If selectTransform needs not be used, dummySelect and dummyTransform are used
// to instantiate it.
class eoDummySelect : public eoSelect<EOT>
{ public : void operator()(const eoPop<EOT>&, eoPop<EOT>&) {} } dummySelect;
class eoDummyTransform : public eoTransform<EOT>
{ public : void operator()(eoPop<EOT>&) {} } dummyTransform;
eoContinue<EOT>& continuator;
eoEvalFunc<EOT>& eval;
eoSelectTransform<EOT>* selectTransform;
eoSelectTransform<EOT> selectTransform;
eoBreed<EOT>& breed;
eoMergeReduce<EOT>* mergeReduce;
// If mergeReduce needs not be used, dummyMerge and dummyReduce are used
// to instantiate it.
eoNoElitism<EOT> dummyMerge;
eoTruncate<EOT> dummyReduce;
eoMergeReduce<EOT> mergeReduce;
eoReplacement<EOT>& replace;
};

View file

@ -44,4 +44,30 @@ template<class EOT> class eoEvalFunc : public eoUnaryFunctor<void, EOT&>
typedef typename EOT::Fitness EOFitT;
};
/**
Counts the number of evaluations actually performed, thus checks first
if it has to evaluate.. etc.
*/
#include <utils/eoParam.h>
template<class EOT> class eoEvalFuncCounter : public eoEvalFunc<EOT>, public eoValueParam<unsigned long>
{
public :
eoEvalFuncCounter(eoEvalFunc<EOT>& _func, std::string _name = "eval_counter")
: func(_func), eoValueParam<unsigned long>(0, _name) {}
void operator()(EOT& _eo)
{
if (_eo.invalid())
{
value()++;
func(_eo);
}
}
private :
eoEvalFunc<EOT>& func;
};
#endif

View file

@ -81,6 +81,20 @@ class eoEvolutionStrategy: public eoAlgo<EOT>
eoEasyEA<EOT> easyEA;
};
template <class EOT>
eoEvolutionStrategy<EOT> make_es(eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoGOpSelector<EOT>& _opSel,
float _lambdaRate,
bool _comma)
{
if (_comma)
return eoEvolutionStrategy<EOT>(_continuator, _eval, _opSel, _lambdaRate, eoEvolutionStrategy<EOT>::comma_strategy());
//else
return eoEvolutionStrategy<EOT>(_continuator, _eval, _opSel, _lambdaRate, eoEvolutionStrategy<EOT>::plus_strategy());
}
//-----------------------------------------------------------------------------
#endif eoSelectTransformReduce_h

View file

@ -36,13 +36,13 @@ class eoGenContinue: public eoContinue<EOT>
public:
/// Ctor for setting a
eoGenContinue( unsigned _totalGens)
eoGenContinue( unsigned long _totalGens)
: repTotalGenerations( _totalGens ),
thisGenerationPlaceHolder(0),
thisGeneration(thisGenerationPlaceHolder){};
/// Ctor for enabling the save/load the no. of generations counted
eoGenContinue( unsigned _totalGens, unsigned& _currentGen)
eoGenContinue( unsigned long _totalGens, unsigned long& _currentGen)
: repTotalGenerations( _totalGens ),
thisGenerationPlaceHolder(0),
thisGeneration(_currentGen){};
@ -57,21 +57,21 @@ public:
/** Sets the number of generations to reach
and sets the current generation to 0 (the begin)*/
virtual void totalGenerations( unsigned _tg ) {
virtual void totalGenerations( unsigned long _tg ) {
repTotalGenerations = _tg;
thisGeneration = 0;
};
/** Returns the number of generations to reach*/
virtual unsigned totalGenerations( )
virtual unsigned long totalGenerations( )
{
return repTotalGenerations;
};
private:
unsigned repTotalGenerations;
unsigned thisGenerationPlaceHolder;
unsigned& thisGeneration;
unsigned long repTotalGenerations;
unsigned long thisGenerationPlaceHolder;
unsigned long& thisGeneration;
};
#endif

View file

@ -113,11 +113,12 @@ class eoPop: public vector<EOT>, public eoObject, public eoPersistent
struct GetFitness { Fitness operator()(const EOT& _eo) const { return _eo.fitness(); } };
Fitness nth_element_fitness(int which) const
{
{ // probably not the fastest way to do this, but what the heck
vector<Fitness> fitness(size());
std::transform(begin(), end(), fitness.begin(), GetFitness());
vector<Fitness>::iterator it = fitness.begin();
vector<Fitness>::iterator it = fitness.begin() + which;
std::nth_element(fitness.begin(), it, fitness.end(), greater<Fitness>());
return *it;
}

View file

@ -192,7 +192,7 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
{
unsigned site = rng.random(min(chrom1.size(), chrom2.size()));
if (std::equal(chrom1.begin(), chrom1.begin()+site, chrom2.begin()))
if (!std::equal(chrom1.begin(), chrom1.begin()+site, chrom2.begin()))
{
swap_ranges(chrom1.begin(), chrom1.begin() + site, chrom2.begin());
@ -267,6 +267,7 @@ template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
};
/** eoBinGxOver --> gene crossover
\class eoBinGxOver eoBitOp.h ga/eoBitOp.h
\ingroup bitstring

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 >

View file

@ -18,7 +18,6 @@ public :
};
#include <iostream>
#include <eoTranspose.h>
#include <eoFixedLength.h>
#include <eoVariableLength.h>
@ -46,8 +45,5 @@ int main(void)
eo.push_back(1);
eo.push_back(2);
eoTranspose<EoType> transpose;
transpose(eo);
return 1;
}

View file

@ -41,7 +41,7 @@ RSC=rc.exe
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
# ADD CPP /nologo /W3 /GR /GX /O2 /I "../src" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
# ADD CPP /nologo /W3 /GR /GX /O2 /I "../src" /I "eo/src" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0xc0a /d "NDEBUG"
# ADD RSC /l 0xc0a /d "NDEBUG"

View file

@ -41,15 +41,15 @@ RSC=rc.exe
# PROP Intermediate_Dir "t_eoFunctor___Win32_Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../src" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "t_eoFunctor - Win32 Debug"
@ -63,16 +63,16 @@ LINK32=link.exe
# PROP Output_Dir "t_eoFunctor___Win32_Debug"
# PROP Intermediate_Dir "t_eoFunctor___Win32_Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../src" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../src" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF