The GA doesn't work

Allan Findlay allan at zedtwo.com
Mon Mar 3 03:52:11 EST 2003


You are initialising all your genomes to "1,1,1,1,1,1" then just swapping
them about with the SwapMutator. There is nothing happening to change these
1's into 2's, 3's etc.

-----------
    Allan

-----Original Message-----
From: agna at brawijaya.ac.id [mailto:agna at brawijaya.ac.id] 
Sent: 01 March 2003 06:30
To: galib at mit.edu
Subject: The GA doesn't work


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;
}

_______________________________________________
galib mailing list
galib at mit.edu
http://lancet.mit.edu/mailman/listinfo/galib
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20030303/cb55eb03/attachment.htm


More information about the galib mailing list