/** * 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; }
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(); }
@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]); } }