private Genotype<G> mutate( final Genotype<G> genotype, final double p, final IntRef alterations ) { final List<Chromosome<G>> chromosomes = new ArrayList<>(genotype.toSeq().asList()); // Add/remove Chromosome to Genotype. final Random random = RandomRegistry.getRandom(); final double rd = random.nextDouble(); if (rd < 1/3.0) { chromosomes.remove(0); } else if (rd < 2/3.0) { chromosomes.add(chromosomes.get(0).newInstance()); } alterations.value += indexes(RandomRegistry.getRandom(), chromosomes.size(), p) .map(i -> mutate(chromosomes, i, p)) .sum(); return Genotype.of(chromosomes); }
@Override protected MutatorResult<Genotype<G>> mutate( final Genotype<G> genotype, final double p, final Random random ) { final ISeq<MutatorResult<Chromosome<G>>> result = genotype.toSeq() .map(gt -> mutate(gt, p, random)); return MutatorResult.of( Genotype.of(result.map(MutatorResult::getResult)), result.stream().mapToInt(MutatorResult::getMutations).sum() ); }
private Object[] groups(final Genotype<G> genotype) { final Object[] groups = new Object[_codecs.length()]; final ISeq<Chromosome<G>> chromosomes = genotype.toSeq(); int start = 0; for (int i = 0; i < _codecs.length(); ++i) { final int end = start + _lengths[i]; final Genotype<G> gt = Genotype.of(chromosomes.subSeq(start, end)); groups[i] = _codecs.get(i).decoder().apply(gt); start = end; } return groups; }
/** * Mutates the given genotype. * * @see #mutate(Chromosome, double, Random) * @see #mutate(Gene, Random) * * @param genotype the genotype to mutate * @param p the mutation probability for the underlying genetic objects * @param random the random engine used for the genotype mutation * @return the mutation result */ protected MutatorResult<Genotype<G>> mutate( final Genotype<G> genotype, final double p, final Random random ) { final int P = probability.toInt(p); final ISeq<MutatorResult<Chromosome<G>>> result = genotype.toSeq() .map(gt -> random.nextInt() < P ? mutate(gt, p, random) : MutatorResult.of(gt)); return MutatorResult.of( Genotype.of(result.map(MutatorResult::getResult)), result.stream().mapToInt(MutatorResult::getMutations).sum() ); }
@Override protected int recombine( final MSeq<Phenotype<G, C>> population, final int[] individuals, final long generation ) { assert individuals.length == 2 : "Required order of 2"; final Random random = RandomRegistry.getRandom(); final Phenotype<G, C> pt1 = population.get(individuals[0]); final Phenotype<G, C> pt2 = population.get(individuals[1]); final Genotype<G> gt1 = pt1.getGenotype(); final Genotype<G> gt2 = pt2.getGenotype(); //Choosing the Chromosome index for crossover. final int chIndex = random.nextInt(min(gt1.length(), gt2.length())); final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy(); final MSeq<Chromosome<G>> c2 = gt2.toSeq().copy(); crossover(c1, c2, chIndex); //Creating two new Phenotypes and exchanging it with the old. population.set( individuals[0], pt1.newInstance(Genotype.of(c1.toISeq()), generation) ); population.set( individuals[1], pt2.newInstance(Genotype.of(c2.toISeq()), generation) ); return getOrder(); }
@Override protected int recombine( final MSeq<Phenotype<G, C>> population, final int[] individuals, final long generation ) { final Random random = RandomRegistry.getRandom(); final Phenotype<G, C> pt1 = population.get(individuals[0]); final Phenotype<G, C> pt2 = population.get(individuals[1]); final Genotype<G> gt1 = pt1.getGenotype(); final Genotype<G> gt2 = pt2.getGenotype(); //Choosing the Chromosome index for crossover. final int cindex = random.nextInt(min(gt1.length(), gt2.length())); final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy(); final ISeq<Chromosome<G>> c2 = gt2.toSeq(); // Calculate the mean value of the gene array. final MSeq<G> mean = mean( c1.get(cindex).toSeq().copy(), c2.get(cindex).toSeq() ); c1.set(cindex, c1.get(cindex).newInstance(mean.toISeq())); population.set( individuals[0], pt1.newInstance(Genotype.of(c1), generation) ); return 1; }
/** * Create a writer for genotypes of arbitrary chromosomes. How to write the * genotypes chromosomes is defined by the given {@link Writer}. * * @param writer the chromosome writer * @param <A> the allele type * @param <G> the gene type * @param <C> the chromosome type * @return a new genotype writer * @throws NullPointerException if the given chromosome {@code writer} is * {@code null} */ public static < A, G extends Gene<A, G>, C extends Chromosome<G> > Writer<io.jenetics.Genotype<G>> writer(final Writer<? super C> writer) { return elem( ROOT_NAME, attr(LENGTH_NAME).map(io.jenetics.Genotype<G>::length), attr(NGENES_NAME).map(io.jenetics.Genotype<G>::geneCount), elems(writer).map(gt -> cast(gt.toSeq())) ); }