示例#1
0
  public void populate(EvolutionState state, int thread) {
    // should we load individuals from a file? -- duplicates are permitted
    if (loadInds != null) {
      try {
        readSubpopulation(state, new LineNumberReader(new FileReader(loadInds)));
      } catch (IOException e) {
        state.output.fatal(
            "An IOException occurred when trying to read from the file "
                + loadInds
                + ".  The IOException was: \n"
                + e);
      }
    } else {
      Hashtable h = null;
      if (numDuplicateRetries >= 1) h = new Hashtable(individuals.length / 2); // seems reasonable

      for (int x = 0; x < individuals.length; x++) {
        for (int tries = 0; tries <= /* Yes, I see that*/ numDuplicateRetries; tries++) {
          individuals[x] = species.newIndividual(state, thread);

          if (numDuplicateRetries >= 1) {
            // check for duplicates
            Object o = h.get(individuals[x]);
            if (o == null) // found nothing, we're safe
            // hash it and go
            {
              h.put(individuals[x], individuals[x]);
              break;
            }
          }
        } // oh well, we tried to cut down the duplicates
      }
    }
  }
示例#2
0
  public void setup(final EvolutionState state, final Parameter base) {
    Parameter def = defaultBase();

    int size;

    // do we load from a file?
    loadInds = state.parameters.getFile(base.push(P_FILE), null);

    // what species do we use?

    species =
        (Species)
            state.parameters.getInstanceForParameter(
                base.push(P_SPECIES), def.push(P_SPECIES), Species.class);
    species.setup(state, base.push(P_SPECIES));

    // how big should our subpopulation be?

    size = state.parameters.getInt(base.push(P_SUBPOPSIZE), def.push(P_SUBPOPSIZE), 1);
    if (size <= 0)
      state.output.fatal(
          "Subpopulation size must be an integer >= 1.\n",
          base.push(P_SUBPOPSIZE),
          def.push(P_SUBPOPSIZE));

    // How often do we retry if we find a duplicate?
    numDuplicateRetries = state.parameters.getInt(base.push(P_RETRIES), def.push(P_RETRIES), 0);
    if (numDuplicateRetries < 0)
      state.output.fatal(
          "The number of retries for duplicates must be an integer >= 0.\n",
          base.push(P_RETRIES),
          def.push(P_RETRIES));

    individuals = new Individual[size];
  }
示例#3
0
 /**
  * Reads a subpopulation in binary form, from the format generated by writeSubpopulation(...). 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 DataInput dataInput)
     throws IOException {
   int numIndividuals = dataInput.readInt();
   if (numIndividuals != individuals.length) {
     state.output.warnOnce(
         "On reading subpopulation from binary stream, the subpopulation size was incorrect.\n"
             + "Had to resize and use newIndividual() instead of readIndividual().");
     individuals = new Individual[numIndividuals];
     for (int i = 0; i < individuals.length; i++)
       individuals[i] = species.newIndividual(state, dataInput);
   } else
     for (int i = 0; i < individuals.length; i++) individuals[i].readIndividual(state, dataInput);
 }
示例#4
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);
        }
      }
  }