Пример #1
0
int
main()
{
  // create the output file
  ofile.open(OFILE);
  if(!ofile)
    {
      cerr << "Could not create output file, exiting..." 
	   << endl;
      return 1;
    }
  
  /* 
     By default GARandomSeed uses the current time multiplied by the 
     application's Process Identifier (on systems that support PIDS)
     as the seed for a pseudo-random number generator. On systems 
     which do not support PIDS, only the current time is used.
  */
  GARandomSeed(); 

  // -- initialize the GA parameters --

  // the maximum fitness of all executions
  // (and of all the valid individuals)
  max_fitness                   = 0.0f;

  // the size of a disk is 2-400 GB
  DISK_SIZE                     = GARandomInt(2, 400);

  // the number of files is 2-30 for each atom
  const unsigned int N_FILES    = GARandomInt(2, 30);

  // the number of disks is half of the number of files
  N_DISKS                       = GARandomInt(2, N_FILES/2);

  // the number of atoms (file teams) is 2-100
  const unsigned int POPULATION = GARandomInt(2, 100);

  // the possibility to use crossover (%)
  const float P_CROSS           = 0.7f;

  // the possibility to use mutation (%)
  const float P_MUT             = 0.1f;

  // the number of maximum genes
  //const unsigned int MAX_GENES  = 200;

  // threshhold for when we have converged (%)
  const float P_CONV            = 0.99f;
  // how many generations back to look
  const unsigned int N_CONV     = 50;
 
  // -- end of initialization --

  // this is not exactly the phenotype,
  // it just only shows in which disk
  // a file goes,
  // but we have to use it cause we
  // will use a real representation for
  // the genome and we want an integer
  // representation for the phenotype
  GABin2DecPhenotype phen;
  for (unsigned int i=0; i<N_FILES; i++)
    phen.add(N_FILES, 0, N_DISKS);
  
  // create the genome
  GABin2DecGenome genome(phen, Objective);

  cout << "executing the GA, please wait";

  // create the GA, set the parameters and execute it
  // this practically means call the fitness function for
  // each atom and evaluate it
  GASimpleGA ga(genome);
  ga.populationSize(POPULATION);
  //ga.nGenerations(MAX_GENES);
  ga.pMutation(P_MUT);
  ga.pCrossover(P_CROSS);
  // write the fitness of its individual in a file
  ga.scoreFilename(FFILE);
  // dump scores to disk every 20th generation
  ga.flushFrequency(20);
  // in case we want to use convergence instead of max genes
  ga.pConvergence(P_CONV);
  ga.nConvergence(N_CONV);
  ga.terminator(GAGeneticAlgorithm::TerminateUponConvergence);

  // execute the GA and initialize the genome (chromosome)
  ga.evolve();
  genome.initialize();

  // give some information about the execution
  ofile << "Size of each disk is " << DISK_SIZE << "Gb" << endl;
  ofile << "Number of disks is " << N_DISKS << endl;
  ofile << "Number of files for each individual: "
	<< N_FILES << endl;
  /*ofile << "Population: " << POPULATION
	<< " individuals" << endl;
  ofile << "Max generations: " << MAX_GENES
  << endl;*/

  // print the phenotype
  // this is in decimal format, we don't print it...
  //ofile << "The GA found:";
  //ofile << ga.statistics().bestIndividual() << " ";
  
  // print the best results 
  ofile << endl << "The amounts of filled disks are:" 
	<< endl;
  for (unsigned int i=0; i<N_DISKS; i++)
    {
      ofile << (i+1) << ": " 
	    << max_values[i] << "Gb " <<endl;
    }

  // close the output file
  ofile.close();

  // print some information to the user
  // after the execution
  cout << "the results have been written to " 
       << OFILE << endl;
  cout << "the fitness results have been written "
       << "to " << FFILE << endl;

  return 0;
}
Пример #2
0
int
main(int argc, char **argv)
{
  cout << "Example 9\n\n";
  cout << "This program finds the maximum value in the function\n";
  cout << "  y = - x1^2 - x2^2\n";
  cout << "with the constraints\n";
  cout << "     -5 <= x1 <= 5\n";
  cout << "     -5 <= x2 <= 5\n";
  cout << "\n\n"; cout.flush();

// 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 i=1; i<argc; i++) {
    if(strcmp(argv[i++],"seed") == 0) {
      seed = atoi(argv[i]);
    }
  }

// Declare variables for the GA parameters and set them to some default values.

  int popsize  = 30;
  int ngen     = 100;
  float pmut   = 0.01;
  float pcross = 0.6;

// Create a phenotype for two variables.  The number of bits you can use to
// represent any number is limited by the type of computer you are using.  In
// this case, we use 16 bits to represent a floating point number whose value
// can range from -5 to 5, inclusive.  The bounds on x1 and x2 can be applied
// here and/or in the objective function.

  GABin2DecPhenotype map;
  map.add(16, -5, 5);
  map.add(16, -5, 5);

