[galib] Possible GAListGenome Problem?

hneil@fastwebnet.it hneil at fastwebnet.it
Sun Feb 19 00:37:59 EST 2006


I tried to strip out as much irrelevant code as possible.  PointGenome is
the typedef of GAListGenome.  I'm trying to make a traveling salesman genetic
algorithm.  It seems to initialize fine.  Notice the !pg.head() check in
generatePointGenome.  

Later in TspObjective (evaluator), the very same check is done and fails.
 The segmentation fault comes from trying to access the members of the array.
 The pointers come up null.  However, the fact that 0 < pg.size() - 1 allows
access to the for loop is proof that something was added perhaps.

To my knowledge, nothing happens in the main that would change the point
genome since it is all in its own class called TspData.  

Any assistance you could provide would be most helpful.

Thanks in advance,

Neil

/***************Code****************/

void TspData::TspInitializer(GAGenome &c)
{
        PointGenome &pg = (PointGenome&)c;
        generatePointGenome(pg);
}

void TspData::generatePointGenome(PointGenome &pg)
{
        std::cout << "generatePointGenome(PointGenome &pg)\n";
        std::cout.flush();
        if(pointlist == NULL)
        {
                std::cout << "Serious error:  pointlist is NULL\n";
                return;
        }

        while(pg.head()) pg.destroy();

        pg.evaluator(TspData::TspObjective);
        pg.initializer(TspData::TspInitializer);
        pg.crossover(PointGenome::OrderCrossover);
        pg.userData(pointlist);

        // Initialize
        for(unsigned int i=0; i < (unsigned int)pointlist->size; i++)
        {
                pg.insert(i, GAListBASE::TAIL);
        }

        if(!pg.head())
        {
                std::cout << "!pg.head() in generatePointGenome!\n";
                std::cout.flush();
        }

        // Randomize
        int size = pg.size();
        for(int i=0; i < size; i++)
        {
                pg.swap(i, GARandomInt(0, size - 1));
        }
}

float TspData::TspObjective(GAGenome &c)
{
        std::cout << "TspObjective\n";
        std::cout.flush();
        PointGenome &pg = (PointGenome&)c;

        PointList_t* pl = (PointList_t*)pg.userData();

        if(pl == NULL)
        {
                return 0.0;
        }

        if(!pg.head())
        {
                std::cout << "WTF!\n";
                std::cout.flush();
        }

        float sum = 0.0;
        for(unsigned int i=0; i < (unsigned int)pg.size() - 1; i++)
        {
                sum += getDistance(*pl->points[*pg[i]], *pl->points[*pg[i+1]]);
        }
        return sum;
}

Output is this:
generatePointGenome(PointGenome &pg)
TspObjective
WTF!
Segmentation fault

------------------
Neil S. Harmon
hneil at clemson.edu
(864)-858-4474






More information about the galib mailing list