[galib] How to pass arguments to the Objective function

matthew wall mbwall at MIT.EDU
Mon Feb 28 15:14:13 EST 2005


On 28 Feb 2005, at 13:36, mathew webber wrote:

> Hello all,
>
> I am a new user to the library and I am very impressed with it so far. 
> I am using it for my Honours project and I am having problems passing 
> an argument to my objective function.
>
> I need to be able to pass a pointer to a 2 dimensional array, which is 
> used to assess all the genomes. The array is read in from file and is 
> the same for every Genome throughout, so it would be very inefficient 
> to read it in every time the objective function was called:(
>
> So I want my objective to read:
>
>                Objective(GAGenome &, int*);
>
> But using the following will obviously not work:
>
>  	int* pointer;
>
>  	GA1DArrayGenome<int> genome(10, Objective(pointer));
>
> Is there a way for me to do this?
>
> Any help is gratefully received on this one!
>
> Thank you in advance,
>
> Mat

there are (at least) two approaches.  the first approach is to use an 
existing genome class and the userData object bound to each genome.  
the second approach is to create your own genome class.

the first approach goes something like this:

class TwoDData : public UserData {
   ...
};

float ObjFunc(GAGenome& g) {
   TwoDData* data = DYN_CAST(TwoDData*, g.userData());
   assert(data);
   ...
}

main() {
   TwoDData data;
   data.readFromDisk();
   GA1DArrayGenome<int> genome(10, ObjFunc);
   genome.userData(&data);
   GASimpleGA ga(genome);
   ga.evolve();
}



the second approach goes something like this:

class TwoDDataGenome : public GA1DArrayGenome<int> {
   static int* arraydata;
   static void ReadData();
   ...
};

float ObjFunc(GAGenome& g) {
   TwoDDataGenome& genome = DYN_CAST(TwoDDataGenome&, g);
   ...
}

main() {
   TwoDDataGenome::ReadData();
   TwoDDataGenome genome(10, ObjFunc);
   GASimpleGA ga(genome);
   ga.evolve();
}


hope that helps...

m



More information about the galib mailing list