/** * Return a scalar {@code Codec} for the given range. * * @param domain the domain of the returned {@code Codec} * @return a new scalar {@code Codec} with the given domain. * @throws NullPointerException if the given {@code domain} is {@code null} */ public static Codec<Long, LongGene> ofScalar(final LongRange domain) { requireNonNull(domain); return Codec.of( Genotype.of(LongChromosome.of(domain)), gt -> gt.getGene().getAllele() ); }
@Override public Codec<ISeq<BitGene>, BitGene> codec() { return Codec.of( Genotype.of(BitChromosome.of(_length, _onesProbability)), gt -> gt.getChromosome().toSeq() ); }
/** * Create a vector {@code Codec} for the given ranges. Each vector element * might have a different domain. The vector length is equal to the number * of domains. * * @param domains the domain ranges * @return a new vector {@code Codec} * @throws NullPointerException if one of the arguments is {@code null} * @throws IllegalArgumentException if the {@code domains} array is empty */ public static Codec<long[], LongGene> ofVector(final LongRange... domains) { if (domains.length == 0) { throw new IllegalArgumentException("Domains must not be empty."); } final ISeq<LongChromosome> chromosomes = Stream.of(domains) .peek(Objects::requireNonNull) .map(LongGene::of) .map(LongChromosome::of) .collect(ISeq.toISeq()); return Codec.of( Genotype.of(chromosomes), gt -> { final long[] args = new long[gt.length()]; for (int i = gt.length(); --i >= 0;) { args[i] = gt.getChromosome(i).getGene().longValue(); } return args; } ); }
@Test(dataProvider = "intVectorData") public void ofIntVector(final IntRange domain, final int length) { final Codec<int[], IntegerGene> codec = Codecs.ofVector(domain, length); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), length); for (IntegerGene gene : gt.getChromosome()) { Assert.assertEquals(gene.getMin().intValue(), domain.getMin()); Assert.assertEquals(gene.getMax().intValue(), domain.getMax()); } final Function<Genotype<IntegerGene>, int[]> f = codec.decoder(); final int[] value = f.apply(gt); Assert.assertEquals(value.length, length); for (int i = 0; i < length; ++i) { Assert.assertEquals(gt.get(0, i).intValue(), value[i]); } }
@Test(dataProvider = "intScalarData") public void ofIntScalar(final IntRange domain) { final Codec<Integer, IntegerGene> codec = Codecs.ofScalar(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().intValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().intValue(), domain.getMax()); final Function<Genotype<IntegerGene>, Integer> f = codec.decoder(); Assert.assertEquals(f.apply(gt).intValue(), gt.getGene().intValue()); }
@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 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 static double f(final Genotype<DoubleGene> gt) { return IntStream.range(0, 50000) .mapToDouble(Math::sinh) .sum()*gt.getGene().getAllele(); }
@Test public void newInstance() { final Genotype<DoubleGene> gt1 = Genotype.of( //Rotation DoubleChromosome.of(DoubleGene.of(-Math.PI, Math.PI)), //Translation DoubleChromosome.of(DoubleGene.of(-300, 300), DoubleGene.of(-300, 300)), //Shear DoubleChromosome.of(DoubleGene.of(-0.5, 0.5), DoubleGene.of(-0.5, 0.5)) ); final Genotype<DoubleGene> gt2 = gt1.newInstance(); Assert.assertEquals(gt1.length(), gt2.length()); for (int i = 0; i < gt1.length(); ++i) { Chromosome<DoubleGene> ch1 = gt1.getChromosome(i); Chromosome<DoubleGene> ch2 = gt2.getChromosome(i); Assert.assertEquals(ch1.length(), ch2.length()); } }
public static void main(final String[] args) { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(/*Dummy fitness function*/gt -> Double.NaN, gtf) .evaluator(new RxEvaluator<>(RxEvaluator::fitness)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(100) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result.getBestPhenotype()); }
/** * Return the chromosome at the given index. It is guaranteed, that the * returned chromosome is not null. * * @see #getChromosome(int) * @since 4.0 * * @param chromosomeIndex Chromosome index. * @return The Chromosome. * @throws IndexOutOfBoundsException if * {@code (index < 0 || index >= _length)}. */ public Chromosome<G> get(final int chromosomeIndex) { return getChromosome(chromosomeIndex); }
final List<C> chromosomes = (List<C>)v[2]; final io.jenetics.Genotype<G> genotype = io.jenetics.Genotype.of(chromosomes); if (length != genotype.length()) { throw new IllegalArgumentException(format( "Expected %d chromosome, but read %d.", length, genotype.length() )); if (ngenes != genotype.geneCount()) { throw new IllegalArgumentException(format( "Expected %d genes, but read %d.", ngenes, genotype.geneCount() ));
static <G extends NumericGene<?, G>> ISeq<DoubleMoments> statistics(final Seq<? extends Phenotype<G, ?>> population) { final Map<Long, DoubleMomentStatistics> statistics = new HashMap<>(); for (Phenotype<G, ?> pt : population) { final Genotype<G> gt = pt.getGenotype(); for (int i = 0; i < gt.length(); ++i) { final Chromosome<G> ch = gt.getChromosome(i); for (int j = 0; j < ch.length(); ++j) { statistics .computeIfAbsent(((long)i << 32) | (j & 0xffffffffL), k -> new DoubleMomentStatistics()) .accept(ch.getGene(j).doubleValue()); } } } return statistics.values().stream() .map(DoubleMomentStatistics::toDoubleMoments) .collect(ISeq.toISeq()); }
@Test public void factoryTest() { final Genotype<DoubleGene> factory = (Genotype<DoubleGene>)_factory; final Genotype<DoubleGene> gt = _factory.newInstance(); Assert.assertEquals(factory.length(), gt.length()); Assert.assertEquals(factory.geneCount(), gt.geneCount()); for (int i = 0; i < factory.length(); ++i) { Assert.assertEquals( factory.getChromosome(i).length(), gt.getChromosome(i).length() ); } }
private static EvolutionStart<DoubleGene, Double> start(final int populationSize, final long generation) { final ISeq<Phenotype<DoubleGene, Double>> population = Genotype.of(DoubleChromosome.of(0, 1)).instances() .map(gt -> Phenotype.of(gt, generation, SpecialEngine::fitness)) .limit(populationSize) .collect(ISeq.toISeq()); return EvolutionStart.of(population, generation); }
@Test public void testCreate() { LongChromosome c1 = LongChromosome.of(0, 100, 10); LongChromosome c2 = LongChromosome.of(0, 100, 10); Genotype<LongGene> g1 = Genotype.of(c1, c2); Genotype<LongGene> g2 = g1.newInstance(); assertNotSame(g1, g2); assertNotEquals(g1, g2); }
/** * Return a new, random genotype by creating new, random chromosomes (calling * the {@link Chromosome#newInstance()} method) from the chromosomes of this * genotype. */ @Override public Genotype<G> newInstance() { return new Genotype<>(_chromosomes.map(Factory::newInstance)); }
public static ISeq<Phenotype<DoubleGene, Double>> population( final int ngenes, final int nchromosomes, final int npopulation ) { final MSeq<DoubleChromosome> chromosomes = MSeq.ofLength(nchromosomes); for (int i = 0; i < nchromosomes; ++i) { chromosomes.set(i, DoubleChromosome.of(0, 10, ngenes)); } final Genotype<DoubleGene> genotype = new Genotype<>(chromosomes.toISeq()); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(npopulation); for (int i = 0; i < npopulation; ++i) { population.set(i, Phenotype.of(genotype.newInstance(), 0, TestUtils.FF)); } return population.toISeq(); }