private void enqueue(final GPNode n, final int argpos, final int depth) {
    if (s_node == null) {
      s_node = new GPNode[MIN_QUEUE_SIZE];
      s_argpos = new int[MIN_QUEUE_SIZE];
      s_depth = new int[MIN_QUEUE_SIZE];
      s_size = 0;
    } else if (s_size == s_node.length) // need to double them
    {
      GPNode[] new_s_node = new GPNode[s_size * 2];
      System.arraycopy(s_node, 0, new_s_node, 0, s_size);
      s_node = new_s_node;
      int[] new_s_argpos = new int[s_size * 2];
      System.arraycopy(s_argpos, 0, new_s_argpos, 0, s_size);
      s_argpos = new_s_argpos;
      int[] new_s_depth = new int[s_size * 2];
      System.arraycopy(s_depth, 0, new_s_depth, 0, s_size);
      s_depth = new_s_depth;
    }

    // okay, let's boogie!
    s_node[s_size] = n;
    s_argpos[s_size] = argpos;
    s_depth[s_size] = depth;
    s_size++;
  }
Exemplo n.º 2
0
  public void eval(
      final EvolutionState state,
      final int thread,
      final GPData input,
      final ADFStack stack,
      final GPIndividual individual,
      final Problem problem) {
    MultiplexerData md = (MultiplexerData) input;
    long[] dat_11 = null; // quiets compiler complaints
    long dat_6 = 0L;
    byte dat_3 = 0;

    // No shortcuts for now
    children[0].eval(state, thread, input, stack, individual, problem);

    if (md.status == MultiplexerData.STATUS_3) dat_3 = md.dat_3;
    else if (md.status == MultiplexerData.STATUS_6) dat_6 = md.dat_6;
    else // md.status == MultiplexerData.STATUS_11
    {
      dat_11 = md.popDat11();
      System.arraycopy(md.dat_11, 0, dat_11, 0, MultiplexerData.MULTI_11_NUM_BITSTRINGS);
    }

    children[1].eval(state, thread, input, stack, individual, problem);

    // modify

    if (md.status == MultiplexerData.STATUS_3) md.dat_3 &= dat_3;
    else if (md.status == MultiplexerData.STATUS_6) md.dat_6 &= dat_6;
    else // md.status == MultiplexerData.STATUS_11
    {
      for (int x = 0; x < MultiplexerData.MULTI_11_NUM_BITSTRINGS; x++) md.dat_11[x] &= dat_11[x];
      md.pushDat11(dat_11);
    }
  }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
0
 public void setIndex(int threadnum, int index) {
   if (indexes == null) indexes = new int[threadnum + 1];
   if (threadnum >= indexes.length) {
     int currentSize = indexes.length;
     int[] temp = new int[threadnum * 2 + 1];
     System.arraycopy(indexes, 0, temp, 0, currentSize);
     indexes = temp;
   }
   indexes[threadnum] = index;
 }
 public void setGenomeLength(int len) {
   boolean[] newGenome = new boolean[len];
   System.arraycopy(
       genome,
       0,
       newGenome,
       0,
       genome.length < newGenome.length ? genome.length : newGenome.length);
   genome = newGenome;
 }
 /**
  * Splits the genome into n pieces, according to points, which *must* be sorted. pieces.length
  * must be 1 + points.length
  */
 public void split(int[] points, Object[] pieces) {
   int point0, point1;
   point0 = 0;
   point1 = points[0];
   for (int x = 0; x < pieces.length; x++) {
     pieces[x] = new boolean[point1 - point0];
     System.arraycopy(genome, point0, pieces[x], 0, point1 - point0);
     point0 = point1;
     if (x >= pieces.length - 2) point1 = genome.length;
     else point1 = points[x + 1];
   }
 }
Exemplo n.º 7
0
 /**
  * Increases arguments to accommodate space if necessary. Sets adf to a. You need to then fill out
  * the arguments yourself.
  */
 public final void prepareADF(ADF a, GPProblem problem) {
   // set to the length requested or longer
   if (a.children.length > arguments.length) // the first time this will nearly always be true
   {
     GPData[] newarguments = new GPData[a.children.length];
     System.arraycopy(arguments, 0, newarguments, 0, arguments.length);
     // fill gap -- ugh, luckily this doesn't happen but a few times
     for (int x = arguments.length; x < newarguments.length; x++)
       newarguments[x] = (GPData) (problem.input.clone());
     arguments = newarguments;
   }
   adf = a;
 }
  /** Joins the n pieces and sets the genome to their concatenation. */
  public void join(Object[] pieces) {
    int sum = 0;
    for (int x = 0; x < pieces.length; x++) sum += ((boolean[]) (pieces[x])).length;

    int runningsum = 0;
    boolean[] newgenome = new boolean[sum];
    for (int x = 0; x < pieces.length; x++) {
      System.arraycopy(pieces[x], 0, newgenome, runningsum, ((boolean[]) (pieces[x])).length);
      runningsum += ((boolean[]) (pieces[x])).length;
    }
    // set genome
    genome = newgenome;
  }
