Added lambda expression (user/automatically defined functions

This commit is contained in:
maartenkeijzer 2005-10-16 15:38:38 +00:00
commit b4f15601cb
9 changed files with 240 additions and 49 deletions

View file

@ -20,21 +20,31 @@
#include <utils/eoRNG.h>
unsigned RandomNodeSelector::select_node(Sym sym) const {
return rng.random(sym.size());
// If subtree is not set (by randomnodeselector for instance, find it now
Sym NodeSelector::NodeSelection::subtree() {
if (subtree_.empty()) {
subtree_ = get_subtree(root_, subtree_index_);
}
return subtree_;
}
unsigned BiasedNodeSelector::select_node(Sym sym) const {
NodeSelector::NodeSelection RandomNodeSelector::select_node(Sym sym) const {
unsigned idx = rng.random(sym.size());
return NodeSelection(sym, idx, Sym() ); // empty subtree, find it when needed
}
NodeSelector::NodeSelection BiasedNodeSelector::select_node(Sym sym) const {
unsigned p = rng.random(sym.size());
Sym res;
for (unsigned i = 0; i < nRounds; ++i) {
Sym res = get_subtree(sym, p);
res = get_subtree(sym, p);
if (res.args().size() > 0) break;
p = rng.random(sym.size());
}
return p;
return NodeSelection(sym, p, res);
}

View file

@ -18,21 +18,37 @@
#ifndef NODESELECTOR_H
#define NODESELECTOR_H
class Sym;
#include <Sym.h>
/** Base class for selecting nodes */
class NodeSelector {
public:
class NodeSelection {
Sym root_;
unsigned subtree_index_;
Sym subtree_;
public :
NodeSelection(Sym r, unsigned idx, Sym s)
: root_(r), subtree_index_(idx), subtree_(s) {}
Sym root() const { return root_; }
unsigned idx() const { return subtree_index_; }
Sym subtree();
};
virtual ~NodeSelector() {}
virtual unsigned select_node(Sym sym) const = 0;
virtual NodeSelection select_node(Sym sym) const = 0;
};
/** Select nodes uniformly */
class RandomNodeSelector : public NodeSelector {
public:
unsigned select_node(Sym sym) const;
NodeSelection select_node(Sym sym) const;
};
/** A node selector that does a specified number of rounds ignoring terminals */
@ -43,7 +59,7 @@ class BiasedNodeSelector : public NodeSelector {
BiasedNodeSelector() : nRounds(3) {} // 3: for binary trees 87.5% chance of selecting an internal node
BiasedNodeSelector(unsigned n) : nRounds(n) {}
unsigned select_node(Sym sym) const;
NodeSelection select_node(Sym sym) const;
};
#endif