Example #1
0
void Initializer(GAGenome &g) {
  GA1DArrayGenome<double> &genome = (GA1DArrayGenome<double> &)g;

  // there are two genes
  genome.gene(0, GARandomFloat(0.0,5*M_PI));
  genome.gene(1, GARandomFloat(0.0,5*M_PI));
  genome.gene(2, GARandomFloat(0.0,5*M_PI));

  return;
}
Example #2
0
void
DoChild(GATreeGenome<Point> & tree, int depth) {
  if(depth >= MAX_DEPTH) return;
  int n = GARandomInt(0,MAX_CHILDREN);	// maximum of 5 children

  Point p(GARandomFloat(0,25),GARandomFloat(0,25),GARandomFloat(0,25));
  tree.insert(p,GATreeBASE::BELOW);

  for(int i=0; i<n; i++)
    DoChild(tree, depth+1);

  tree.parent();		// move the iterator up one level
}
// We must also specialize the allele set so that the alleles are handled
// properly.  Be sure to handle bounds correctly whether we are discretized
// or continuous.  Handle the case where someone sets stupid bounds that 
// might cause an infinite loop for exclusive bounds.
float
GAAlleleSet<float>::allele() const {
  float value = 0.0;
  if(core->type == GAAllele::ENUMERATED)
    value = core->a[GARandomInt(0, core->sz-1)];
  else if(core->type == GAAllele::DISCRETIZED){
    float n = (core->a[1] - core->a[0]) / core->a[2];
    int m = (int)n;
    if(core->lowerb == GAAllele::EXCLUSIVE) m -= 1;
    if(core->upperb == GAAllele::EXCLUSIVE) m -= 1;
    value = core->a[0] + GARandomInt(0,(int)m) * core->a[2];
    if(core->lowerb == GAAllele::EXCLUSIVE) value += core->a[2];
  }
  else{
    if(core->a[0] == core->a[1] && 
       core->lowerb == GAAllele::EXCLUSIVE && 
       core->upperb == GAAllele::EXCLUSIVE) {
      value = core->a[0];
    }
    else {
      do {
	value = GARandomFloat(core->a[0], core->a[1]);
      } while ((core->lowerb == GAAllele::EXCLUSIVE && value == core->a[0]) ||
	       (core->upperb == GAAllele::EXCLUSIVE && value == core->a[1]));
    }
  }
  return value;
}
Example #4
0
int Mutator_old(GAGenome& g, float pmut)
{
    GAListGenome<int> &child=(GAListGenome<int> &)g;
    register int n, i;
    if ((GARandomFloat() >= pmut) || (pmut <= 0)) return 0;

    n = child.size();

    if (GARandomFloat()<0.5)
        {
            child.swap(GARandomInt(0,n-1),GARandomInt(0,n-1)); // swap only one time
        }
    else
        {
            int nNodes = GARandomInt(1,((int)(n/2-1)));       // displace nNodes
            child.warp(GARandomInt(0,n-1));                   // with or without
            GAList<int> TmpList;                              // inversion
            for(i=0; i<nNodes; i++)
                {
                    int *iptr = child.remove();
                    TmpList.insert(*iptr,GAListBASE::AFTER);
                    delete iptr;
                    child.next();
                }
            int invert;
            child.warp(GARandomInt(0,n-nNodes));
            invert = (GARandomFloat()<0.5) ? 0 : 1;
            if (invert) TmpList.head();
            else TmpList.tail();

            for(i=0; i<nNodes; i++)
                {
                    int *iptr = TmpList.remove();
                    child.insert(*iptr,GAListBASE::AFTER);
                    delete iptr;
                    if (invert) TmpList.prev();
                    else TmpList.next();
                }
        }
    child.head();		// set iterator to root node

    return (1);
}
Example #5
0
int Mutator_2opt(GAGenome& g, float pmut)
{
	if ((GARandomFloat() >= pmut) || (pmut <= 0)) return 0;
	GAListGenome<int>& child=(GAListGenome<int> &)g;
    GAListIter<int> cura(child), nexta(child);
	nexta.next();
	
	for(int i=0;i<ntowns;i++, cura.next(), nexta.next())
	{
		GAListIter<int> curb(child), nextb(child);
		nextb.next();
		
		for(int j=0;j<ntowns;j++, curb.next(), nextb.next())
		{
			if(DISTANCE[*cura.current()][*nexta.current()]+DISTANCE[*curb.current()][*nextb.current()]>
				DISTANCE[*cura.current()][*nextb.current()]+DISTANCE[*curb.current()][*nexta.current()])
			{
				std::swap(*nexta.current(), *nextb.current());
			}
		}
	}
	return 1;
}
Example #6
0
int
main(int argc, char *argv[]) {
  cout << "Random Seed Test\n\n";
  cout << "This program does three runs of a genetic algorithm, with the \n";
  cout << "random seed resetting between each run.  Each of the three runs \n";
  cout << "should be identical\n\n";
  cout.flush();

  GAParameterList params;
  GASteadyStateGA::registerDefaultParameters(params);
  params.set(gaNnGenerations, 100);
  params.set(gaNflushFrequency, 5);
  params.set(gaNpMutation, 0.001);
  params.set(gaNpCrossover, 0.8);
  params.parse(argc, argv, gaFalse);

  int i,j;
  char filename[128] = "smiley.txt";
  unsigned int seed=0;

  for(i=1; i<argc; i++){
    if(strcmp("file", argv[i]) == 0 || strcmp("f", argv[i]) == 0){
      if(++i >= argc){
        cerr << argv[0] << ": the file option needs a filename.\n";
        exit(1);
      }
      else{
        sprintf(filename, argv[i]);
        continue;
      }
    }
    else if(strcmp("seed", argv[i]) == 0){
      if(++i >= argc){
        cerr << argv[0] << ": the seed option needs a filename.\n";
        exit(1);
      }
      else {
	seed = atoi(argv[i]);
	continue;
      }
    }
    else {
      cerr << argv[0] << ":  unrecognized arguement: " << argv[i] << "\n\n";
      cerr << "valid arguments include standard GAlib arguments plus:\n";
      cerr << "  f\tfilename from which to read (" << filename << ")\n";
      cerr << "\n";
      exit(1);
    }
  }

  const int n=5;

  cout << n << " random numbers\n";
  GAResetRNG(seed);
  for(i=0; i<n; i++)
    cout << " " << GARandomFloat();
  cout << "\n";

  cout << n << " random numbers\n";
  GAResetRNG(seed);
  for(i=0; i<n; i++)
    cout << " " << GARandomFloat();
  cout << "\n";

  cout << n << " random numbers\n";
  GAResetRNG(seed);
  for(i=0; i<n; i++)
    cout << " " << GARandomFloat();
  cout << "\n";
  cout.flush();

  ifstream inStream(filename);
  if(!inStream){
    cerr << "Cannot open " << filename << " for input.\n";
    exit(1);
  }

  int height, width;
  inStream >> height >> width;

  short **target = new short*[width];
  for(i=0; i<width; i++)
    target[i] = new short[height];

  for(j=0; j<height; j++)
    for(i=0; i<width; i++)
      inStream >> target[i][j];

  inStream.close();

  GA2DBinaryStringGenome genome(width, height, objective, (void *)target);
  GASimpleGA ga(genome);
  ga.parameters(params);

  // first run

  GAResetRNG(seed);
  genome.initialize();
  cout << genome << "\n";
  ga.set(gaNscoreFilename, "bog1.dat");
  ga.evolve();

  genome = ga.statistics().bestIndividual();
  cout << "run 1:  the random seed is: " << GAGetRandomSeed() << "\n";
  for(j=0; j<height; j++){
    for(i=0; i<width; i++)
      cout << (genome.gene(i,j) == 1 ? '*' : ' ') << " ";
    cout << "\n";
  }
  cout << "\n"; cout.flush();

  // second run

  GAResetRNG(seed);
  genome.initialize();
  cout << genome << "\n";
  ga.set(gaNscoreFilename, "bog2.dat");
  ga.evolve();

  genome = ga.statistics().bestIndividual();
  cout << "run 2:  the random seed is: " << GAGetRandomSeed() << "\n";
  for(j=0; j<height; j++){
    for(i=0; i<width; i++)
      cout << (genome.gene(i,j) == 1 ? '*' : ' ') << " ";
    cout << "\n";
  }
  cout << "\n"; cout.flush();

  // third run

  GAResetRNG(seed);
  genome.initialize();
  cout << genome << "\n";
  ga.set(gaNscoreFilename, "bog3.dat");
  ga.evolve();

  genome = ga.statistics().bestIndividual();
  cout << "run 3:  the random seed is: " << GAGetRandomSeed() << "\n";
  for(j=0; j<height; j++){
    for(i=0; i<width; i++)
      cout << (genome.gene(i,j) == 1 ? '*' : ' ') << " ";
    cout << "\n";
  }
  cout << "\n"; cout.flush();

  for(i=0; i<width; i++)
    delete target[i];
  delete [] target;

  return 0;
}