19 SelFitnessProportionalOpP selFitOp_;
20 SelRandomOpP selRandomOp_;
21 SelBestOpP selBestOp_;
22 SelWorstOpP selWorstOp_;
35 selBestOp_ = (SelBestOpP) (
new SelBestOp);
45 registerParameter(state,
"replace", (voidP) (
new std::string(
"random")), ECF::STRING);
53 selFitOp_->initialize(state);
55 selFitOp_->setSelPressure(10);
58 selRandomOp_->initialize(state);
59 selBestOp_->initialize(state);
60 selWorstOp_->initialize(state);
65 std::string replace = *((std::string*) par.get());
66 replaceWorst_ =
false;
67 if(replace ==
"worst")
73 if(state->getGenotypes()[0]->getName() != flp->getName()) {
74 ECF_LOG_ERROR(state,
"Error: this algorithm accepts only a single FloatingPoint genotype!");
79 voidP sptr = state->getGenotypes()[0]->getParameterValue(state,
"dimension");
80 uint numDimension = *((uint*) sptr.get());
81 voidP lBound = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
82 double lbound = *((
double*) lBound.get());
83 voidP uBound = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
84 double ubound = *((
double*) uBound.get());
90 fp->setParameterValue(state,
"dimension", (voidP)
new uint(numDimension));
91 fp->setParameterValue(state,
"lbound", (voidP)
new double(lbound));
92 fp->setParameterValue(state,
"ubound", (voidP)
new double(ubound));
95 uint popSize = state->getPopulation()->getLocalDeme()->getSize();
105 IndividualP first = selFitOp_->select(*deme);
106 IndividualP second = selBestOp_->select(*deme);
111 child = selWorstOp_->select(*deme);
113 child = selRandomOp_->select(*deme);
116 mate(first, second, child);
128 IndividualP trial = (IndividualP) deme->at(0)->copy();
132 FloatingPointP fp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (trial->getGenotype(0));
137 fp->realValue[0] = 3.14;
142 if(trial->fitness->isBetterThan(child->fitness))
152typedef boost::shared_ptr<MyAlg> MyAlgP;
182#include <boost/property_tree/ptree.hpp>
183#include <boost/property_tree/xml_parser.hpp>
184#include <boost/foreach.hpp>
185namespace pt = boost::property_tree;
301 void setValue(
void* value)
326 func.b = first.b && second.b;
327 func.v = first.v + second.v;
339#include "examples/GPSymbReg/SymbRegEvalOp.h"
340#include "examples/GPSymbReg/zbr.h"
341int main(
int argc,
char **argv)
346 StateP state =
static_cast<StateP
> (
new State);
348 state->setEvalOp(
static_cast<EvaluateOpP
> (
new SymbRegEvalOp));
364 Tree::PrimitiveP zbr = (Tree::PrimitiveP)
new Tree::Ad;
367Tree::PrimitiveP myFunc = (Tree::PrimitiveP)
new Tree::MyFunc;
368tree->addFunction(myFunc);
372tree->addTerminal(myTerm);
375 state->addGenotype(tree);
377 if (!state->initialize(argc, argv))
388 std::vector<IndividualP> vec = state->getPopulation()->getHof()->getBest();
389 IndividualP ind = vec[0];
390 XMLNode xml2 = XMLNode::parseString(
"<Individual size=""1"" gen=""77""><FitnessMin value=""4.26326e-14""/><Tree size=""36"">+ + * * X * X X - * X X + X X + sin X * X * X X / - * X X X / / X X + X X </Tree></Individual>",
"Individual");
392 state->getAlgorithm()->evaluate(ind);
393 std::cout << ind->toString();
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
std::string name_
algorithm name
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Helper function: register a single parameter with the system.
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
FloatingPoint class - implements genotype as a vector of floating point values.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
Best individual selection operator.
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.
Worst individual selection operator.
State class - backbone of the framework.
Symbolic regression evaluation operator (using AP genotype).
void execute(void *result, Tree &tree)
Execute the primitive.
void execute(void *result, Tree &tree)
Execute the primitive.
Base primitive class (Tree genotype).
void getNextArgument(void *result, Tree &tree)
Execute next child node's primitive (execute next subtree).
void setName(std::string name)
Set primitive's name.
Tree class - implements genotype as a tree.
bool addFunction(PrimitiveP)
Add user defined function primitive. Must be called prior to initialization (no impact otherwise).