[galib] problem with genome

TECPRO@terra.es TECPRO at terra.es
Sun Sep 25 14:34:21 EDT 2011



I am new to gelib and when trying to define a genome I get the following error:


ex1.c: In function ‘int altermain(task_info)’:

ex1.c:49: error: missing template arguments before ‘genome’

ex1.c:49: error: expected ‘;’ before ‘genome’

ex1.c:57: error: ‘genome’ was not declared in this scope


I am modifying the ex1.c example changing the GA2Dbinary to a GA1DArrayGenome and I have defined a new objetive function.

Thanks in advance.



#include <ga/GASimpleGA.h> // we're going to use the simple GA

#include <ga/GA2DBinStrGenome.h> // and the 2D binary string genome

#include <ga/std_stream.h>

#include <ga/GA1DArrayGenome.h>

#define cout STD_COUT

#include "header.h"


#include <ga/GAArray.h>

#include <ga/GAGenome.h>

#include <ga/GAAllele.h>



  int NUM_TAR = 29;

  int MAX_NUM = 15;


int altermain(task_info tasklist)


  cout << "Example 1\n\n";

  cout << "This program tries to fill a 2DBinaryStringGenome with\n";

  cout << "alternating 1s and 0s using a SimpleGA\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.



int argc = 0;

char **argv = NULL;

  for(int ii=1; ii<argc; ii++) {

    if(strcmp(argv[ii++],"seed") == 0) {

      GARandomSeed((unsigned int)atoi(argv[ii]));




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


  int width    = 10;

  int height   = 5;

  int popsize  = 30;

  int ngen     = 400;

  float pmut   = 0.001;

  float pcross = 0.9;



// Now create the GA and run it.  First we create a genome of the type that

// we want to use in the GA.  The ga doesn't operate on this genome in the

// optimization - it just uses it to clone a population of genomes.

  GA1DArrayGenome genome(NUM_TAR, Objective); 


  //GA2DBinaryStringGenome genome(width, height, Objective);


// Now that we have the genome, we create the genetic algorithm and set

// its parameters - number of generations, mutation probability, and crossover

// probability.  And finally we tell it to evolve itself.


  GASimpleGA ga(genome);







// Now we print out the best genome that the GA found.


  cout << "The GA found:\n" << ga.statistics().bestIndividual() << "\n";


// That's it!



  return 0;






// This is the objective function.  All it does is check for alternating 0s and

// 1s.  If the gene is odd and contains a 1, the fitness is incremented by 1.

// If the gene is even and contains a 0, the fitness is incremented by 1.  No

// penalties are assigned. 

//   We have to do the cast because a plain, generic GAGenome doesn't have 

// the members that a GA2DBinaryStringGenome has.  And it's ok to cast it

// because we know that we will only get GA2DBinaryStringGenomes and

// nothing else.




Objective(GAGenome&amp; g) {

  GA2DBinaryStringGenome &amp; genome = (GA2DBinaryStringGenome &amp;)g;

  float score=0.0;

  int count=0;

  for(int i=0; i<genome.width(); i++){

    for(int j=0; j<genome.height(); j++){

      if(genome.gene(i,j) == 0 &amp;&amp; count%2 == 0)

 score += 1.0;

      if(genome.gene(i,j) == 1 &amp;&amp; count%2 != 0)

 score += 1.0;




  return score;




float Objective(GAGenome&amp; g) 



GA1DArrayGenome &amp; genome = (GA1DArrayGenome &amp;)g;


float score=0;

int i,j;


int tiempo_contador[1000]; for (j=1; j<= MAX_NUM; j++){tiempo_contador[j]=0;}

 //printf(" (%d)evaluacion %d ",es_inicial,contador);




   tiempo_contador[ genome.gene(i) ]=tiempo_contador[ genome.gene(i) ]+ structarray[i].value;




   if(tiempo_contador[j]>score) score=tiempo_contador[j];




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20110925/9f911db7/attachment.htm

More information about the galib mailing list