[galib] Galib fails to solve a problem using GA1DArrayAlleleGenome

bagaini.fr bagaini.fr at wanadoo.fr
Mon Apr 21 11:35:26 EDT 2008


Hi,
I'm a beginner with Galib and i try to solve a simple problem before using Galib for more complex problems.
I have a genome which each gene takes different <int> values (ArrayAlleleGenome) and I want Galib give one of the solutions that verify :
Sum(gene(i))=31 and product(gene(i))=26460.
When I launch this small program Galib give me 1 3 2 19 1 1 (with NoInitializer) or 1 1 1 1 1 1 (with UniformInitializer)!
What's wrong?
Thanks for your help. Best regards
Francois

float objective(GAGenome &);

int main(int argc, char *argv[])
{
  int i;
  GAAlleleSetArray<int> allelesp;
  allelesp.add(1,10,1);
  allelesp.add(1,7,1);
  allelesp.add(1,5,1);
  allelesp.add(1,20,1);
  allelesp.add(1,3,1);
  allelesp.add(1,3,1);
  GA1DArrayAlleleGenome<int> genomep(allelesp,objective);
  int param[6]={1,3,2,19,1,1};                                                 //initialziation of genome with determined values
  for(i=0;i<genomep.length();i++)
  genomep.gene(i,param[i]);
  genomep.initializer(GAGenome::NoInitializer);  //or GA1DArrayAlleleGenome<int>::UniformInitializer
  genomep.mutator(GA1DArrayAlleleGenome<int>::FlipMutator);
  genomep.crossover(GA1DArrayGenome<int>::OnePointCrossover);
  GASteadyStateGA ga(genomep);
  GASigmaTruncationScaling trunc;
  ga.scaling(trunc);
  ga.set(gaNpopulationSize, 40);
  ga.set(gaNpCrossover, 0.6); 
  ga.set(gaNpMutation, 0.8);
  ga.set(gaNnGenerations, 10000);
  ga.set(gaNpReplacement, 0.25);
  ga.evolve(1);
 genomep=ga.statistics().bestIndividual();                            //Print result
 for(i=0; i<genomep.length(); i++)
 {
      cout<<"  ";
      cout << genomep.gene(i);
  }
    cout << "\n";
  return 0;
}

float objective(GAGenome & c)
{
  GA1DArrayAlleleGenome<int> & genome = (GA1DArrayAlleleGenome<int> &)c;
  float value=0.0,prof;
  int sum=0,prod=1,g;
 for(int i=0;i<genome.length();i++)
 {
  g=genome.gene(i);
  sum+=genome.gene(i);
  prod*=genome.gene(i);
 }
 prof=prod/26460;
 if(prof>1)
  prof=1/prof;
 value=1/fabs(sum-31)+prof;                                //value range from 0 to 2
  return(value);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20080421/8e8e1ea4/attachment.htm


More information about the galib mailing list