コード例 #1
0
  @Override
  protected Individual select(EvolutionaryAlgorithm algorithm, List<Individual> individuals) {

    double random;
    double acum;
    int n = individuals.size();
    double min = 2 - this.max;
    double range = this.max - min;
    Individual individual;
    int i;

    // Se ordena la lista de individuos según el comparador en orden inverso:
    Collections.sort(individuals, algorithm.getComparator());
    Collections.reverse(individuals);

    // Ahora la posicion en la lista de cada individuo indica su rank:
    random = EAFRandom.nextDouble();

    acum = 0.0;

    i = 0;
    do {

      i++;
      acum += (1.0 / n) * (this.max - range * ((double) (i - 1.0) / (double) (n - 1.0)));

    } while (acum < random);

    individual = individuals.get(--i);

    return (Individual) individual.clone();
  }
コード例 #2
0
  @Override
  public List<Individual> operate(EvolutionaryAlgorithm algorithm, List<Individual> individuals)
      throws OperatorException {
    MacroevolutionaryAlgorithm alg = (MacroevolutionaryAlgorithm) algorithm;
    FitnessComparator<Individual> comparator = algorithm.getComparator();
    Wxy wxy;
    MaIndividual indA, indB;
    double weight;
    int survivors = 0;

    if (comparator instanceof MaximizingFitnessComparator) {
      wxy = new WxyMaximizing();
    } else {
      if (comparator instanceof MinimizingFitnessComparator) {
        wxy = new WxyMinimizing();
      } else {
        throw new OperatorException("Wrong comparator for Inherit Extintion operator");
      }
    }
    for (int i = 0; i < individuals.size(); i++) {
      indA = (MaIndividual) individuals.get(i);
      if (indA.getBestFitness() != indA.getFitness()) {
        indA.setSurvivor(false);
      } else {
        for (int j = i + 1; j < individuals.size(); j++) {
          indB = (MaIndividual) individuals.get(j);
          if (!indA.isSurvivor() || !indB.isSurvivor()) {
            alg.setWxy(i, j, wxy.get(indA, indB));
          }
        }
        weight = 0;
        for (int j = 0; j < i; j++) {
          weight -= alg.getWxy(j, i);
        }
        for (int j = i + 1; j < individuals.size(); j++) {
          weight += alg.getWxy(i, j);
        }
        if (weight > 0) {
          indA.setSurvivor(true);
          survivors++;
        } else {
          if (weight < 0) {
            indA.setSurvivor(false);
          } else {
            if (EAFRandom.nextDouble() < 0.5) {
              indA.setSurvivor(true);
              survivors++;
            } else {
              indA.setSurvivor(false);
            }
          }
        }
      }
    }

    // We always need at least one survivor
    if (survivors == 0) {
      BestIndividual chooser = new BestIndividual();
      ((MaIndividual) chooser.get(algorithm, individuals, null)).setSurvivor(true);
    }

    return individuals;
  }