Genome & Population Initialization
Gerald Bianchi
gerald.bianchi at vision.ee.ethz.ch
Sat Mar 29 23:25:11 EST 2003
Hi!!
I am PhD student. I am using Galib to minimize a function with binary
variables. The optimization works quite well.
However I would like to speed up the Genetic Algorithm. I would like to
use my own initialization for the population. I do not want to
initialize randomly the population.
I define my own Initializer function such:
void initGen(GAGenome & c)
{
GA1DBinaryStringGenome genome = (GA1DBinaryStringGenome &)c;
for(int i=0; i< genome.length(); i++)
cout << genome.gene(i,1);
}
Then I create a binary genome with the new Initializer
GA1DBinaryStringGenome genome(length, Objective);
genome.initializer(initGen);
genome.initialize();
This is my display function:
void displayGenome(GAGenome &g)
{
GA1DBinaryStringGenome genome = (GA1DBinaryStringGenome &)g;
for(int j=0; j<genome.length();j++)
cout << genome.gene(j);
cout << " ";
}
As you can see I would like to initialize my genome to 1.
Unfortunately, when I display the genes value of my genome,
I do not get 1 but 0. All my genome is initialized with 0.
My question is: where I am wrong ?? I did not find my error.
Does anybody know something about intialization?
Many Thanks
Gerald
Here, you can find my program:
----------------------------------
#include <stdio.h>
#include <iostream.h>
#include <ga/GASimpleGA.h>
#include <ga/GA1DBinStrGenome.h>
float Objective(GAGenome &);
void initGen(GAGenome & c);
void initPop(GAPopulation &);
void displayPopulation(GASimpleGA &ga);
void displayGenome(GAGenome &g);
int main(int argc, char **argv)
{
// Declare variables for the GA parameters and set them to some
default values.
int length = 4;
int popsize = 10;
int ngen = 10;
float pmut = 0.1;
float pcross = 0.3;
for(int ii=1; ii<argc; ii++) {
if(strcmp(argv[ii++],"seed") == 0) {
GARandomSeed((unsigned int)atoi(argv[ii]));
}
}
//Creation of my genome
GA1DBinaryStringGenome genome(length, Objective);
genome.initializer(initGen);
genome.initialize();
//Display my genome
cout << "Genome display\n";
displayGenome(genome);
cout << "\nGenome display\n";
//Creation of my population
GAPopulation pop(genome, length);
pop.size(popsize);
pop.initializer(initPop);
pop.initialize();
//Creation of my Genetic Algorithm
GASimpleGA ga(pop);
// ga.populationSize(popsize);
ga.nGenerations(ngen);
ga.pMutation(pmut);
ga.pCrossover(pcross);
ga.initialize();
//Display the individuals of my population
displayPopulation(ga);
cout << endl;
while(!ga.done())
{
ga.step();
displayPopulation(ga);
}
return 0;
}
float Objective(GAGenome& g) {
GA1DBinaryStringGenome & genome = (GA1DBinaryStringGenome &)g;
float y;
y = -1*genome.gene(0) + 10*genome.gene(1) + 5*genome.gene(2) +
6*genome.gene(3);
return y*y;
}
// In this function, I initialize the genes to 1
// and display the new value
void initGen(GAGenome & c)
{
GA1DBinaryStringGenome genome = (GA1DBinaryStringGenome &)c;
for(int i=0; i< genome.length(); i++){
cout << genome.gene(i,1);
}
cout << endl;
}
void initPop(GAPopulation & p)
{
for (int i =0; i<p.size() ; i++)
{
cout << "*"; cout.flush();
p.individual(i).initialize();
displayGenome(p.individual(i));
}
}
void displayPopulation(GASimpleGA &ga)
{
for(int ii=0; ii<ga.population().size(); ii++)
{
ga.population().individual(ii);
displayGenome(ga.population().individual(ii));
}
cout << endl;
}
void displayGenome(GAGenome &g)
{
GA1DBinaryStringGenome genome = (GA1DBinaryStringGenome &)g;
for(int j=0; j<genome.length();j++)
cout << genome.gene(j);
cout << " ";
}
Here I provide you the result:
----------------------------------
1111 //I display the intialized genome from the
//initGen function
Genome display
0000 //From the displayGenome function, I display
//the genome. As you can see, my initialisation
//did not work
Genome display //Here I initialize my population
From displayGenome From initGen
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
0000 *1111
Pop size = 10
//Display the population before the Genetic Algortihm is running
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
//Display the population when the Genetic Algortihm is running
0010 1000 0000 0000 0000 0000 0000 0000 0000 0000
0010 1000 1000 0000 0000 0000 0000 0000 0000 0000
1101 0001 1001 0010 1010 1010 0000 0000 0000 0000
1101 0100 1100 0001 1001 1001 1010 1010 1000 0000
0101 0110 1110 1011 1100 0001 1001 1010 1010 1000
0101 0110 1101 1110 1100 0010 1010 1010 1000 1000
0101 0110 1101 0110 1101 1100 0010 1010 1000 0000
1111 0101 0110 0110 1101 1101 0011 1100 1100 0010
0111 0111 1111 0101 0110 0110 1101 0110 1110 1100
0111 1111 1111 1111 1111 1101 1101 1101 1110 0100
The GA found:
0111
Nb crossover:
36
NB mutations:
42
Nb generation:
10
--
***************************************
* Bianchi Gerald *
* ETH Zurich *
* Computer Vision Laboratory (BIWI) *
* *
* Gloriastrasse 35 *
* CH-8092 Zurich *
* Switzerland *
* phone: +41 1 632 51 80 *
* email: bianchi at vision.ee.ethz.ch *
* http://www.vision.ee.ethz.ch *
***************************************
More information about the galib
mailing list