class problem...

Edmanuel E. Torres eetorres at lycos.com
Sun Jun 16 19:26:34 EDT 2002


Hi friends

The code below are a little variation of the example ex9.C found into GAlib, I'm treating to put it into the member function of the class named crga without successful, becouse any genome.phenotype() update its values. 
I can't understand why. I'm a newbie using GAlib, I will be very grateful with some help.

TIA

Edmanuel Torres

--------------------------------- crga.h -----------------------------
#include <ga/ga.h>
#include <math.h>
#include <iostream.h>

class CRga{
public:
	CRga();
	typedef float (*Evaluator)(GAGenome &);
	void CGgaTrain(Evaluator f);
	int   popsize, ngen, seed;
	float pmut, pcross;
};
----------------------------------------------------------------------
-------------------------------- crga.cpp ----------------------------
#include "CRga.h"

CRga::CRga(){
	popsize  = 10;
	ngen     = 10;
	pmut     = 0.01;
	pcross   = 0.6;
	seed     = 5;
}

void CRga::CGgaTrain(Evaluator f){
  
  GABin2DecPhenotype map;
  map.add(16, -10, 10);
  map.add(16, -10, 10);
  GABin2DecGenome genome(map, f);

  GASimpleGA ga(genome) ;
  GASigmaTruncationScaling scaling;
  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";
}
----------------------------------------------------------------------
---------------------------------main.cpp ----------------------------

#include "crga.h"

float objective(GAGenome &);
float sigmoid(float x);

int main(int argc, char **argv)
{
  CRga rga;
  cout << "This program finds the minimum value in the error function\n";
  cout << "over the interval (0,1)\n";
  cout << "\n"; cout.flush();
  rga.CGgaTrain(objective);
  return 0;
}
// This objective function tries to maximize the value of the function
//
float objective(GAGenome & c)
{
	GABin2DecGenome & genome = (GABin2DecGenome &)c;
	float y;
	y = genome.phenotype(0) + 10*sin(5*genome.phenotype(0)) \
     + 7*cos(4*genome.phenotype(0)) - 10*sin(2*genome.phenotype(1))\
     +23*cos(6*genome.phenotype(1));
	return y;
}
----------------------------------------------------------------------



_______________________________________________________
WIN a first class trip to Hawaii.  Live like the King of Rock and Roll
on the big Island. Enter Now!
http://r.lycos.com/r/sagel_mail/http://www.elvis.lycos.com/sweepstakes



More information about the galib mailing list