예제 #1
0
  /**
   * Reads a subpopulation from the format generated by printSubpopulation(....). If the number of
   * individuals is not identical, the individuals array will be deleted and replaced with a new
   * array, and a warning will be generated as individuals will have to be created using
   * newIndividual(...) rather than readIndividual(...).
   */
  public void readSubpopulation(final EvolutionState state, final LineNumberReader reader)
      throws IOException {
    // read in number of individuals and check to see if this appears to be a valid subpopulation
    int numIndividuals = Code.readIntegerWithPreamble(NUM_INDIVIDUALS_PREAMBLE, state, reader);

    // read in individuals
    if (numIndividuals != individuals.length) {
      state.output.warnOnce(
          "On reading subpopulation from text stream, the subpopulation size didn't match.\n"
              + "Had to resize and use newIndividual() instead of readIndividual().");
      individuals = new Individual[numIndividuals];
      for (int i = 0; i < individuals.length; i++) {
        int j = Code.readIntegerWithPreamble(INDIVIDUAL_INDEX_PREAMBLE, state, reader);
        // sanity check
        if (j != i)
          state.output.warnOnce(
              "On reading subpopulation from text stream, some individual indexes in the subpopulation did not match.");
        individuals[i] = species.newIndividual(state, reader);
      }
    } else
      for (int i = 0; i < individuals.length; i++) {
        int j = Code.readIntegerWithPreamble(INDIVIDUAL_INDEX_PREAMBLE, state, reader);
        // sanity check
        if (j != i)
          state.output.warnOnce(
              "On reading subpopulation from text stream, some individual indexes in the subpopulation did not match.");
        if (individuals[i] != null) individuals[i].readIndividual(state, reader);
        else {
          state.output.warnOnce(
              "On reading subpopulation from text stream, some of the preexisting subpopulation's slots were null.\n"
                  + "Had to use newIndividual() instead of readIndividual().  If you're starting an evolutionary run by reading an\n"
                  + "existing population from a file, this is expected -- ignore this message.");
          individuals[i] = species.newIndividual(state, reader);
        }
      }
  }