Exemple #1
0
// ---------------------------------------------------------------------------
// Just loop through all the individuals
void EvaluateAll( GPEnvironment& environment )
{
	int n_individuals = environment.GetPopulationSize();
	for( int i = 0; i < n_individuals; ++i )
	{
		// for each dog, we'll run the test a number of times
		// where each time a stick will be thrown, and the dog
		// will need to fetch it.
		// its fitness will be graded as an average of these attempts.
		GPFitness individual_fitness = 0;
		const int kThrows = 10;
		for( int j = 0; j < kThrows; ++j )
		{
			individual_fitness += EvaluateIndividual( environment, i );
		}

		individual_fitness = individual_fitness / kThrows;

		// after putting the dog through its paces, we will manually call the fitness
		// function to see how well it did. we can then override the fitness value
		// which the framework has for it.
		// NOTE: the framework needs to have either automatically called the Fitness
		// or otherwise had it set with an OverrideIndividualFitness() call
		// because it requires the fitness value to perform crossover and mutations.
		environment.OverrideIndividualFitness( i, individual_fitness );
	}
}
Exemple #2
0
void DE(Individual Pop, int n, Individual New)
{
	int i, best;
	int p1, p2, p3;

	if(Gen==1) {
		DEBest=0;
		for(i=1; i<n; i++)
			if(Better(&Pop[i], &Pop[DEBest])) DEBest=i;
	}

	best=DEBest;
	for(i=0; i<n; i++) {
		DEselect(p1, p2, p3);
		if(DEBinomialCrossoverFlag)
			DE1bin(New[i].x, Pop[i].x,
				Pop[p1].x, Pop[p2].x, Pop[p3].x)
		else
			DE1exp(New[i].x, Pop[i].x,
				Pop[p1].x, Pop[p2].x, Pop[p3].x)
		EvaluateIndividual(&New[i]);
		if(Better(&New[i], &Pop[i])) { /* new is better than old */
			if(Better(&New[i], &Pop[DEBest])) {
				if(!SwapPopulationFlag) DEBest=i;
				else best=i;
			}
			if(!SwapPopulationFlag)
				Copy(&Pop[i], &New[i]);
			SuccessEval++;
		}
		else {
			if(SwapPopulationFlag)
				Copy(&New[i], &Pop[i]);
			FailEval++;
		}
	}
	if(SwapPopulationFlag) DEBest=best;
}