[galib] Getting several genomes of only 0's...

David Love wvpryde at hotmail.com
Thu Jan 3 18:06:57 EST 2008




> Hello, David,
>
> There is something weird in your code. Are you sure it is compiling?
> For example, the compiler should complain about the line:
>
> GA1DArrayGenome& genome = (GA1DArrayGenome&)g;
>
> since GA1DArrayGenome is a template class. The line should instead read :
>
>
> GA1DArrayGenome<float>& genome = (GA1DArrayGenome<float>&)g;
>
>
> Meanwhile, I could'n figure out why your genomes are being populated
> with zeros.
> What about the variable "minwidth"? Where its value comes from?
>
>
> Good luck
>
>
> Anselmo

Hi Anselmo,

For some reason my code didn't copy correctly when I pasted it into the e-mail--everything between angle brackets was deleted.  I am reposting it at the bottom of this e-mail, and it looks correct right now.  Hopefully it remains correct when it is sent.

minwidth and maxwidth are globally defined as:

static float minwidth = 70;
static float maxwidth = 200 - minwidth;

Thanks,
David

int main(int argc, char **argv) {

  for(int ii=1; ii < argc; ii++) {
    if(strcmp(argv[ii++],"seed") == 0) {
      GARandomSeed((unsigned int)atoi(argv[ii]));
    }
  }

// Declare variables for the GA parameters and set them to some default values.
  
  int length	= NUM_PIECEWISE_CONST_LAYERS + 3;
  int popsize  = POPULATION_SIZE;
  int ngen     = NUMBER_GENERATIONS;
  float pmut	= PROBABILITY_MUTATION;
  float pcross = PROBABILITY_CROSSOVER;
  GA1DArrayGenome genome( length, Objective );
  genome.initializer(::Initializer);
  genome.mutator(::Mutator);
  genome.crossover(::Crossover);
  genome.comparator(::Comparator);

  GASimpleGA ga(genome);
  ga.minimize();
  ga.nBestGenomes( 5 );
  ga.nConvergence( 10 );
  ga.populationSize(popsize);
  ga.nGenerations(ngen);
  ga.pMutation(pmut);
  ga.pCrossover(pcross); 
   
  ga.selectScores( GAStatistics::AllScores );
  ga.scoreFrequency( 1 );
  ga.flushFrequency( 5 );
  
  ga.evolve(); 

// Now we print out the best genome that the GA found.

	cout << "The GA found:\n" << ga.statistics() << "\n";
  cout << "The GA found:\n" << ga.statistics().bestPopulation() << "\n";
	
  return 0;
}

void Initializer(GAGenome& g) {
	GA1DArrayGenome& genome = (GA1DArrayGenome&)g;
	int		ii;

 	static int n = 1;
	printf( "Inside Initializer, n = %d\n", n++ );	
	float LayerNumber;
	
	int Sum = MULT_LENGTH;
	int Number = -1;
	int MaxInt = getlen( NUM_PIECEWISE_CONST_LAYERS, Sum, Number );
	
	LayerNumber = (float) GARandomInt(1, MaxInt);
	genome.gene( 0, LayerNumber );
	for( ii = 1; ii <= NUM_PIECEWISE_CONST_LAYERS; ii++ ) {
		genome.gene( ii, GARandomFloat( minwidth, maxwidth ) );
	}
	
	for( ii = NUM_PIECEWISE_CONST_LAYERS + 1; ii < NUM_PIECEWISE_CONST_LAYERS + 3; ii++ ) {
		genome.gene( ii, GARandomFloat( minwidth, maxwidth ) );
	}
	
	cout << genome << endl;
}
_________________________________________________________________
Share life as it happens with the new Windows Live.
http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_122007



More information about the galib mailing list