/** * Prints an entire subpopulation in a form readable by humans but also parseable by the computer * using readSubpopulation(EvolutionState, LineNumberReader). */ public void printSubpopulation(final EvolutionState state, final PrintWriter writer) { writer.println(NUM_INDIVIDUALS_PREAMBLE + Code.encode(individuals.length)); for (int i = 0; i < individuals.length; i++) { writer.println(INDIVIDUAL_INDEX_PREAMBLE + Code.encode(i)); individuals[i].printIndividual(state, writer); } }
/** * Prints an entire subpopulation in a form readable by humans but also parseable by the computer * using readSubpopulation(EvolutionState, LineNumberReader) with a verbosity of * Output.V_NO_GENERAL. */ public void printSubpopulation(final EvolutionState state, final int log) { state.output.println(NUM_INDIVIDUALS_PREAMBLE + Code.encode(individuals.length), log); for (int i = 0; i < individuals.length; i++) { state.output.println(INDIVIDUAL_INDEX_PREAMBLE + Code.encode(i), log); individuals[i].printIndividual(state, log); } }
protected void parseGenotype(final EvolutionState state, final LineNumberReader reader) throws IOException { // read in the next line. The first item is the number of genes String s = reader.readLine(); DecodeReturn d = new DecodeReturn(s); Code.decode(d); if (d.type != DecodeReturn.T_INTEGER) // uh oh state.output.fatal( "Individual with genome:\n" + s + "\n... does not have an integer at the beginning indicating the genome count."); int lll = (int) (d.l); genome = new boolean[lll]; // read in the genes for (int i = 0; i < genome.length; i++) { Code.decode(d); genome[i] = (boolean) (d.l != 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); } } }
public String genotypeToString() { StringBuilder s = new StringBuilder(); s.append(Code.encode(genome.length)); for (int i = 0; i < genome.length; i++) s.append(Code.encode(genome[i])); return s.toString(); }