Ejemplo n.º 1
0
  public void setup(final EvolutionState state, final Parameter base) {
    super.setup(state, base); // actually unnecessary (Individual.setup() is
    // empty)

    Parameter def = defaultBase();

    if (!(species instanceof CharVectorSpecies))
      state.output.fatal("IntegerVectorIndividual requires an IntegerVectorSpecies", base, def);
    CharVectorSpecies s = (CharVectorSpecies) species;

    genome = new char[s.genomeSize];
  }
Ejemplo n.º 2
0
  public int produce(
      int min,
      int max,
      int start,
      int subpopulation,
      Individual[] inds,
      EvolutionState state,
      int thread) {

    // grab individuals from our source and stick 'em right into inds.
    // we'll modify them from there
    int n = sources[0].produce(min, max, start, subpopulation, inds, state, thread);

    // should we bother?
    if (!state.random[thread].nextBoolean(likelihood))
      return reproduce(
          n,
          start,
          subpopulation,
          inds,
          state,
          thread,
          false); // DON'T produce children from source -- we already did

    // now let's mutate 'em
    for (int q = start; q < n + start; q++) {
      if (sources[0] instanceof SelectionMethod) inds[q] = (Individual) (inds[q].clone());

      // duplicate from the genome between a random begin and end point,
      // and put that at the end of the new genome.
      VectorIndividual ind = (VectorIndividual) (inds[q]);

      int len = ind.genomeLength();

      // zero length individual, just return
      if (len == 0) {
        return n;
      }

      int end = 0;
      int begin = state.random[thread].nextInt(len + 1);
      do {
        end = state.random[thread].nextInt(len + 1);
      } while (begin == end); // because the end is exclusive, start cannot be
      // equal to end.

      if (end < begin) {
        int temp = end; // swap if necessary
        end = begin;
        begin = temp;
      }

      // copy the original into a new array.
      Object[] original = new Object[2];
      ind.split(new int[] {0, len}, original);

      // copy the splice into a new array
      Object[] splice = new Object[3];
      ind.split(new int[] {begin, end}, splice);

      // clone the genes in splice[1] (which we'll concatenate back in) in case we're using
      // GeneVectorIndividual
      ind.cloneGenes(splice[1]);

      // appends the pieces together with the splice at the end.
      ind.join(new Object[] {original[1], splice[1]});
    }
    return n; // number of individuals produced, 1 here.
  }