galib inside other classes
D. Brittain
David.Brittain at bristol.ac.uk
Thu Nov 6 06:06:54 EST 1997
On Tue, 4 Nov 1997, H. Brown Cribbs wrote:
> Hello GALib Users:
> I was wondering what experience others had had using the Galib
> classes within other classes. I have a fairly complex system
> I'm designing for some reinforcement learning research and I've
> reached programming dilemma.
>
> My problem entails evolving a individuals who have to be encoded
> and decoded at each generation to go out into its environment for
> evaluation. This means that I have written a "fake" evaluator
> that simple reads data from each individual's evalData member.
>
> This worked fine in my test problem that did not entail encapsulating
> the ga within another class. As soon as I did that I ran into the
> problems with setting my evaluator and initializer due to the pointer
> to a function now being required to be a pointer into a member function.
>
> The only work around I've thought of is to include a pointer to the
> objects I need to encode/evaluate and define unique default initializer
> and evaluators. I would do such a thing in a derived class of the
> GALib's GASteadyStateGA. Does this seem a "clean" solution? Or is it
> a kludge?
>
> Your opinions would be greatly appreciated as to what my options are,
> and thanks in advance,
The wayu I solve this problem (actually I think it's based on an
example in GALib) is to derive a new class that inherits from GAGenome
and from the class which is encapsulating the Genome.
You then declare initialiser, mutator, comparator, crossover, and
evaluator as static.
eg.
class OtianGenome : public GAGenome, public Otian{
...
public:
static void OGInitialiser(GAGenome& g);
static int OGMutator(GAGenome& g, float f);
static float OGComparator(const GAGenome& g1, const GAGenome& g2);
static int OGCrossover(const GAGenome&, const GAGenome&, GAGenome*,
GAGenome*);
static float Evaluator(GAGenome& g);
}
The constructor then looks like this:
OtianGenome::OtianGenome(GAGenome::Evaluator f, void* u):
GAGenome(OGInitialiser,
OGMutator,
OGComparator){
primSplitgenome=new GARealGenome(constructAlleles(),f,u);
customerAllocGenome=new SetShareGenome(customers.size());
evaluator(Evaluator);
userData(u);
crossover(OGCrossover);
}
For my problem I use the evaluator function to decode the genome (the
function call genome.buildOtian()) and then call the evaluator in the
super class:
float OtianGenome::Evaluator(GAGenome& g){
OtianGenome & genome = (OtianGenome &)g;
genome.buildOtian();
return float(genome.Otian::Obj());
}
I hope this makes sense, and that I understood your question correctly.
Dave
------------------------------------------------------------------------------
David Brittain david.brittain at bristol.ac.uk
Design Information Group tel: +44 117 928 8914
University of Bristol fax: +44 117 928 8912
83 Woodland Road
Bristol, UK.
------------------------------------------------------------------------------
More information about the galib
mailing list