Added symbolic regression, which tests combined ops as well

This commit is contained in:
mac 2000-08-18 08:44:22 +00:00
commit 8ae796ab28
14 changed files with 161 additions and 199 deletions

View file

@ -4,7 +4,7 @@
##
###############################################################################
SUBDIRS = es ga utils other
SUBDIRS = es ga gp utils other
lib_LIBRARIES = libeo.a
libeo_a_SOURCES = eoPrintable.cpp eoPersistent.cpp eoFunctorStore.cpp

View file

@ -24,7 +24,7 @@ class eoGOpBreeder: public eoUnaryFunctor<void, eoPop<EOT>&>
public:
/// Default constructor.
eoGOpBreeder( eoGOpSelector<EOT>& _opSel,
eoPopIndiSelector<EOT>& _selector)
eoSelectOneIndiSelector<EOT>& _selector)
: opSel( _opSel ), selector(_selector)
{}
@ -47,7 +47,7 @@ class eoGOpBreeder: public eoUnaryFunctor<void, eoPop<EOT>&>
private:
eoGOpSelector<EOT>& opSel;
eoPopIndiSelector<EOT>& selector;
eoSelectOneIndiSelector<EOT>& selector;
// the inserter can be local as there's no point in changing it from the outside
eoBackInserter<EOT> inserter;

View file

@ -185,78 +185,5 @@ class eoPop: public vector<EOT>, public eoObject, public eoPersistent
};
/**
Keeps and calculates information about a population such
as the sum of fitnesses and whether the population is sorted.
It can be used to cache information about a population between
calls of functors, see for instance how this makes eoProportional
just this tat more efficient
@see eoSelectOne, eoSelect, eoProportional
template <class EOT>
class eoPopStats
{
public :
typedef typename EOT::Fitness FitnessType;
/// Initialize by stating nothing is known
eoPopStats() :
sum_fitness_calculated(false),
pop_sorted_calculated(false)
{}
/// Call this function after the pop might have/is changed
void reset()
{
sum_fitness_calculated = false;
pop_sorted_calculated = false;
}
/// Calculate sum_fitness or return cached value
FitnessType get_sum_fitness(const eoPop<EOT>& _pop)
{
if (sum_fitness_calculated)
return sum_fitness;
sum_fitness = 0.0;
for (int i = 0; i < _pop.size(); ++i)
{
sum_fitness += _pop[i].fitness();
}
sum_fitness_calculated = true;
return sum_fitness;
}
/// Check if the pop is sorted or return cached_value
bool is_pop_sorted(const eoPop<EOT>& _pop)
{
if (pop_sorted_calculated)
return pop_sorted;
int i;
for (i = 1; i < _pop.size(); ++i)
{
if (!(_pop[i-1] < _pop[i]))
{ // not in sort order
break;
}
}
pop_sorted = (i == _pop.size());
pop_sorted_calculated = true;
return pop_sorted;
}
bool sum_fitness_calculated;
FitnessType sum_fitness;
bool pop_sorted_calculated;
bool pop_sorted;
};
*/
#endif

View file

@ -40,12 +40,10 @@ template <class EOT>
class eoSequentialGOpSel : public eoGOpSelector<EOT>
{
public :
eoSequentialGOpSel(void) : combined(*this, getRates()) {}
virtual eoGeneralOp<EOT>& selectOp()
{
return combined;
return combined.bind(*this, getRates());
}
private :

View file

@ -65,7 +65,7 @@ template<class EOT> class eoSteadyStateTransform: public eoTransform<EOT>
nSteps = pop.size(); // make a 'generation equivalent'
}
for (unsigned i = 0; i < steps; ++i)
for (unsigned i = 0; i < nSteps; ++i)
{
selector.bind(pop);
inserter.bind(pop);
@ -77,7 +77,7 @@ template<class EOT> class eoSteadyStateTransform: public eoTransform<EOT>
private:
eoGOpSelector<EOT>& opSelector;
eoSelectOneIndiSelector<EOT>& selector;
eoSelectOneIndiSelector<EOT> selector;
eoSteadyStateInserter<EOT>& inserter;
unsigned steps;
};

View file

