[galib] integrating GA with a program that does other functions

Essam Almasri masryesam at yahoo.com
Wed Dec 29 06:30:10 EST 2004



 I have little experience with C++. I have the problem of integrating the GA in a part of a program that does GA and other things. Taking ex9 as acase study I have derived a function called getvalue and within this function. This I call this function in my program.as seen at the end this massage. In this sollution I have ignored the argc and argv and this part. The sollution seems to be OK.

for(int ii=1; ii
if(strcmp(argv[ii++],"seed") == 0) {
seed = atoi(argv[ii]);

What should I do for not ignoring them.


Thank you in advance




float objective(GAGenome &);
int getvalue();
int main()

// here are the other parts of the program


 return 0;
int getvalue()

  cout << "Example 9\n\n";
  cout << "This program finds the maximum value in the function\n";
  cout << "  y = - x1^2 - x2^2\n";
  cout << "with the constraints\n";
  cout << "     -5 <= x1 <= 5\n";
  cout << "     -5 <= x2 <= 5\n";
  cout << "\n\n"; cout.flush();

// See if we've been given a seed to use (for testing purposes).  When you
// specify a random seed, the evolution will be exactly the same each time
// you use that seed number.

  unsigned int seed = 0;

// Declare variables for the GA parameters and set them to some default values.

  int popsize  = 30;
  int ngen     = 100;
  float pmut   = 0.01;
  float pcross = 0.6;

// Create a phenotype for two variables.  The number of bits you can use to
// represent any number is limited by the type of computer you are using.  In
// this case, we use 16 bits to represent a floating point number whose value
// can range from -5 to 5, inclusive.  The bounds on x1 and x2 can be applied
// here and/or in the objective function.

  GABin2DecPhenotype map;
  map.add(16, -5, 5);
  map.add(16, -5, 5);

// Create the template genome using the phenotype map we just made.

  GABin2DecGenome genome(map, objective);

// Now create the GA using the genome and run it.  We'll use sigma truncation
// scaling so that we can handle negative objective scores.

  GASimpleGA ga(genome);
  GASigmaTruncationScaling scaling;

// Dump the results of the GA to the screen.

  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;

// This objective function tries to maximize the value of the function
//                  y = -(x1*x1 + x2*x2)
objective(GAGenome & c)
  GABin2DecGenome & genome = (GABin2DecGenome &)c;

  float y;
  y = -genome.phenotype(0) * genome.phenotype(0);
  y -= genome.phenotype(1) * genome.phenotype(1);
  return y;

Do you Yahoo!?
 The all-new My Yahoo! – What will yours do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20041229/4e7f33d0/attachment.htm

More information about the galib mailing list