Beispiel #1
0
// Runs a full round of experiments
void performExperiments(struct parameters* params,
                        unsigned int nGenerations,
                        unsigned int nRepeats,
                        const std::string& nodeFunctions) {
  setCustomFitnessFunction(params, fitness, "PocMan Level 1");

  struct chromosome* bestChromosome = nullptr;
  double bestFitness = DBL_MAX;
  std::vector<double> fitnesses;
  std::vector<double> durations;
  fitnesses.reserve(nRepeats);
  durations.reserve(nRepeats);

  for (unsigned int i = 0; i < nRepeats; i++) {
    // Run CGP, record the results
    auto start = std::chrono::steady_clock::now();
    struct chromosome* chromo = runCGP(params, nullptr, nGenerations);
    auto end = std::chrono::steady_clock::now();

    double fitness = getChromosomeFitness(chromo);
    fitnesses.push_back(fitness);

    std::chrono::duration<double> diff = end - start;
    double duration = diff.count();
    durations.push_back(duration);

    // Should we keep this chromosome?
    if (fitness < bestFitness) {
      if (bestChromosome != nullptr)
        freeChromosome(bestChromosome);
      bestChromosome = chromo;
      bestFitness = fitness;
    } else {
      freeChromosome(chromo);
    }
  }

  // Stats!
  double meanFitness, stdevFitness, minFitness, maxFitness;
  std::tie(meanFitness, stdevFitness, minFitness, maxFitness) = calculateStats(fitnesses);

  double meanDuration, stdevDuration, minDuration, maxDuration;
  std::tie(meanDuration, stdevDuration, minDuration, maxDuration) = calculateStats(durations);

  // Logging!
  log("### PARAMETERS\n");
  log(parametersToString(params));
  log("generations: " + std::to_string(nGenerations) + "\n");
  log("\n--------------------\n\n");
  log("### FITNESS VALUES\n");
  for (unsigned int i = 0; i < nRepeats; i++)
    log("(" + std::to_string(i + 1) + "):\t" + std::to_string(fitnesses[i]) + "\n");
  log("\n--------------------\n\n");
  log("### STATISTICS (FITNESS)\n");
  log("mean : " + std::to_string(meanFitness) + "\n");
  log("std  : " + std::to_string(stdevFitness) + "\n");
  log("rng  : " + std::to_string(minFitness) +":" + std::to_string(maxFitness) + "\n");
  log("\n--------------------\n\n");
  log("### DURATION VALUES\n");
  for (unsigned int i = 0; i < nRepeats; i++)
    log("(" + std::to_string(i + 1) + "):\t" + std::to_string(durations[i]) + "\n");
  log("\n--------------------\n\n");
  log("### STATISTICS (DURATIONS)\n");
  log("mean : " + std::to_string(meanDuration) + "\n");
  log("std  : " + std::to_string(stdevDuration) + "\n");
  log("rng  : " + std::to_string(minDuration) + ":" + std::to_string(maxDuration) + "\n");
  log("\n--------------------\n\n");
  log("### BEST CHROMOSOME\n");
  log(chromosomeToString(bestChromosome, 0));
  log("\n--------------------\n\n");
  log("### GAMEPLAY TRACE\n");
  log(controller.generateGameTrace(bestChromosome));
}
Beispiel #2
0
int main(void){
	
	int i, gen;
	
	struct parameters *params = NULL;
	struct chromosome *population[POPULATIONSIZE];
	struct chromosome *fittestChromosome = NULL;
	struct dataSet *trainingData = NULL;
		
	double targetFitness = 0;
	int maxGens = 10000;
				
	params = initialiseParameters(NUMINPUTS, NUMNODES, NUMOUTPUTS, ARITY);
	
	addNodeFunction(params, "or,nor,and,nand");
	/*setTargetFitness(params, targetFitness);*/
	setMutationType(params, "probabilistic");
	setMutationRate(params, 0.08);
	
	trainingData = initialiseDataSetFromFile("./examples/parity3bit.data");
	
	for(i=0; i<POPULATIONSIZE; i++){
		population[i] = initialiseChromosome(params);
	}
	
	fittestChromosome = initialiseChromosome(params);
	
	/* for the number of allowed generations*/
	for(gen=0; gen<maxGens; gen++){
		
		/* set the fitnesses of the population of chromosomes*/
		for(i=0; i<POPULATIONSIZE; i++){
			setChromosomeFitness(params, population[i], trainingData);
		}
		
		/* copy over the last chromosome to fittestChromosome*/
		copyChromosome(fittestChromosome, population[POPULATIONSIZE - 1]);
		
		/* for all chromosomes except the last*/
		for(i=0; i<POPULATIONSIZE-1; i++){
			
			/* copy ith chromosome to fittestChromosome if fitter*/
			if(getChromosomeFitness(population[i]) < getChromosomeFitness(fittestChromosome)){
				copyChromosome(fittestChromosome, population[i]);
			}
		}
				
		/* termination condition*/
		if(getChromosomeFitness(fittestChromosome) <= targetFitness){
			break;
		}
				
		/* set the first member of the population to be the fittest chromosome*/
		copyChromosome(population[0], fittestChromosome);
		
		/* set remaining member of the population to be mutations of the
		 fittest chromosome*/
		for(i=1; i<POPULATIONSIZE; i++){
			
			copyChromosome(population[i], fittestChromosome);
			mutateChromosome(params, population[i]);
		}
	}
	
	printf("gen\tfitness\n");
	printf("%d\t%f\n", gen, getChromosomeFitness(fittestChromosome));
	
	
	
	for(i=0; i<POPULATIONSIZE; i++){
		freeChromosome(population[i]);
	}
	
	freeChromosome(fittestChromosome);
	freeDataSet(trainingData);
	freeParameters(params);
	
	return 0;
}