// Create the template genome using the phenotype map we just made.

  GABin2DecGenome genome(map, objective);

// Now create the GA using the genome and run it.  We'll use sigma truncation
// scaling so that we can handle negative objective scores.

  GASimpleGA ga(genome);
  GASigmaTruncationScaling scaling;
  ga.populationSize(popsize);
  ga.nGenerations(ngen);
  ga.pMutation(pmut);
  ga.pCrossover(pcross);
  ga.scaling(scaling);
  ga.scoreFilename("bog.dat");
  ga.scoreFrequency(10);
  ga.flushFrequency(50);
  ga.evolve(seed);

// Dump the results of the GA to the screen.

  genome = ga.statistics().bestIndividual();
  cout << "the ga found an optimum at the point (";
  cout << genome.phenotype(0) << ", " << genome.phenotype(1) << ")\n\n";
  cout << "best of generation data are in '" << ga.scoreFilename() << "'\n";

  return 0;
}
Пример #3
0
int
main(int argc, char *argv[])
{
  cout << "Example 19\n\n";
  cout << "This program runs the DeJong test problems.\n\n";
  cout.flush();

// 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]);
    }
  }

  GAParameterList params;
  GASteadyStateGA::registerDefaultParameters(params);
  params.set(gaNpopulationSize, 30);	// population size
  params.set(gaNpCrossover, 0.9);	// probability of crossover
  params.set(gaNpMutation, 0.001);	// probability of mutation
  params.set(gaNnGenerations, 400);	// number of generations
  params.set(gaNpReplacement, 0.25);	// how much of pop to replace each gen
  params.set(gaNscoreFrequency, 10);	// how often to record scores
  params.set(gaNflushFrequency, 50);	// how often to dump scores to file
  params.set(gaNscoreFilename, "bog.dat");
  params.parse(argc, argv, gaFalse);    // parse command line for GAlib args

  int whichFunction = 0;

  for(int i=1; i<argc; i++){
    if(strcmp("function", argv[i]) == 0 || strcmp("f", argv[i]) == 0){
      if(++i >= argc){
        cerr << argv[0] << ": you must specify a function (1-5)\n";
        exit(1);
      }
      else{
	whichFunction = atoi(argv[i]) - 1;
	if(whichFunction < 0 || whichFunction > 4){
	  cerr << argv[0] << ": the function must be in the range [1,5]\n";
	  exit(1);
	}
        continue;
      }
    }
    else if(strcmp("seed", argv[i]) == 0){
      if(++i < argc) continue;
      continue;
    }
    else {
      cerr << argv[0] << ":  unrecognized arguement: " << argv[i] << "\n\n";
      cerr << "valid arguements include standard GAlib arguments plus:\n";
      cerr << "  f\twhich function to evaluate (all)\n";
      cerr << "parameters are:\n\n" << params << "\n\n";
      exit(1);
    }
  }

// Create the phenotype map depending on which dejong function we are going
// to be running.

  GABin2DecPhenotype map;
  switch(whichFunction){
  case 0:
    map.add(16, -5.12, 5.12);
    map.add(16, -5.12, 5.12);
    map.add(16, -5.12, 5.12);
    break;

  case 1:
    map.add(16, -2.048, 2.048);
    map.add(16, -2.048, 2.048);
    break;

  case 2:
    map.add(16, -5.12, 5.12);
    map.add(16, -5.12, 5.12);
    map.add(16, -5.12, 5.12);
    map.add(16, -5.12, 5.12);
    map.add(16, -5.12, 5.12);
    break;

  case 3:
    {
      for(int j=0; j<30; j++)
	map.add(16, -1.28, 1.28);
    }
    break;

  case 4:
    map.add(16, -65.536, 65.536);
    map.add(16, -65.536, 65.536);
    break;

  default:
    map.add(16, 0, 0);
    break;
  }

