[galib] 1D array with user defined type

Faisal Goussous fasfoosxxx at gmail.com
Mon Oct 30 18:35:21 EST 2006


Hello,

 I am trying to use a GA1DArrayGenome with the elements of the array being
instances of user defined class "Point".

The error appears on the line when I try to isntantiate a genome of type
GA1DArrayGenome<Point> :

GA1DArrayGenome<Point> genome1(5,objective);
(I need to create an array of 5 points).

Here is my code, and following that are the errors that I keep on getting.
If anyone has any suggestions, I would greatly appreciate it.

Regards,

Faisal Goussous
Research Assistant
The University of Iowa
IA, USA



#include <iostream>
#include <fstream>
#include <stdio.h>
#include <ga/ga.h>
#include <ga/std_stream.h>
#include <ga/GA1DArrayGenome.h>
#include <ga/GA1DArrayGenome.c>


#define INSTANTIATE_REAL_GENOME
#define ostream STD_OSTREAM

using std::ifstream;
using std::ofstream;
using std::ios;
using std::cout;
using std::cin;
using std::endl;

// function prototypes
float objective(GAGenome &);

/*************************************************************************************************/
class Point {
public:
  Point(float x, float y, float z) { _x = x; _y = y; _z = z; }
  Point(const Point & p) { _x = p._x; _y = p._y; _z = p._z; }
  Point(){} // default constructor

  int operator ==(const Point & p) const
  {
   if((p.x()==_x)&&(p.y()==_y)&&(p.z()==_z))
    return 1;
   else return 0;
  }

  int operator !=(const Point & p) const
  {
   if((p.x()==_x)&&(p.y()==_y)&&(p.z()==_z))
    return 0;
   else return 1;
  }

  Point & operator=(const Point & p) { _x=p._x;_y=p._y;_z=p._z; return
*this; }
  ~Point() {}

  float x() const { return _x; }
  float y() const { return _y; }
  float z() const { return _z; }
  float x(float val) { return _x=val; }
  float y(float val) { return _y=val; }
  float z(float val) { return _z=val; }

  friend ostream & operator<<(ostream & os, const Point & p){
    os << "(" << p._x << ", " << p._y << ", " << p._z << ")";
    return os;
  }

protected:
  float _x, _y, _z;
};

/***************************************************************************************************/
int main(int argc, char** argv)
{

// See if we've been given a seed to use (for testing purposes).  When you
// specify a random seed, the evolution will be exactly the same each time
// you use that seed number.
  unsigned int seed = 0;
  for(int ii=1; ii<argc; ii++)
  {
    if(strcmp(argv[ii++],"seed") == 0)
 {
      seed = atoi(argv[ii]);
    }
  }

// create the genome (design representation)
GA1DArrayGenome<Point> genome1(5,objective);

// Create a GA class and set its parameters and options
GASteadyStateGA ga1(genome1);

// no scaling of fitness function values
GANoScaling scale;
ga1.scaling(scale);

GAParameterList params;
GASteadyStateGA::registerDefaultParameters(params);
params.set(gaNnGenerations, 100); // number of generations
params.set(gaNpopulationSize, 110); // population size
params.set(gaNscoreFrequency, 10); // Specify how often the generational
scores should be recorded
params.set(gaNflushFrequency, 50); //specify how often the scores should be
flushed to disk (every 50 generations)
params.set(gaNpCrossover, 0.6);  // probability of crossover
params.set(gaNpMutation, 0.10);  // probability of mutation (higher than
normal, but suitable for the problem)
params.set(gaNselectScores, (int)GAStatistics::AllScores);
params.parse(argc, argv, gaFalse);

ga1.parameters(params);
ga1.set(gaNscoreFilename, "faisal's output.dat");
ga1.initialize();

// start the evolution
ga1.evolve();

cout<< ga1.population().best();

return 0 ;

}

// The Fitness Function to be MAXIMIZED
float objective(GAGenome& g)
{
  GA1DArrayGenome<Point>& genome = (GA1DArrayGenome<Point>&)g;

  Point p1 = genome.gene(0);
  Point p2 = genome.gene(1);
  Point p3 = genome.gene(2);
  Point p4 = genome.gene(3);
  Point p5 = genome.gene(4);

  float objective = p1.z()+p2.z()+p3.z()+ p4.z()+ p5.z(); // objective
function
  return objective;
}

Errors ( actually only 1 eerror):

*Compiling...*

*main.cpp*

*D:\Profile\fgoussou\Desktop\GAs\GAlib\ga\GAArray.h(34) : error C2440: 'type
cast' : cannot convert from 'int' to 'Point'*

*No constructor could take the source type, or constructor overload
resolution was ambiguous*

*D:\Profile\fgoussou\Desktop\GAs\GAlib\ga\GAArray.h(33) : while compiling
class-template member function 'GAArray<T>::GAArray(unsigned int)'*

*with*

*[*

*T=Point*

*]*

*D:\Profile\fgoussou\Desktop\GAs\GAlib\ga\GA1DArrayGenome.h(37) : see
reference to class template instantiation 'GAArray<T>' being compiled*

*with*

*[*

*T=Point*

*]*

*main.cpp(80) : see reference to class template instantiation
'GA1DArrayGenome<T>' being compiled*

*with*

*[*

*T=Point*

*]*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/galib/attachments/20061030/e7d74a31/attachment.htm


More information about the galib mailing list