Example #1
0
void main()
{
	int i, test = 0;
	// set from global variable
	int generation = m;
	Individual best;

	randomize();

	worldInit();
	populationInit(1); // 100% of population
	fitness(); // explicit, not in populationInit()

	best = getBestIndividual();

	for(i = 0; i < maxGenerations; i++){

		if(test){
			printf("#generation: %4d, fitness: %4d\n", i, best.fitness);
		}else{
			printf("#generation: %4d, fitness: %4d\n", i, best.fitness);
			printAxisOfBest(best);
		}

		if(generation == 0){
			// a)
			(K == xAxis) ? K = 1 : K++;

			// b)
			//K = random(xAxis);

			// c)
			//if(K == 10) K = 25; else K = 10;

			worldInit();
			populationInit(3); // reinicialize 30% of population
			fitness();
			generation = m;

		}else{
			generation--;
		}

		selection();
		crossover();
		mutation();
		fitness();

		best = getBestIndividual();
	}


	printf("#generation: %4d, fitness: %4d\n", i, best.fitness);
	if(!test)
		printAxisOfBest(best);
}
void sortPopulation(struct POPULATION *population) {
	struct INDIVIDUAL temp;
	int i, j, k;

    for (i = POPULATION_SIZE - 1; i >= 0; i--) {
        for (j = 0; j < i; j++) {
			k = getBestIndividual(population->population[j], population->population[j + 1]);

			if(k == 2) {
                temp = population->population[j];
                population->population[j] = population->population[j + 1];
                population->population[j + 1] = temp;
            }
        }
	}
}
Example #3
0
void CMA_ES_Population::generationUpdate()
{
  std::vector<unsigned> indices = sortedIndices();
  arx = sort(indices, arx, static_cast<unsigned>(mu));
  arz = sort(indices, arz, static_cast<unsigned>(mu));

  xmean = arx.transpose() * weights;
  VectorXf zmean = arz.transpose() * weights;

  ps = (1.f - cs) * ps + static_cast<float>(sqrt(cs * (2.f - cs) * mueff)) * (B*      zmean);
  pc = (1.f - cc) * pc + static_cast<float>(sqrt(cc * (2.f - cc) * mueff)) * (B* D* zmean);

  float hsig = ps.norm() / sqrt(1 - pow(1 - cs, 2 * counteval / lambda_)) / chiN < 1.4 + 2.f / (n + 1);

  C = (1 - c1 - cmu) * C
      + c1 * (pc * pc.transpose() + (1 - hsig) * cc * (2 - cc) * C)
      + cmu * (B* D* arz.transpose()) * weights.asDiagonal() * (B* D* arz.transpose()).transpose();
  //OUTPUT_WARNING("" << C);

  sigma *= exp((cs / damps) * (ps.norm() / chiN - 1));
  ASSERT(!std::isnan(sigma));
  if(counteval - eigenval > lambda_ / (c1 / cmu) / n / 10)
  {
    eigenval = counteval;
    //C.triangularView<Eigen::Lower>() = C.triangularView<Eigen::Upper>().transpose();
    Eigen::SelfAdjointEigenSolver<MatrixXf> es(C);
    //OUTPUT_WARNING("Success" << es.info());
    B = es.eigenvectors();
    D = sqrt(es.eigenvalues().array()).matrix().asDiagonal();
  }

  curIndividualNr = 0;
  configuration.initialization = getBestIndividual();
  //save("nn_balancer_gen_" + std::to_string(generationCounter) + "_fit_" + std::to_string(static_cast<int>(getFitness(indices[0]))) + ".evo");
  updateIndividuals();
  somethingChanged = true;
}
Example #4
0
void main()
{
	int i;
	int isUnique;
	Individual best;

	randomize();

	worldInit();

	// max 4 unique individuals, 5 and more last long to find
	while(uniques < 4){
		populationInit();
		fitness();

		for(i = 0; i < maxGenerations; i++){

			selection();
			crossover();
			mutation();
			fitness();

			best = getBestIndividual();

			if(best.fitness == maxFitness){
				isUnique = unique(best);
				if(isUnique){
					theBestOf[uniques] = best;
					uniques++;
				}
				break;
			}
		}
	}

	printAxisOfBest();
}