@ -117,8 +117,12 @@ template <class EOT>
class eoCombinedOp : public eoGeneralOp<EOT>
{
public :
eoCombinedOp(const std::vector<eoGeneralOp<EOT>*>& _ops, const std::vector<float>& rates)
: ops(_ops), rates(_rates) {}
eoCombinedOp& bind(const std::vector<eoGeneralOp<EOT>*>& _ops, const std::vector<float>& _rates)
{
ops = &_ops;
rates = &_rates;
return *this;
}
class eoDelayedSelector : public eoIndiSelector<EOT>
{
@ -147,7 +151,7 @@ class eoCombinedOp : public eoGeneralOp<EOT>
};
/** Applies all ops in the combined op
It works in the following way
It first applies the
*/
void operator()( eoIndiSelector<EOT>& _in,
eoInserter<EOT>& _out )
@ -156,7 +160,7 @@ class eoCombinedOp : public eoGeneralOp<EOT>
eoPop<EOT> next;
unsigned i;
for (i = 0; i < ops.size(); ++i)
for (i = 0; i < ops->size(); ++i)
{
eoDelayedSelector delay(_in, intermediate);
inserter.bind(next);
@ -166,13 +170,13 @@ class eoCombinedOp : public eoGeneralOp<EOT>
// apply operators until we have as many outputs as inputs
do
{
if (flip(rates[i])) // should this flip be here?
(*ops[i])(delayedSelector, inserter);
if (rng.flip(rates->operator[](i))) // should this flip be here?
(*ops->operator[](i))(delay, inserter);
counter++;
if (counter > 1000)
{
throw logical_error("eoCombinedOp: no termination after 1000 tries, did you forget to insert individuals in your eoGeneralOp?");
throw logic_error("eoCombinedOp: no termination after 1000 tries, did you forget to insert individuals in your eoGeneralOp?");
}
}
while (next.size() < intermediate.size());
@ -187,8 +191,8 @@ class eoCombinedOp : public eoGeneralOp<EOT>
}
private :
const std::vector<eoGeneralOp<EOT>*>& ops;
const std::vector<float> rates;
const std::vector<eoGeneralOp<EOT>*>* ops;
const std::vector<float>* rates;
eoBackInserter<EOT> inserter;
};

View file

