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

Looking at chromosomes


The most straightforward way of doing it is using chrom::getValue() function, which turns raw bits into unsigned longs; this obviously means that the gene size can never be bigger than the size of an unsigned in the particular machine, but don't worry, GAGS will warn you if you attempt to define huge genes. Thus, chromosomes can be fully decoded this way:

cout << "Gene values : ";       // Printing chromosome as unsigned 
for ( unsigned i =0; i < NUMGENES; i ++ ) {
  cout << aChromosome.getValue( i*SIZEGENES, SIZEGENES ) << " ";

But most usually, a chromosome will be watched as an array of floats, with fixed or variable range; each gene in the chromosome must be decoded to this range before evaluating it. views are used with this aim: decoding chromosomes to evaluate them; and they are not part of the chromosome, but separate objects, mainly so that new views can be defined, withouth delving into the chrom code.

For instance, if you want to print your aChromosome as if it were an array of floats in the range -1, 1 (that is: start -1, range 2), a view could be defined and used in this way

    view<float> vista( SIZEGENES, -1, 2 );
    for ( unsigned i = 0; i < vista.size(&aChromosome); i ++ ) {
        cout << vista( &aChromosome, i) << " ";
    cout << endl;

views are also functors, that is, they have function syntax, and several are defined by default: fixed-range array, variable-range array, and travelling salesman (which prints letters, instead of numbers). size computes the size in genes of the chromosome that is being decoded, remember that GAGS uses variable-lenght chromosomes always. They are also defined in such a way that different tipes are created depending on the parameters you give it: fixed and variable-range array are defined using the same constructor.

With views, which define a standard decoding, chromosomes can be easily evaluated and fitness computed, as explained in the next section.

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