[galib] Problem with GAAlleleArray
    Ulises Estrada 
    ulises.estrada at gmail.com
       
    Sat Feb 19 01:10:20 EST 2005
    
    
  
Hi, I'm trying to run a little program using AlleleArrays but there is
something that is not working, from what I understood if I put and
AlleleArray in a genome, the only values that it can take are those
given in the AlleleArray.
So, if I have the following GAAlleleSet:
Allele1 containing   {1, 2, 3}
Allele2                   {10, 11, 12};
Allele3                  {20, 21, 22};
and I do:
GAAlleleSetArray<int> AlelleArray;
AlelleArray.add(Allele1);
AlleleArray.add(Allele2);
AlleleArray.add(Allele3),
the genome's genes will take  {1, 2, 3, 10, 11, 12, 13, 20, 21, 22}
values, but when
I run the program it takes only {1, 2, 3} values or the first
AlleleSet given to the AlelleArray.
I show my code next, It seems ok to me, I hope someone would help me.
//------------------------------------------------------------------------------------------------------------------
float Objective2(GAGenome& g){
  GA2DArrayAlleleGenome<int> & genome = (GA2DArrayAlleleGenome<int> &)g;
  int suma = 0;
  for(int i=0; i<genome.width(); i++){
      for(int j=0; j<genome.height(); j++){
          suma += genome.gene(i,j);
      }
  }
  return suma;        
}
int main(int argc, char **argv) {
  int width    = 10;
  int height   = 5;
  int popsize  = 90;
  int ngen     = 400;
  float pmut   = 0.001;
  float pcross = 0.9;           
  
  GAAlleleSet<int> allele1;
  for(int i=10; i<=13; i++)
      allele1.add(i);
  GAAlleleSet<int> allele2;
  for(int i=20; i<=23; i++)
      allele2.add(i);
  GAAlleleSet<int> allele3;
  for(int i=0; i<=3; i++)
      allele3.add(i);
  GAAlleleSet<int> allele4;
  for(int i=30; i<=33; i++)
      allele4.add(i);
  GAAlleleSetArray<int> arreglo;
  arreglo.add(allele1);
  arreglo.add(allele2);
  arreglo.add(allele3);
  arreglo.add(allele4);
  
  GA2DArrayAlleleGenome<int> genome2(width, height, arreglo, Objective2);
  genome2.initializer(GA2DArrayAlleleGenome<int>::UniformInitializer);
  GASteadyStateGA ga(genome2);
  ga.pReplacement(0.4);
  ga.populationSize(popsize);
  ga.nGenerations(ngen);
  ga.pMutation(pmut);
  ga.pCrossover(pcross);
  ga.initialize();
  while(!ga.done()){
       if(ga.generation()%50 == 0){
                             cout<< "In generation # " <<
ga.generation() << "\n";
                             cout<< "Best evaluated: " <<
ga.population().max() << "\n";
                             cout<< "The genome:" << "\n";
                             cout<< ga.population().best() << "\n";
       }
       ga.step();
  }
  cout << "The GA found:\n" << ga.statistics().bestIndividual() << "\n";
  getch();
  return 0;
}
//-------------------------------------------------------------------------------------------------------------------
    
    
More information about the galib
mailing list