[galib] Allele sets of integers

Nate Barney barney at chgr.mc.vanderbilt.edu
Fri Dec 12 15:08:12 EST 2003


Hi,

I really appreciate your help with this problem.  I don't use IRC a lot,
but I do have an example that's fairly short.

In case it's helpful, my build environment is Red Hat Linux 9, on an
Intel machine, using gcc v3.2.2.

Here's the example:

#include <ga/GASimpleGA.h>
#include <ga/GA1DArrayGenome.h>
#include <ga/GAAllele.h>
#include <iostream>

using namespace std;

float objective(GAGenome &g)
{
    GA1DArrayGenome<unsigned char> &a =
        dynamic_cast<GA1DArrayGenome<unsigned char>&>(g);

    unsigned long count = 0;

    for (int i = 0; i < a.size(); i++)
        count += a.gene(i);

    return static_cast<float>(count);
}

void display_genome(ostream &out,const GAGenome &g)
{
    const GA1DArrayGenome<unsigned char> &a =
        dynamic_cast<const GA1DArrayGenome<unsigned char>&>(g);

    for (int i = 0; i < a.size(); i++)
    {
        if (i)
            out << ' ';

        out << static_cast<unsigned>(a.gene(i));
    }
}

int main(int argc,char *argv[])
{
    // I'd like to do something like this, but it doesn't seem to work
    //GAAlleleSet<unsigned char> allele(0,0xFF);

    // This is what does work
    GAAlleleSet<unsigned char> allele;
    for (int i = 0; i < 0xFF; ++i)
        allele.add(i);

    GA1DArrayAlleleGenome<unsigned char> genome(50,allele,objective);
    GASimpleGA ga(genome);

    ga.evolve();
    cout << "The GA found:" << endl;
    display_genome(cout,ga.statistics().bestIndividual());
    cout << endl;

    return 0;
}




More information about the galib mailing list