/** * Create a new random {@code IntegerChromosome} of length one. * * @param min the minimal value of this chromosome (inclusively). * @param max the maximal value of this chromosome (inclusively). * @return a new random {@code IntegerChromosome} of length one */ public static IntegerChromosome of(final int min, final int max) { return of(min, max, 1); }
@Override public IntegerChromosome newInstance(final ISeq<IntegerGene> genes) { return new IntegerChromosome(genes, lengthRange()); }
@Override public IntegerChromosome 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<int[], IntegerGene> ofVector( final IntRange domain, final int length ) { requireNonNull(domain); require.positive(length); return Codec.of( Genotype.of(IntegerChromosome.of(domain, length)), gt -> gt.getChromosome().as(IntegerChromosome.class).toArray() ); }
static void write(final IntegerChromosome ch, final XMLStreamWriter xml) throws XMLStreamException { xml.writeStartElement("int-chromosome"); xml.writeAttribute("length", Integer.toString(ch.length())); xml.writeStartElement("min"); xml.writeCharacters(ch.getMin().toString()); xml.writeEndElement(); xml.writeStartElement("max"); xml.writeCharacters(ch.getMax().toString()); xml.writeEndElement(); xml.writeStartElement("alleles"); for (IntegerGene gene : ch) { xml.writeStartElement("allele"); xml.writeCharacters(gene.getAllele().toString()); xml.writeEndElement(); } xml.writeEndElement(); xml.writeEndElement(); }
/** * Returns an int 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 int[] toArray(final int[] array) { final int[] a = array.length >= length() ? array : new int[length()]; for (int i = length(); --i >= 0;) { a[i] = intValue(i); } return a; }
@Test(invocationCount = 20, successPercentage = 95) public void newInstanceDistribution() { using(new Random(12345), r -> { final int min = 0; final int max = 10000000; final MinMax<Integer> mm = MinMax.of(); final Histogram<Integer> histogram = Histogram.ofInteger(min, max, 10); for (int i = 0; i < 1000; ++i) { final IntegerChromosome chromosome = IntegerChromosome.of(min, max, 500); chromosome.toSeq().forEach(g -> { mm.accept(g.getAllele()); histogram.accept(g.getAllele()); }); } Assert.assertTrue(mm.getMin().compareTo(0) >= 0); Assert.assertTrue(mm.getMax().compareTo(100) <= 100); assertUniformDistribution(histogram); }); }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(length()); out.writeObject(lengthRange()); out.writeInt(_min); out.writeInt(_max); for (IntegerGene gene : _genes) { out.writeInt(gene.getAllele()); } }
/** * Returns a sequential stream of the alleles with this chromosome as its * source. * * @since 4.3 * * @return a sequential stream of alleles */ public IntStream intStream() { return IntStream.range(0, length()).map(this::intValue); }
/** * Create a new random chromosome. * * @since 4.0 * * @param min the min value of the {@link IntegerGene}s (inclusively). * @param max the max value of the {@link IntegerGene}s (inclusively). * @param lengthRange the allowed length range of the chromosome. * @return a new {@code IntegerChromosome} 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 IntegerChromosome of( final int min, final int max, final IntRange lengthRange ) { final ISeq<IntegerGene> values = IntegerGene.seq(min, max, lengthRange); return new IntegerChromosome(values, lengthRange); }
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 variableDoubleSum() { final Problem<int[], IntegerGene, Integer> problem = Problem.of( array -> IntStream.of(array).sum(), Codec.of( Genotype.of(IntegerChromosome.of(0, 100, IntRange.of(10, 100))), gt -> gt.getChromosome().as(IntegerChromosome.class).toArray() ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem) .alterers( new Mutator<>(), new SwapMutator<>()) .selector(new TournamentSelector<>()) .minimizing() .build(); final int[] result = problem.codec().decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ); Assert.assertTrue(result.length < 50, "result length: " + result.length); //System.out.println(result.length); //System.out.println(Arrays.toString(result)); }
/** * Create a new {@code IntegerChromosome} 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 IntegerChromosome of(final IntegerGene... genes) { return new IntegerChromosome(ISeq.of(genes), IntRange.of(genes.length)); }
public static IntegerChromosome nextIntegerChromosome() { return IntegerChromosome.of(Integer.MIN_VALUE, Integer.MAX_VALUE, 20); }
/** * Create a new {@code IntegerChromosome} 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 IntegerChromosome of(final Iterable<IntegerGene> genes) { final ISeq<IntegerGene> values = ISeq.of(genes); return new IntegerChromosome(values, IntRange.of(values.length())); }
public static IntegerChromosome nextIntegerChromosome() { return IntegerChromosome.of(Integer.MIN_VALUE, Integer.MAX_VALUE, 20); }
/** * Create a new random {@code IntegerChromosome}. * * @param min the min value of the {@link IntegerGene}s (inclusively). * @param max the max value of the {@link IntegerGene}s (inclusively). * @param length the length of the chromosome. * @return a new random {@code IntegerChromosome} * @throws IllegalArgumentException if the length is smaller than one */ public static IntegerChromosome of( final int min, final int max, final int length ) { return of(min, max, IntRange.of(length)); }
/** * Create a new random {@code IntegerChromosome} of length one. * * @since 3.2 * * @param range the integer range of the chromosome. * @return a new random {@code IntegerChromosome} of length one * @throws NullPointerException if the given {@code range} is {@code null} */ public static IntegerChromosome of(final IntRange range) { return of(range.getMin(), range.getMax(), 1); }