Exemplo n.º 9
0
  /** Sets up parameters. */
  public void setup(final EvolutionState state, final Parameter base) {
    if (!(state instanceof EvolutionAgent))
      state.output.fatal("DRMStatistics requires an  EvolutionAgent", null, null);
    EvolutionAgent agent = (EvolutionAgent) state;

    super.setup(state, base);
    frequency = state.parameters.getIntWithDefault(base.push(P_FREQUENCY), null, 1);
    store_best = state.parameters.getBoolean(base.push(P_STORE_BEST), null, true);
    // use_collective = state.parameters.getBoolean(base.push(P_COLLECTIVE),null,true);

    // If we are root, set up the base filename and the logtable
    if (agent.iamroot) {
      // I'm not sure that outputting everything to the current directory is right
      basefilename =
          System.getProperty("user.dir")
              + File.separator
              + state.parameters.getFile(base.push(P_STATISTICS_FILE), null).getName();
      logtable = new Hashtable();
    } else defaultlog = -3; // Maybe can be useful for recognizing it as a non valid log

    creationtime = System.currentTimeMillis();
  }
  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;
    }
  }
Exemplo n.º 12
0
  public void eval(
      final EvolutionState state,
      final int thread,
      final GPData input,
      final ADFStack stack,
      final GPIndividual individual,
      final Problem problem) {
    MultiplexerData md = (MultiplexerData) input;

    if (md.status == MultiplexerData.STATUS_3)
      md.dat_3 = Fast.M_3[bitpos + MultiplexerData.STATUS_3];
    else if (md.status == MultiplexerData.STATUS_6)
      md.dat_6 = Fast.M_6[bitpos + MultiplexerData.STATUS_6];
    else // md.status == MultiplexerData.STATUS_11
    System.arraycopy(
          Fast.M_11[bitpos + MultiplexerData.STATUS_11],
          0,
          md.dat_11,
          0,
          MultiplexerData.MULTI_11_NUM_BITSTRINGS);
  }
  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++;
        }
      }
    }
  }
Exemplo n.º 14
0
  /**
   * This one checks that the stats message was received. If not, the message is sent again up to
   * five times. Run time an best individual of run are logged.
   */
  public void finalStatistics(final EvolutionState state, final int result) {
    super.finalStatistics(state, result);

    EvolutionAgent agent = (EvolutionAgent) state;

    StatisticsData data =
        new StatisticsData(
            new Address(agent.getName()),
            state.generation,
            System.currentTimeMillis() - creationtime,
            getBestIndividual(state),
            getBestIndividual(state));

    if (agent.iamroot) // Local logging
    printStatistics(state, data); // Every statistic will go there
    else { // DRM logging
      for (int i = 0; i < 5; i++) { // Try to send final data 5 times
        IRequest request = agent.fireMessage(agent.getRootAddress(), EvolutionAgent.M_STATS, data);
        while (request.getStatus() == IRequest.WAITING) {
          Thread.yield();
          // try{Thread.sleep(1000);}
          // catch(Exception e){state.output.error("Exception: " + e);}
        }
        if (request.getStatus() == IRequest.DONE) {
          break;
        } else {
          state.output.error("There was an error sending final statistics.");
          try {
            Thread.sleep(1000 * i ^ 2);
          } catch (Exception e) {
            state.output.error("Exception: " + e);
          }
        }
      }
    }
  }
  /**
   * Assign the set of students (presumably non-incoming-freshmen, but this is not checked) passed
   * to their dorm rooms, in a way that does not take race into account. (compare {@link
   * #assignByRace}.) As a precursor (side effect) to this, any students already existing in
   * upperclass dorms will be removed.
   */
  public void assign(Bag students) {

    emptyDorms();

    // Purge all freshmen. (We're not assigning those.)
    Bag upperclassmen = null;
    try {
      upperclassmen = (Bag) students.clone();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    for (int x = upperclassmen.size() - 1; x >= 0; x--) {
      Student s = (Student) upperclassmen.get(x);
      if (s.getGrade() < 2) {
        upperclassmen.remove(s);
      }
    }

    for (int x = 0; x < upperclassmen.size(); x++) {
      Student s = (Student) upperclassmen.get(x);
      if (s.getGrade() < 2) {
        // We're only assigning upperclassmen here.
        continue;
      }
      s.leaveRoom();
      for (int y = 0; y < dorms.size(); y++) {
        if (s.hasRoom()) {
          break;
        }
        Dorm d = (Dorm) dorms.get(y);
        if (d.isFull()) {
          continue;
        }
        if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) {
          continue;
        }
        int i = 0;
        while (i < d.getNumRooms()) {
          if (s.hasRoom()) {
            break;
          }
          try {
            d.getRoomByIndex(i).addResident(s);
          } catch (IllegalArgumentException e) { // System.out.println(e);
          }
          i++;
        }
      }
    }

    // Error testing to see if there are upperclassmen who don't have a
    //  room/dorms that aren't full
    // int q=0;
    for (int x = 0; x < upperclassmen.size(); x++) {
      Student s = (Student) upperclassmen.get(x);
      if (!s.hasRoom()) {
        System.out.println(s + " has no upperclass dorm room!");
      }
    }
  }
  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++;
        }
      }
    }
  }