Ejemplo n.º 1
0
 private static double atof(String s) {
   double d = Double.valueOf(s).doubleValue();
   if (Double.isNaN(d) || Double.isInfinite(d)) {
     System.err.print("NaN or Infinity in input\n");
     System.exit(1);
   }
   return (d);
 }
  public void randomizeOrder(final EvolutionState state, final Individual[] individuals) {
    // copy the inds into a new array, then dump them randomly into the
    // subpopulation again
    Individual[] queue = new Individual[individuals.length];
    int len = queue.length;
    System.arraycopy(individuals, 0, queue, 0, len);

    for (int x = len; x > 0; x--) {
      int i = state.random[0].nextInt(x);
      individuals[x - 1] = queue[i];
      // get rid of queue[i] by swapping the highest guy there and then
      // decreasing the highest value  :-)
      queue[i] = queue[x - 1];
    }
  }
  public void evalSingleElimination(
      final EvolutionState state,
      final Individual[] individuals,
      final int subpop,
      final GroupedProblemForm prob) {
    // for a single-elimination tournament, the subpop[0] size must be 2^n for
    // some value n.  We don't check that here!  Check it in setup.

    // create the tournament array
    Individual[] tourn = new Individual[individuals.length];
    System.arraycopy(individuals, 0, tourn, 0, individuals.length);
    int len = tourn.length;
    Individual[] competition = new Individual[2];
    int[] subpops = new int[] {subpop, subpop};
    boolean[] updates = new boolean[2];
    updates[0] = updates[1] = true;

    // the "top half" of our array will be losers.
    // the bottom half will be winners.  Then we cut our array in half and repeat.
    while (len > 1) {
      for (int x = 0; x < len / 2; x++) {
        competition[0] = tourn[x];
        competition[1] = tourn[len - x - 1];

        prob.evaluate(state, competition, updates, true, subpops, 0);
      }

      for (int x = 0; x < len / 2; x++) {
        // if the second individual is better, or coin flip if equal, than we switch them around
        if (tourn[len - x - 1].fitness.betterThan(tourn[x].fitness)
            || (tourn[len - x - 1].fitness.equivalentTo(tourn[x].fitness)
                && state.random[0].nextBoolean())) {
          Individual temp = tourn[x];
          tourn[x] = tourn[len - x - 1];
          tourn[len - x - 1] = temp;
        }
      }

      // last part of the tournament: deal with odd values of len!
      if (len % 2 != 0) len = 1 + len / 2;
      else len /= 2;
    }
  }
  public void evalNRandomOneWayPopChunk(
      final EvolutionState state,
      int from,
      int numinds,
      int threadnum,
      final Individual[] individuals,
      final int subpop,
      final GroupedProblemForm prob) {
    Individual[] queue = new Individual[individuals.length];
    int len = queue.length;
    System.arraycopy(individuals, 0, queue, 0, len);

    Individual[] competition = new Individual[2];
    int subpops[] = new int[] {subpop, subpop};
    boolean[] updates = new boolean[2];
    updates[0] = true;
    updates[1] = false;
    int upperBound = from + numinds;

    for (int x = from; x < upperBound; x++) {
      competition[0] = individuals[x];
      // fill up our tournament
      for (int y = 0; y < groupSize; ) {
        // swap to end and remove
        int index = state.random[0].nextInt(len - y);
        competition[1] = queue[index];
        queue[index] = queue[len - y - 1];
        queue[len - y - 1] = competition[1];
        // if the opponent is not the actual individual, we can
        // have a competition
        if (competition[1] != individuals[x]) {
          prob.evaluate(state, competition, updates, false, subpops, 0);
          y++;
        }
      }
    }
  }
  public void evalNRandomTwoWayPopChunk(
      final EvolutionState state,
      int from,
      int numinds,
      int threadnum,
      final Individual[] individuals,
      final int subpop,
      final GroupedProblemForm prob) {

    // the number of games played for each player
    EncapsulatedIndividual[] individualsOrdered = new EncapsulatedIndividual[individuals.length];
    EncapsulatedIndividual[] queue = new EncapsulatedIndividual[individuals.length];
    for (int i = 0; i < individuals.length; i++)
      individualsOrdered[i] = new EncapsulatedIndividual(individuals[i], 0);

    Individual[] competition = new Individual[2];
    int[] subpops = new int[] {subpop, subpop};
    boolean[] updates = new boolean[2];
    updates[0] = true;
    int upperBound = from + numinds;

    for (int x = from; x < upperBound; x++) {
      System.arraycopy(individualsOrdered, 0, queue, 0, queue.length);
      competition[0] = queue[x].ind;

      // if the rest of individuals is not enough to fill
      // all games remaining for the current individual
      // (meaning that the current individual has left a
      // lot of games to play versus players with index
      // greater than his own), then it should play with
      // all. In the end, we should check that he finished
      // all the games he needs. If he did, everything is
      // ok, otherwise he should play with some other players
      // with index smaller than his own, but all these games
      // will count only for his fitness evaluation, and
      // not for the opponents' (unless allowOverEvaluations is set to true)

      // if true, it means that he has to play against all opponents with greater index
      if (individuals.length - x - 1 <= groupSize - queue[x].nOpponentsMet) {
        for (int y = x + 1; y < queue.length; y++) {
          competition[1] = queue[y].ind;
          updates[1] = (queue[y].nOpponentsMet < groupSize) || allowOverEvaluation;
          prob.evaluate(state, competition, updates, false, subpops, 0);
          queue[x].nOpponentsMet++;
          if (updates[1]) queue[y].nOpponentsMet++;
        }
      } else // here he has to play against a selection of the opponents with greater index
      {
        // we can use the queue structure because we'll just rearrange the indexes
        // but we should make sure we also rearrange the other vectors referring to the individuals

        for (int y = 0; groupSize > queue[x].nOpponentsMet; y++) {
          // swap to the end and remove from list
          int index = state.random[0].nextInt(queue.length - x - 1 - y) + x + 1;
          competition[1] = queue[index].ind;

          updates[1] = (queue[index].nOpponentsMet < groupSize) || allowOverEvaluation;
          prob.evaluate(state, competition, updates, false, subpops, 0);
          queue[x].nOpponentsMet++;
          if (updates[1]) queue[index].nOpponentsMet++;

          // swap the players (such that a player will not be considered twice)
          EncapsulatedIndividual temp = queue[index];
          queue[index] = queue[queue.length - y - 1];
          queue[queue.length - y - 1] = temp;
        }
      }

      // if true, it means that the current player needs to play some games with other players with
      // lower indexes.
      // this is an unfortunate situation, since all those players have already had their groupSize
      // games for the evaluation
      if (queue[x].nOpponentsMet < groupSize) {
        for (int y = queue[x].nOpponentsMet; y < groupSize; y++) {
          // select a random opponent with smaller index (don't even care for duplicates)
          int index;
          if (x > 0) // if x is 0, then there are no players with smaller index, therefore pick a
            // random one
            index = state.random[0].nextInt(x);
          else index = state.random[0].nextInt(queue.length - 1) + 1;
          // use the opponent for the evaluation
          competition[1] = queue[index].ind;
          updates[1] = (queue[index].nOpponentsMet < groupSize) || allowOverEvaluation;
          prob.evaluate(state, competition, updates, false, subpops, 0);
          queue[x].nOpponentsMet++;
          if (updates[1]) queue[index].nOpponentsMet++;
        }
      }
    }
  }