scores all the same
Mesimer, Daniel James (UMKC-Student)
djm0fc at umkc.edu
Fri Jul 12 14:58:31 EDT 2002
I am running a GARealGenome GASteadyStateGA and all of my scores are the same.
I can't seem to get it to change ever, even during different runs.
the code follows: (I know it is not too clean)
BEGIN -- code -->
#include <iostream.h>
#include <assert.h>
#include <fstream.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <ga/ga.h>
#include <ga/GARealGenome.h>
#include <ga/GARealGenome.C>
#define TNUM 99
#define TNUM_dbl 99.0
ifstream filein;
ofstream fileout;
int obj;
struct spleen
{
float ct_volume;
float us_w;
float us_d;
float av_l;
};
spleen Spleen[TNUM];
float Objective(GAGenome & g);
GABoolean Terminator(GAGeneticAlgorithm & ga);
void readFile();
int main()
{
time_t now;
(void)time(&now);
GARandomSeed(now);
obj = 0;
/* BEGIN -- declaring GA objects */
GARealAlleleSet allele(0.0, 1.0); //creating an allele set so that all genomes will pull value from 0 - 1
GARealGenome genome(3, allele, Objective); //creating Genome object to put into GA
GASteadyStateGA ga(genome); //creating the Genetic Algorithm based on the previous Genome
GANoScaling scaling; //creating a different scaling scheme to use with our scores.
//GARankSelector selector; //creating selector to always pick the best individuals for mating
/* END -- declaring GA objects */
/* BEGIN -- setting genome parameters */
//genome.mutator(GARealGenome::FlipMutator);
//genome.crossover(GARealGenome::OnePointCrossover);
/* END -- setting genome parameters */
readFile();
/* BEGIN -- setting GA parameters */
//ga.terminator(Terminator);
ga.scaling(scaling);
//ga.selector(selector);
ga.populationSize(500);
ga.nGenerations(1000);
//ga.nConvergence(10); //setting the number of generations to use for convergence test
//ga.pConvergence(0.01); //setting the percentage to for convergence test
ga.pCrossover(1.0); //We wish to perform Crossovers all of the time
ga.pMutation(0.01); //Setting the Mutation probability
ga.pReplacement(0.2); //going to replace 20% of the population every generation
ga.scoreFilename("GAscores_proj1.dat");
ga.scoreFrequency(10); //recording generational scores every 10 generations
ga.flushFrequency(50); //Flushing scores to disk every 50 generations
/* END -- setting Ga parameters */
ga.evolve();
cout << "Number of calls to Objective: " << obj << endl;
cout << "GA Stats...\n\n";
cout << "GA BestIndividual: " << ga.statistics().bestIndividual() << endl;
cout << "\nGA Full Statistics:\n" << ga.statistics() << endl;
return 0;
}
float Objective(GAGenome & g)
{
int i;
float score;
float approx_value[TNUM];
float abs_err[TNUM];
float rel_err[TNUM];
float relative_error;
score = 0.0;
GARealGenome & genome = (GARealGenome &)g;
for(i = 0;i < TNUM;i++)
{
approx_value[i] = Spleen[i].us_w * genome.gene(1) * Spleen[i].us_d * genome.gene(2) * Spleen[i].av_l * genome.gene(3);
abs_err[i] = fabs(Spleen[i].ct_volume - approx_value[i]);
rel_err[i] = abs_err[i] / (fabs(Spleen[i].ct_volume));
}
relative_error = 0.0;
cout << "Objective..." << endl;
for(i = 0;i < TNUM;i++)
{
relative_error += rel_err[i];
}
score = relative_error / TNUM_dbl;
//Attempting to use relative error measurements for scoring
obj++;
return 100.0 * exp(-2.0*score);
}
GABoolean Terminator(GAGeneticAlgorithm & ga)
{
const float thresh = 10.0;
float score;
GABoolean stop;
score = ga.statistics().current();
cout << "Current Max Score: " << (float)ga.statistics().current() << endl;
if(score <= thresh)
stop = gaTrue;
else
stop = gaFalse;
return stop;
}
void readFile()
{
int i;
filein.open("Objective_Data.csv");
assert(filein);
assert(fileout);
for(i = 0;i < TNUM;i++)
{
filein >> Spleen[i].ct_volume >> Spleen[i].us_w >> Spleen[i].us_d >> Spleen[i].av_l;
}
filein.close();
}
<-- END -- cod
More information about the galib
mailing list