/** * Create a new random {@code DoubleChromosome} of length one. * * @param min the minimal value of this chromosome (inclusively). * @param max the maximal value of this chromosome (exclusively). * @return a new {@code DoubleChromosome} with the given parameter */ public static DoubleChromosome of(final double min, final double max) { return of(min, max, 1); }
@Test public void doubleStream() { final DoubleChromosome chromosome = DoubleChromosome.of(0, 1, 1000); final double[] values = chromosome.doubleStream().toArray(); Assert.assertEquals(values.length, 1000); for (int i = 0; i < values.length; ++i) { Assert.assertEquals(chromosome.getGene(i).doubleValue(), values[i]); Assert.assertEquals(chromosome.doubleValue(i), values[i]); } }
/** * Returns an double array containing all of the elements in this chromosome * in proper sequence. If the chromosome fits in the specified array, it is * returned therein. Otherwise, a new array is allocated with the length of * this chromosome. * * @since 3.0 * * @param array the array into which the elements of this chromosomes are to * be stored, if it is big enough; otherwise, a new array is * allocated for this purpose. * @return an array containing the elements of this chromosome * @throws NullPointerException if the given {@code array} is {@code null} */ public double[] toArray(final double[] array) { final double[] a = array.length >= length() ? array : new double[length()]; for (int i = length(); --i >= 0;) { a[i] = doubleValue(i); } return a; }
@Override public DoubleChromosome newInstance(final ISeq<DoubleGene> genes) { return new DoubleChromosome(genes, lengthRange()); }
@Override public DoubleChromosome newInstance() { return of(_min, _max, lengthRange()); }
/** * Return a vector {@code Codec} for the given range. All vector values * are restricted by the same domain. * * @param domain the domain of the vector values * @param length the vector length * @return a new vector {@code Codec} * @throws NullPointerException if the given {@code domain} is {@code null} * @throws IllegalArgumentException if the {@code length} is smaller than * one. */ public static Codec<double[], DoubleGene> ofVector( final DoubleRange domain, final int length ) { requireNonNull(domain); require.positive(length); return Codec.of( Genotype.of(DoubleChromosome.of(domain, length)), gt -> gt.getChromosome().as(DoubleChromosome.class).toArray() ); }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(length()); out.writeObject(lengthRange()); out.writeDouble(_min); out.writeDouble(_max); for (DoubleGene gene : _genes) { out.writeDouble(gene.getAllele()); } }
private static double fitness(final Genotype gt) { final DoubleChromosome dc = (DoubleChromosome)gt.getChromosome(0); final BitChromosome bc = (BitChromosome)gt.getChromosome(1); final IntegerChromosome ic = (IntegerChromosome)gt.getChromosome(2); return dc.doubleValue() + bc.bitCount() + ic.doubleValue(); }
/** * Returns a sequential stream of the alleles with this chromosome as its * source. * * @since 4.3 * * @return a sequential stream of alleles */ public DoubleStream doubleStream() { return IntStream.range(0, length()).mapToDouble(this::doubleValue); }
/** * Create a new random chromosome. * * @since 4.0 * * @param min the min value of the {@link DoubleGene}s (inclusively). * @param max the max value of the {@link DoubleGene}s (exclusively). * @param lengthRange the allowed length range of the chromosome. * @return a new {@code DoubleChromosome} with the given parameter * @throws IllegalArgumentException if the length of the gene sequence is * empty, doesn't match with the allowed length range, the minimum * or maximum of the range is smaller or equal zero or the given * range size is zero. * @throws NullPointerException if the given {@code lengthRange} is * {@code null} */ public static DoubleChromosome of( final double min, final double max, final IntRange lengthRange ) { final ISeq<DoubleGene> genes = DoubleGene.seq(min, max, lengthRange); return new DoubleChromosome(genes, lengthRange); }
/** * Create a new {@code DoubleChromosome} with the given genes. * * @param genes the genes of the chromosome. * @return a new chromosome with the given genes. * @throws IllegalArgumentException if the length of the genes array is * empty. */ public static DoubleChromosome of(final DoubleGene... genes) { return new DoubleChromosome(ISeq.of(genes), IntRange.of(genes.length)); }
public static DoubleChromosome nextDoubleChromosome() { return DoubleChromosome.of(0.0, 1.0, 20); }
/** * Create a new {@code DoubleChromosome} with the given genes. * * @since 4.3 * * @param genes the genes of the chromosome. * @return a new chromosome with the given genes. * @throws NullPointerException if the given {@code genes} are {@code null} * @throws IllegalArgumentException if the length of the genes array is * empty. */ public static DoubleChromosome of(final Iterable<DoubleGene> genes) { final ISeq<DoubleGene> values = ISeq.of(genes); return new DoubleChromosome(values, IntRange.of(values.length())); }
public static DoubleChromosome nextDoubleChromosome() { return DoubleChromosome.of(0.0, 1.0, 20); }
/** * Create a new random {@code DoubleChromosome}. * * @param min the min value of the {@link DoubleGene}s (inclusively). * @param max the max value of the {@link DoubleGene}s (exclusively). * @param length the length of the chromosome. * @return a new {@code DoubleChromosome} with the given parameter * @throws IllegalArgumentException if the {@code length} is smaller than * one. */ public static DoubleChromosome of( final double min, final double max, final int length ) { return of(min, max, IntRange.of(length)); }
/** * Create a new random {@code DoubleChromosome} of length one. * * @since 3.2 * * @param range the double range of the chromosome. * @return a new random {@code DoubleChromosome} of length one * @throws NullPointerException if the given {@code range} is {@code null} */ public static DoubleChromosome of(final DoubleRange range) { return of(range.getMin(), range.getMax()); }
/** * Create a new random {@code DoubleChromosome}. * * @since 3.2 * * @param range the integer range of the chromosome. * @param length the length of the chromosome. * @return a new random {@code DoubleChromosome} * @throws NullPointerException if the given {@code range} is {@code null} * @throws IllegalArgumentException if the {@code length} is smaller than * one. */ public static DoubleChromosome of(final DoubleRange range, final int length) { return of(range.getMin(), range.getMax(), length); }