// Now create the sample genome and run the GA.

  GABin2DecGenome genome(map, objective[whichFunction]);
  //  GAStatistics stats;

  GASteadyStateGA ga(genome);
  ga.parameters(params);
  GASigmaTruncationScaling scaling;
  ga.scaling(scaling);

  cout << "running DeJong function number " << (whichFunction + 1) << " ...\n";

  ga.evolve(seed);

  cout << "the ga generated:\n" << ga.statistics().bestIndividual() << "\n";
  cout << "\nthe statistics for the run are:\n" << ga.statistics();
  cout << "\nbest-of-generation data are in 'bog.dat'\n";
  cout.flush();

  return 0;
}
Пример #4
0
int
main(int argc, char **argv)
{
    cout << "Example 10\n\n";
    cout << "This program uses sharing to do speciation.  The objective\n";
    cout << "function has more than one optimum, so different genomes\n";
    cout << "may have equally high scores.  Speciation keeps the population\n";
    cout << "from clustering at one optimum.\n";
    cout << "  Both gene-wise and phenotype-wise distance functions are used.\n";
    cout << "  Populations from all three runs are written to the files \n";
    cout << "pop.nospec.dat, pop.genespec.dat and pop.phenespec.dat.  The\n";
    cout << "function is written to the file sinusoid.dat\n\n";
    cout.flush();

// 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.

    for(int ii = 1; ii < argc; ii++)
    {
        if(strcmp(argv[ii++], "seed") == 0)
        {
            GARandomSeed((unsigned int)atoi(argv[ii]));
        }
    }

    int i;
    char filename[32] = "sinusoid.dat";
    char popfilename1[32] = "pop.nospec.dat";
    char popfilename2[32] = "pop.genespec.dat";
    char popfilename3[32] = "pop.phenespec.dat";
    ofstream outfile;

// Create a phenotype for two variables.  The number of bits you can use to
// represent any number is limited by the type of computer you are using.  In
// this case, we use 16 bits to represent a floating point number whose value
// can range from the minimum to maximum value as defined by the macros.

    GABin2DecPhenotype map;
    map.add(NBITS, MIN_VALUE, MAX_VALUE);

// Create the template genome using the phenotype map we just made.

    GABin2DecGenome genome(map, Objective);

// Now create the GA using the genome and set all of the parameters.
// You'll get different results depending on the type of GA that you use.  The
// steady-state GA tends to converge faster (depending on the type of replace-
// ment method you specify).

    GASimpleGA ga(genome);
    ga.set(gaNpopulationSize, 200);
    ga.set(gaNnGenerations, 50);
    ga.set(gaNpMutation, 0.001);
    ga.set(gaNpCrossover, 0.9);
    ga.parameters(argc, argv);


// Do the non-speciated and write to file the best-of-generation.

    cout << "running with no speciation (fitness proportionate scaling)...\n";
    cout.flush();
    GALinearScaling lin;
    ga.scaling(lin);
    ga.evolve();
    genome = ga.statistics().bestIndividual();
    cout << "the ga found an optimum at the point " << genome.phenotype(0) << endl;

    outfile.open(popfilename1, (std::ios::out | std::ios::trunc));
    if(outfile.fail())
    {
        cerr << "Cannot open " << popfilename1 << " for output.\n";
        exit(1);
    }
    for(i = 0; i < ga.population().size(); i++)
    {
        outfile << ((GABin2DecGenome&)(ga.population().individual(i))).phenotype(0);
        outfile << "\t";
        outfile << ga.population().individual(i).score() << "\n";
    }
    outfile.close();



// Now do speciation using the gene-wise distance function

    cout << "running the ga with speciation (sharing using bit-wise)...\n";
    cout.flush();
    GASharing bitSharing(BitDistance);
    ga.scaling(bitSharing);
    ga.evolve();
    genome = ga.statistics().bestIndividual();
    cout << "the ga found an optimum at the point " << genome.phenotype(0) << endl;

    outfile.open(popfilename2, (std::ios::out | std::ios::trunc));
    if(outfile.fail())
    {
        cerr << "Cannot open " << popfilename2 << " for output.\n";
        exit(1);
    }
    for(i = 0; i < ga.population().size(); i++)
    {
        outfile << ((GABin2DecGenome&)(ga.population().individual(i))).phenotype(0);
        outfile << "\t";
        outfile << ga.population().individual(i).score() << "\n";
    }
    outfile.close();



// Now do speciation using the phenotype-wise distance function

    cout << "running the ga with speciation (sharing using phenotype-wise)...\n";
    cout.flush();
    GASharing pheneSharing(PhenotypeDistance);
    ga.scaling(pheneSharing);
    ga.evolve();
    genome = ga.statistics().bestIndividual();
    cout << "the ga found an optimum at the point " << genome.phenotype(0) << endl;

    outfile.open(popfilename3, (std::ios::out | std::ios::trunc));
    if(outfile.fail())
    {
        cerr << "Cannot open " << popfilename3 << " for output.\n";
        exit(1);
    }
    for(i = 0; i < ga.population().size(); i++)
    {
        outfile << ((GABin2DecGenome&)(ga.population().individual(i))).phenotype(0);
        outfile << "\t";
        outfile << ga.population().individual(i).score() << "\n";
    }
    outfile.close();


// Now dump the function to file for comparisons

    cout << "dumping the function to file..." << endl;
    outfile.open(filename, (std::ios::out | std::ios::trunc));
    if(outfile.fail())
    {
        cerr << "Cannot open " << filename << " for output.\n";
        exit(1);
    }
    float inc = MAX_VALUE - MIN_VALUE;
    inc /= pow(2.0, NBITS);
    for(float x = MIN_VALUE; x <= MAX_VALUE; x += inc)
    {
        outfile << x << "\t" << FUNCTION(x) << "\n";
    }
    outfile << "\n";
    outfile.close();

    return 0;
}