# [galib] Optimization Program

Somnath Bhattacharjee sn.bhattacharjee at yahoo.co.in
Thu Nov 15 05:14:13 EST 2007

```I want to make an optimization problem to solve a quadratic equation (y=X*X+5*X+6).
I designed a program but it's showing some non desired result. I'm beginner in GA please tell me what I have to do. Is the following progam is right?

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

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";
return 0;
}

float
objective(GAGenome & c)
{
GABin2DecGenome & genome = (GABin2DecGenome &)c;
float y;
y = genome.phenotype(0) * genome.phenotype(0) + 5 * genome.phenotype(0) + 6;
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/20071115/ba9b42be/attachment.htm
```