add a 'found' method to eoEvalKeepBest, to test wether a best solution was evaluated
This commit is contained in:
parent
740275b31a
commit
f2950521a4
1 changed files with 15 additions and 3 deletions
|
|
@ -23,6 +23,8 @@
|
||||||
#ifndef eoEvalKeepBest_H
|
#ifndef eoEvalKeepBest_H
|
||||||
#define eoEvalKeepBest_H
|
#define eoEvalKeepBest_H
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <eoEvalFunc.h>
|
#include <eoEvalFunc.h>
|
||||||
#include <utils/eoParam.h>
|
#include <utils/eoParam.h>
|
||||||
|
|
||||||
|
|
@ -89,27 +91,31 @@ template<class EOT> class eoEvalKeepBest : public eoEvalFunc<EOT>, public eoValu
|
||||||
* This constructor uses a dummy evaluator.
|
* This constructor uses a dummy evaluator.
|
||||||
*/
|
*/
|
||||||
eoEvalKeepBest( std::string _name = "VeryBest. " )
|
eoEvalKeepBest( std::string _name = "VeryBest. " )
|
||||||
: eoValueParam<EOT>(EOT(), _name), dummy_eval(), func(dummy_eval) {}
|
: eoValueParam<EOT>(EOT(), _name), dummy_eval(), func(dummy_eval), _found(false) {}
|
||||||
|
|
||||||
|
|
||||||
/** A constructor for wrapping your own evaluator in a eoEvalKeepBest.
|
/** A constructor for wrapping your own evaluator in a eoEvalKeepBest.
|
||||||
*/
|
*/
|
||||||
eoEvalKeepBest(eoEvalFunc<EOT>& _func, std::string _name = "VeryBest. ")
|
eoEvalKeepBest(eoEvalFunc<EOT>& _func, std::string _name = "VeryBest. ")
|
||||||
: eoValueParam<EOT>(EOT(), _name), dummy_eval(), func(_func) {}
|
: eoValueParam<EOT>(EOT(), _name), dummy_eval(), func(_func), _found(false) {}
|
||||||
|
|
||||||
virtual void operator()(EOT& sol)
|
virtual void operator()(EOT& sol)
|
||||||
{
|
{
|
||||||
|
_found = false;
|
||||||
|
|
||||||
func(sol); // evaluate
|
func(sol); // evaluate
|
||||||
assert( ! sol.invalid() );
|
assert( ! sol.invalid() );
|
||||||
|
|
||||||
// if there is no best kept
|
// if there is no best kept
|
||||||
if( this->value().invalid() ) {
|
if( this->value().invalid() ) {
|
||||||
|
// FIXME replace this test by a reference fitness passed at instanciation
|
||||||
// take the first individual as best
|
// take the first individual as best
|
||||||
this->value() = sol;
|
this->value() = sol;
|
||||||
} else {
|
} else {
|
||||||
// if sol is better than the kept individual
|
// if sol is better than the kept individual
|
||||||
if( sol.fitness() > this->value().fitness() ) {
|
if( sol.fitness() > this->value().fitness() ) {
|
||||||
this->value() = sol;
|
this->value() = sol;
|
||||||
|
_found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -128,6 +134,12 @@ template<class EOT> class eoEvalKeepBest : public eoEvalFunc<EOT>, public eoValu
|
||||||
this->value() = new_best;
|
this->value() = new_best;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Tell if a best individual has been found during the last call
|
||||||
|
bool found() const
|
||||||
|
{
|
||||||
|
return _found;
|
||||||
|
}
|
||||||
|
|
||||||
class DummyEval : public eoEvalFunc<EOT>
|
class DummyEval : public eoEvalFunc<EOT>
|
||||||
{
|
{
|
||||||
void operator()(EOT& sol) {/*empty*/}
|
void operator()(EOT& sol) {/*empty*/}
|
||||||
|
|
@ -136,6 +148,6 @@ template<class EOT> class eoEvalKeepBest : public eoEvalFunc<EOT>, public eoValu
|
||||||
protected :
|
protected :
|
||||||
DummyEval dummy_eval;
|
DummyEval dummy_eval;
|
||||||
eoEvalFunc<EOT>& func;
|
eoEvalFunc<EOT>& func;
|
||||||
|
bool _found;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue