[galib] GA program

Somnath Bhattacharjee sn.bhattacharjee at yahoo.co.in
Fri Nov 16 13:47:32 EST 2007


I want to solve the Quadratic equation y= x*x+5*x+6. According to manual calculation the result will be two roots ( -3 and -2 , for these values of x the function value will be ZERO ). Now by GA the results (means the roots) should be nearly -3 and -2 (not exactly -3,-2).
I'm not sure that  it is right or not. I'm beginner in GA so your suggestion will be benchmark for me.
    
I want a result like -2.9986 and -1.9878 (which is nearly the roots bot not exactly -3,-2).
 
 I'm sending the total program.
 
Regards
Somnath

#include <stdio.h>
#include <ga/ga.h>
#include <ga/std_stream.h>
#define cout STD_COUT
float objective(GAGenome &);
int
main(int argc, char **argv)
{
  cout << "This program finds the roots value in the function\n";
  cout << "  y=x*x + 5*x + 6 ";
  cout << "with the constraints\n";
  cout << "     -3 <= x <= -2\n";
  cout << "\n\n"; cout.flush();
  unsigned int seed = 0;
  for(int i=1; i<argc; i++) {
    if(strcmp(argv[i++],"seed") == 0) {
      seed = atoi(argv[i]);
    }
  }
  int popsize  = 30;
  int ngen     = 100;
  float pmut   = 0.01;
  float pcross = 0.6;
  GABin2DecPhenotype map;
  map.add(16, -3, -2);
  map.add(16, -3, -2);
  
  GABin2DecGenome genome(map, objective);
  GASimpleGA ga(genome);
  GASigmaTruncationScaling scaling;
  ga.minimize();
  ga.populationSize(popsize);
  ga.nGenerations(ngen);
  ga.pMutation(pmut);
  ga.pCrossover(pcross);
  ga.scaling(scaling);
  ga.scoreFilename("bog.dat");
  ga.scoreFrequency(10);
  ga.flushFrequency(50);
  ga.evolve(seed);
  genome = ga.statistics().bestIndividual();
  cout << "the ga found an optimum at the point (";
  cout << genome.phenotype(0) << ", " << genome.phenotype(1) << ")\n\n";
  cout << "best of generation data are in '" << ga.scoreFilename() << "'\n";
  return 0;
}
 
float
objective(GAGenome & c)
{
  GABin2DecGenome & genome = (GABin2DecGenome &)c;
  float y=6.0;
  y  += genome.phenotype(0) * genome.phenotype(0);
   y += 5 * genome.phenotype(0);
    return y;
}


      Get the freedom to save as many mails as you wish. To know how, go to http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20071117/74bad972/attachment.htm


More information about the galib mailing list