@ -17,19 +17,12 @@ template <class FType, class Node>
class eoParseTree : public EO<FType>, public parse_tree<Node>
{
public :
typedef parse_tree<Node>::subtree Subtree;
eoParseTree(void) {}
eoParseTree(const parse_tree<Node>& tree) : parse_tree<Node>(tree) {}
typedef typename parse_tree<Node>::subtree Type;
eoParseTree(void) : EO<FType>(), parse_tree<Node>() {}
eoParseTree(unsigned _size, eoRnd<Type>& _rnd)
: EO<FType>(), parse_tree<Node>(_rnd())
{
pruneTree(_size);
}
eoParseTree(eoRnd<Type>& _rnd)
: EO<FType>(), parse_tree<Node>(_rnd())
{}
virtual void pruneTree(unsigned _size)
{
if (_size < 1)
@ -37,7 +30,7 @@ public :
if (size() > _size)
{
Type* sub = &operator[](size() - 2); // prune tree
Subtree* sub = &operator[](size() - 2); // prune tree
while (sub->size() > _size)
{
@ -101,7 +94,7 @@ class eoGpDepthInitializer : public eoInit< eoParseTree<FType, Node> >
const vector<Node>& _initializor,
bool _grow = true)
:
eoRnd<EoType::Type>(),
eoInit<EoType>(),
max_depth(_max_depth),
initializor(_initializor),
grow(_grow)
@ -115,7 +108,8 @@ class eoGpDepthInitializer : public eoInit< eoParseTree<FType, Node> >
generate(sequence, max_depth);
_tree = parse_tree<Node>(sequence.begin(), sequence.end());
parse_tree<Node> tmp(sequence.begin(), sequence.end());
_tree.swap(tmp);
}
void generate(list<Node>& sequence, int the_max, int last_terminal = -1)
@ -180,10 +174,10 @@ public:
/// Dtor
virtual ~eoSubtreeXOver () {};
void operator()(eoIndiSelector<EoType>& _source, eoInserter<EoType>& _sink ) const
void operator()(eoIndiSelector<EoType>& _select, eoInserter<EoType>& _insert )
{
EoType eo1 = _source.select();
const EoType& eo2 = _source.select();
EoType eo1 = _select();
const EoType& eo2 = _select();
int i = rng.random(eo1.size());
int j = rng.random(eo2.size());
@ -193,7 +187,7 @@ public:
eo1.pruneTree(max_length);
eo1.invalidate();
_sink.insert(eo1);
_insert(eo1);
}
unsigned max_length;
@ -206,7 +200,7 @@ public:
typedef eoParseTree<FType, Node> EoType;
eoBranchMutation(eoRnd<EoType::Type>& _init, unsigned _max_length)
eoBranchMutation(eoInit<EoType>& _init, unsigned _max_length)
: eoGeneralOp<EoType>(), max_length(_max_length), initializer(_init)
{};
@ -215,25 +209,28 @@ public:
/// Dtor
virtual ~eoBranchMutation() {};
void operator()(eoIndiSelector<EoType>& _source, eoInserter<EoType>& _sink ) const
void operator()(eoIndiSelector<EoType>& _select, eoInserter<EoType>& _insert )
{
EoType eo1 = _source.select();
EoType eo1 = _select();
int i = rng.random(eo1.size());
EoType eo2(eo1[i].size(), initializer); // create random other to cross with
EoType eo2;
initializer(eo2);
eo1[i] = eo2.back(); // insert subtree
int j = rng.random(eo2.size());
eo1[i] = eo2[j]; // insert subtree
eo1.pruneTree(max_length);
eo1.invalidate();
_sink.insert(eo1);
_insert(eo1);
}
private :
unsigned max_length;
eoRnd<EoType::Type>& initializer;
eoInit<EoType>& initializer;
};

View file

@ -148,7 +148,7 @@ public :
if (arity == 0)
return 0;
T* t new T [arity];
T* t = new T [arity];
for (int i = 0; i < arity; ++i)
{

View file

@ -16,15 +16,6 @@
* modified is included with the above copyright notice.
* Special disclaimer and political statement:
* In contrast with the rest of the EO package where you might have found this code, this software
* does NOT fall under the GNU Lesser Public License or the GNU Public License, nor is anyone allowed
* by the copyright holder (that's me) to put it under either license.
* Doing this would limit my and your freedom to use this software in any way
* you or I see fit, including but not limited to closed-source software.
Usage information.
@ -173,7 +164,7 @@ namespace gp_parse_tree
#include "node_pool.h"
/// This ones defined because gcc does not always implement namespaces
template <class T>
inline void do_the_swap(T& a, T& b)
{
@ -371,21 +362,20 @@ public :
void swap(subtree& y)
{
do_the_swap(content, y.content);
do_the_swap(args, y.args);
do_the_swap(parent, y.parent);
do_the_swap(content, y.content);
do_the_swap(args, y.args);
adopt();
y.adopt();
do_the_swap(_cumulative_size, y._cumulative_size);
do_the_swap(_depth, y._depth);
do_the_swap(_size, y._size);
do_the_swap(parent, y.parent);
do_the_swap(_cumulative_size, y._cumulative_size);
do_the_swap(_depth, y._depth);
do_the_swap(_size, y._size);
updateAfterInsert();
}
friend void swap(subtree& x, subtree& y)
{
x.swap(y);
}
protected :
virtual void updateAfterInsert(void)
@ -973,7 +963,7 @@ ptrdiff_t* distance_type(gp_parse_tree::parse_tree<T>::iterator)
return 0;
}
// Put customized swaps also in std...
/* Put customized swaps also in std...
template<class T> inline
void swap(gp_parse_tree::parse_tree<T>& a, gp_parse_tree::parse_tree<T>& b)
@ -985,7 +975,7 @@ template<class T> inline
void iter_swap(vector<gp_parse_tree::parse_tree<T> >::iterator a, vector<gp_parse_tree::parse_tree<T> > b)
{
a->swap(*b);
}
}*/
} // namespace std

View file

@ -59,7 +59,7 @@ template <class EOT>
class eoAverageStat : public eoStat<EOT, double>
{
public :
eoAverageStat(std::string _description = "AverageFitness") : eoStat<EOT, double>(0.0, _description) {}
eoAverageStat(std::string _description = "Average Fitness") : eoStat<EOT, double>(0.0, _description) {}
static double sumFitness(double _sum, const EOT& _eot)
{
@ -103,6 +103,20 @@ public :
}
};
template <class EOT>
class eoBestFitnessStat : public eoStat<EOT, typename EOT::Fitness >
{
public :
typedef typename EOT::Fitness Fitness;
eoBestFitnessStat(std::string _description = "Best Fitness") : eoStat<EOT, Fitness>(Fitness(), _description) {}
virtual void operator()(const eoPop<EOT>& _pop)
{
value() = _pop.nth_element_fitness(0);
}
};
/*
template <class EOT>
class eoStdevStat : public eoStat<EOT, double >