[galib] Help: Initializer: first generation always are zeros, not a real number!

Kupfer, Michael (ARC-AFA)[UC SANTA CRUZ] Michael.Kupfer at nasa.gov
Fri Oct 3 13:25:26 EDT 2008


Hello Yi!

 

I took GAlib example1 and added an own initializer.

With it (line 41) one now can set the first genes either to 0 or to 1.

Try to go from there.

 

Best,

Michael.

 

--------------------------------------------------

Michael Kupfer

 

 

University Affiliated Research Center

UC Santa Cruz

NASA Ames Research Center

Building 210, Room 255, Mail Stop 210-6

Moffett Field, CA 94035

U.S.A.

 

Email: Michael.Kupfer AT nasa.gov <mailto:Michael.Kupfer at nasa.gov> 

Phone: (650)604-6424

 

 

/* ----------------------------------------------------------------------------

  ex1.C

  mbwall 28jul94

  Copyright (c) 1995-1996  Massachusetts Institute of Technology

 

 DESCRIPTION:

   Example program for the SimpleGA class and 2DBinaryStringGenome class.

This program tries to fill the 2Dgenome with alternating 1s and 0s. 

  This example uses the default crossover (single point), default mutator

(uniform random bit flip), and default initializer (uniform random) for the

2D genome.

  Notice that one-point crossover is not necessarily the best kind of crossover

to use if you want to generate a 'good' genome with this kind of objective 

function.  But it does work.

---------------------------------------------------------------------------- */

#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>

 

#define cout STD_COUT

 

void MyInitializer(GAGenome & g);

 

float Objective(GAGenome &);  // This is the declaration of our obj function.

                        // The definition comes later in the file.

 

int k = 0;

 

 

//------------------- own initializer---------------------

void MyInitializer(GAGenome & g)

{

      //cout << "Hello" <<endl;     

      

      GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)g;

 

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

            {

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

                  {

                        genome.gene(i,j,1); // to set the genes with which the population gets initialized: either 1 or 0

                                                      // stupid example; I should have taken one where one can seed it also with non binary values

                  }

            }

}

//---------------------end of own initializer--------------------------

 

 

int

main(int argc, char **argv)

{

  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.

 

  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  = 10;

  int ngen     = 200;

  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.

 

  GA2DBinaryStringGenome genome(width, height, Objective);

  genome.initializer(MyInitializer);

 

// 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);

  ga.populationSize(popsize);

  ga.nGenerations(ngen);

  ga.pMutation(pmut);

  ga.pCrossover(pcross);

    

  ga.evolve();

 

    

// 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.

 

float

Objective(GAGenome& g) {

  GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)g;

  float score=0.0;

  int count=0;

  

  if(k < 40)            // print out the first few generations to see how the genome looks like

  {

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

      {

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

            {

                  cout << genome.gene(i,j);

            }

            cout << endl;

      }

      cout << endl;

  }

 

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

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

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

      score += 1.0;

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

      score += 1.0;

      count++;

    }

  }

  k++;

  return score;

  

}

 

 

________________________________

From: galib-bounces at mit.edu [mailto:galib-bounces at mit.edu] On Behalf Of yi.zong at risoe.dk
Sent: Friday, October 03, 2008 3:12 AM
To: galib at mit.edu
Subject: [galib] Help: Initializer: first generation always are zeros, not a real number!

 

Dear Michael, Patrik and all,

I have tried your suggestion, but it still has the problem: the first generation's individuals always are zeros, not a real number that I expect!

Could you give me some other suggestion? 

 Thank you in advance,

 

Yi

 

 

 

Yi Zong
Post doc
Phone direct +45 4677 5045
Mobile 
yi.zong at risoe.dk
 

Wind Energy Department
Risø National Laboratory for Sustainable Energy
Technical University of Denmark - DTU
Building 118, P.O. Box 49
DK-4000 Roskilde, Denmark
Tel +45 4677 4677
Fax +45 4677 5083
www.risoe.dtu.dk 












 

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


More information about the galib mailing list