next up previous
Next: Genetic Operators Up: Doing GAs with Previous: Introduction

Creating chromosomes

Chromosomes are the basic building blocks of a genetic algorithm, and are used to represent a problem solution. You will not usually need to create them directly, but just in case, this is the way it would be done.

main() {
  const unsigned numBits = 12;
  chrom aChromosome( numBits );
  cout << aChromosome << endl;
which would output somethig like
(kal-el) ~/txt/tex/gags/examples> ex1
that is, an 12 bit string. Chromosomes are always bit strings, and they are not much more than that. They have no structure, and no operators to work with them, other than getLength(), operator[] and getValue. Since chromosomes can only be changed at birth time, there are no mutators, only inspectorsgif.

Actually, there are other ways of creating chromosomes: through copy constructors (from other chromosomes), or using a constructor from a bitString, which is the internal representation used for the chromosomes' bit strings. A bitString is actually a set of ordered bits, whose value can be true or false, but they can be defined using char strings.

bitString aBS = "101001111111";
chrom otherChrom( aBS );
Actually, this can be done in a single sentence, since automatic conversion is applied to the character string; but the actual way it is done is as shown.

There is not much you can do with only a chromosome, so that we will go ahead to the next section, which deals with how to operate on a chromosome.

J.J. Merelo Guervos
Fri Aug 22 12:58:28 MDT 1997