Problems with GAlib
Aureli Soria Frisch
Aureli.Soria_Frisch at ipk.fhg.de
Fri Aug 24 13:54:59 EDT 2001
Hello again,
Maybe you need the code (Thanks again):
/* ----------------------------------------------------------------------------
example_wsum.c
---------------------------------------------------------------------------- */
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <ga/GASimpleGA.h>
#include <ga/GARealGenome.h>
#include <ga/GARealGenome.C>
float Objective(GAGenome &);
int main(int argc, char **argv)
{
cout << "Example weighted sum\n\n";
cout << "Tries to classify data with weighted sum\n";
int i,j;
// iris Data
char filename[128]="iris.txt";
// Read in the pattern from the specified file. File format is pretty
simple:
// two integers that give the height then width of the matrix, then the matrix
// of 1's and 0's (with whitespace inbetween).
ifstream inStream(filename, ios :: in);
if(!inStream){
cerr << "Cannot open " << filename << " for input.\n";
exit(1);
}
int height, width;
inStream >> height >> width;
float **target = new float*[width];
for(i=0; i<width; i++)
target[i] = new float[height];
for(j=0; j<height; j++)
for(i=0; i<width; i++)
inStream >> target[i][j];
inStream.close();
// Print out the pattern to be sure we got the right one.
cout << "input pattern:\n";
for(j=0; j<height; j++){
for(i=0; i<width; i++)
cout << (target[i][j]) << " ";
cout << "\n";
}
cout << "\n"; cout.flush();
// Declare variables for the GA parameters and set them to some default values.
int popsize = 50;
int ngen = 200;
float pmut = 0.01;
float pcross = 0.6;
// 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;
for(int ii=1; ii<argc; ii++) {
if(strcmp(argv[ii++],"seed") == 0) {
seed = atoi(argv[ii]);
}
}
/*
* // Create a phenotype then fill it with the phenotypes we will need to
map to
* // the values we read from the file. The arguments to the add() method
of a
* // Bin2Dec phenotype are (1) number of bits, (2) min value, and (3) max
value.
* // The phenotype maps a floating-point number onto the number of bits that
* // you designate. Here we just make everything use 8 bits and use the
max and
* // min that were used to generate the target values. You can
experiment with
* // the number of bits and max/min values in order to make the GA work
better
* // or worse.
*
* GABin2DecPhenotype map;
* for(i=0; i<n; i++)
* map.add(8, min[i], max[i]);
*/
/*
*
* // Create the template genome using the phenotype map we just made. The
* // GA will use this genome to clone the population that it uses to do the
* // evolution. We pass the objective function to create the genome. We
* // also use the user data function in the genome to keep track of our
* // target values.
*
* GABin2DecGenome genome(map, Objective, (void *)target);
*/
GARealAlleleSet limits(0,1);
GARealGenome genome(4,limits,Objective, (void *)target);
// Now create the GA using the genome, set the parameters, and run it.
GASimpleGA ga(genome);
ga.populationSize(popsize);
ga.nGenerations(ngen);
ga.pMutation(pmut);
ga.pCrossover(pcross);
ga.scoreFilename("bog.dat");
ga.flushFrequency(20); // dump scores to disk every 50th generation
ga.evolve(seed);
// Dump the results of the GA to the screen. We print out first what a random
// genome looks like (so we get a bit of a feel for how hard it is for the
// GA to find the right values) then we print out the best genome that the
// GA was able to find.
genome.initialize();
cout << "random values in the genome:\n";;
for(i=0; i<4; i++){
cout.width(10); cout << genome.gene(i) << " ";
}
cout << "\n";
genome = ga.statistics().bestIndividual();
cout << "the ga generated:\n";
for(i=0; i<4; i++){
cout.width(10); cout << genome.gene(i) << " ";
}
cout << "\n\n"; cout.flush();
// We could print out the genome directly, like this:
// cout << genome << "\n";
cout << "best of generation data are in 'bog.dat'\n";
// Clean up by freeing the memory we allocated.
delete []target;
return 0;
}
// This objective tries to maximize each element in the genome.
float Objective(GAGenome& g)
{
GARealGenome& genome = (GARealGenome&)g;
float value=0.0,genome_value;
float **matrix=(float **)g.userData();
for(int i=0; i<150; i++)
{
genome_value=0.0;
for(int j=0;j<genome.length();j++)
{
genome_value+=matrix[i][j]*genome.gene(j);
}
if (i<50)
{
value+=fabs(1.0-genome_value);
}
else
{
value+=fabs(0.0-genome_value);
}
}
return value;
}
#################################
Aureli Soria Frisch
Fraunhofer IPK
Dept. Pattern Recognition
post: Pascalstr. 8-9, 10587 Berlin, Germany
e-mail:aureli at ipk.fhg.de
fon: +49 30 39006-150
fax: +49 30 3917517
#################################
More information about the galib
mailing list