[galib] galib Digest, Vol 63, Issue 5

yi.zong@risoe.dk yi.zong at risoe.dk
Tue Oct 7 09:33:19 EDT 2008

Dear all,
   I still can not solve my problem. (Initializer: first generation
always are zeros,	not a real number!)
   Could you give me any idea?
   Thank you for your help!


-----Original Message-----
From: galib-bounces at mit.edu [mailto:galib-bounces at mit.edu] On Behalf Of
galib-request at mit.edu
Sent: Saturday, October 04, 2008 7:08 PM
To: galib at mit.edu
Subject: galib Digest, Vol 63, Issue 5

Send galib mailing list submissions to
	galib at mit.edu

To subscribe or unsubscribe via the World Wide Web, visit
or, via email, send a message with subject or body 'help' to
	galib-request at mit.edu

You can reach the person managing the list at
	galib-owner at mit.edu

When replying, please edit your Subject line so it is more specific
than "Re: Contents of galib digest..."

Today's Topics:

   1. Re: Help: Initializer: first generation always are zeros,	not
      a real number! (Kupfer, Michael (ARC-AFA)[UC SANTA CRUZ])


Message: 1
Date: Fri, 3 Oct 2008 12:25:26 -0500
From: "Kupfer, Michael (ARC-AFA)[UC SANTA CRUZ]"
	<Michael.Kupfer at nasa.gov>
Subject: Re: [galib] Help: Initializer: first generation always are
	zeros,	not a real number!
To: <yi.zong at risoe.dk>, <galib at mit.edu>
	<87D67DA289F034488A9D7E4CDEFD28CC98CB08 at NDMSEVS34B.ndc.nasa.gov>
Content-Type: text/plain; charset="iso-8859-1"

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.






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



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

Phone: (650)604-6424





  mbwall 28jul94

  Copyright (c) 1995-1996  Massachusetts Institute of Technology



   Example program for the SimpleGA class and 2DBinaryStringGenome

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

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

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

2D genome.

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

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

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

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




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




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

// specify a random seed, the evolution will be exactly the same each

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


  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

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

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


  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

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

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

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

// nothing else.



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;





  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

Could you give me some other suggestion? 

 Thank you in advance,






Yi Zong
Post doc
Phone direct +45 4677 5045
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


-------------- next part --------------
An HTML attachment was scrubbed...


galib mailing list
galib at mit.edu

End of galib Digest, Vol 63, Issue 5

More information about the galib mailing list