The GA doesn't work

agna@brawijaya.ac.id agna at brawijaya.ac.id
Sat Mar 1 01:30:12 EST 2003


Dear Sir/Madar, 

I am new user for GAlib.
I've tried to instantiate a genome from GA1DArrayGenome<int>.
Following this email is my own simple source.
No problem with the program, but the GA does nothing.
The final genome is the same as the initial genome.
What's wrong ?
Is there anyone ready to help me ? 

With best wishes
A Naba
 -----------------------------
#include <stdio.h>
#include <iostream.h>
#include <ga/ga.h> 

float Objective(GAGenome & c);
void IntArrayInitializer(GAGenome & c); 

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

  int i,len;
  len=6;
  //Create Target
  int target[len];
  for(i=0;i<len;i++)
    target[i] = i+1;

  // Create genome object
  GA1DArrayGenome<int> genome(len,Objective,(void *)target);

  // to ensure that the target the right one
  cout<< "Check Target : \n";
  int * initialData = (int *) genome.userData();
  for(i=0;i<genome.length();i++)
    cout << initialData[i] << " ";
  cout << endl;

  genome.initializer(IntArrayInitializer);
  genome.mutator(GA1DArrayGenome<int>::SwapMutator);
  genome.crossover(GA1DArrayGenome<int>::OnePointCrossover);

  GAParameterList params;
  GASteadyStateGA::registerDefaultParameters(params);
  params.set(gaNpopulationSize, 100);
  params.set(gaNpCrossover, 0.8);
  params.set(gaNpMutation, 0.01);	// probability of mutation
  params.set(gaNnGenerations, 100);	// number of generations
  params.set(gaNscoreFrequency, 5);	// how often to record scores
  params.set(gaNflushFrequency, 5);    // HOW often to flush scores
  params.set(gaNscoreFilename, "bog.dat");
  params.parse(argc, argv, gaFalse);

  GASteadyStateGA ga(genome);

  ga.parameters(params);

  GARankSelector selector;
  ga.selector(selector);

  ga.initialize();  // If the genome has its own initializer, do this
  genome =  ga.statistics().bestIndividual();
  // Initial Genome after initialized
  cout << "Initial Genome : \n";
  for(i=0;i<genome.length();i++)
    cout << genome[i] << " ";
  cout << endl;

  while(!ga.done()) ga.step();
  ga.flushScores();

  genome =  ga.statistics().bestIndividual();

  cout << "Best Genome : " << endl;
  for(i=0;i<genome.length();i++)
    cout << genome[i] << " ";
  cout << endl; 

  return 0;
}

float Objective(GAGenome & c){
  GA1DArrayGenome<int> & genome = (GA1DArrayGenome<int> &) c;
  int * pattern = (int *)c.userData(); 

  float value = 0.0;
  for(int i=0;i<genome.length();i++)
    value += 1.0/(1.0 + fabs(genome[i]-pattern[i])); 

  return(value);
} 

void IntArrayInitializer(GAGenome & c){
   GA1DArrayGenome<int> & genome = (GA1DArrayGenome<int> &) c;

   for(int i=0;i<genome.length();i++)
     genome[i] = 1;
}




More information about the galib mailing list