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 } } }
/** * 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); }
/** * 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); } } }