Example #1
0
/*
  Returns i >= 0 where i is the index of the gene that it dislodged
  itself to.  i < 0 indicates it did not dislodge any gene.
 */
int try_dislodge(double *attempter, double *fitness, int age, int into_layer)
{
  int i, k = into_layer;
  if (k > MAX_LAYER) {
    // Sorry. No layer above to dislodge oneself to.
    return -1;
  }
  // start_search helps distribute the dislodge search evenly.
  int start_search = rand() * POP_PER_LAYER;
  for (i = 0; i < POP_PER_LAYER; i++) {
    int li = (start_search + i) % POP_PER_LAYER;
    int pi = INDIV_LINDEX(k, li);
    double *fitness_b = fitness_matrix + FITNESS_LINDEX(k, li);
    if (is_dominated(fitness, fitness_b)
        || ages[pi] > max_age[k]) {
      // New genome dominates current one, or the current one is too old.
      try_dislodge(genes[pi],
                   fitness_b,
                   ages[pi],
                   k + 1);
            
      copy(attempter, genes[pi]);
      memcpy(fitness_b, fitness, sizeof(double) * FITNESS_COUNT);
      ages[pi] = age;
      return pi;
    }
  }
  return -2;
}
Example #2
0
/* non_dominated_sorting: calculate fitness according to NSGA-II. Specially designed to consider only first Pareto front
 * parameter: solutions matrix
 * parameter: number of individuals
 * returns: array with the Pareto front
 */
int* non_dominated_sorting(float ** solutions, int number_of_individuals)
{
	/* iterators */
	int iterator_solution = 0;
	int iterator_comparision = 0;
	/* Pareto front identificator initializated to 1 */
	int solutions_allocated = 0;
	/* Pareto fronts array */
	int *pareto_fronts = (int *) malloc (number_of_individuals *sizeof (int));
	/* Pareto fronts initializated to 0 */
	for (iterator_solution=0; iterator_solution < number_of_individuals; iterator_solution++)
	{
		pareto_fronts[iterator_solution] = 0;
	}
	/* auxiliar integers */
	int dominance;
	int dont_add;
	int allocated_solutions=0;
	/* iterate on solutions */
	for (iterator_solution=0; iterator_solution < number_of_individuals; iterator_solution++)
	{
		/* flag for a solution to be added */
		dont_add = 0;
		/* compare with the actual Pareto front */
		if (pareto_fronts[iterator_solution]==0)
		{
			for (iterator_comparision=0; iterator_comparision < number_of_individuals; iterator_comparision++)
			{
				/* if the solution is not itself, it is not been evaluated or is in the actual Pareto front */
				if (iterator_solution != iterator_comparision && pareto_fronts[iterator_comparision]==0 || pareto_fronts[iterator_comparision]==actual_pareto_front)
				{
					/* verificate the dominance between both*/
					dominance = is_dominated(solutions,iterator_solution,iterator_comparision);
					/* is dominated by a solution that is in the Pareto front, so this solution is not added*/
					if (dominance == -1)
					{
						dont_add = 1;
						break;
					}
				}
			}	
			/* if the solution is not dominated by any other, let's add it to the actual Pareto front */
			if (dont_add == 0)
			{
				pareto_fronts[iterator_solution] = actual_pareto_front;
			}
		}
	}
	return pareto_fronts;
}