@Override public DoubleGene newInstance(final Double value) { return of(value, _min, _max); }
@Override public DoubleGene mean(final DoubleGene that) { return of(_value + (that._value - _value)/2.0, _min, _max); }
@Override public DoubleGene newInstance(final Number number) { return of(number.doubleValue(), _min, _max); }
@Override protected Factory<DoubleGene> factory() { return () -> DoubleGene.of(0, Double.MAX_VALUE); }
/** * Create a new random {@code DoubleGene}. It is guaranteed that the value * of the {@code DoubleGene} lies in the interval [min, max). * * @param min the minimal valid value of this gene (inclusively). * @param max the maximal valid value of this gene (exclusively). * @return a new {@code DoubleGene} with the given parameter */ public static DoubleGene of(final double min, final double max) { return of(nextDouble(min, max, getRandom()), min, max); }
@Override public DoubleGene newInstance() { return of(nextDouble(_min, _max, getRandom()), _min, _max); }
@Test public void createInvalidNumber() { final DoubleGene gene = DoubleGene.of(0.0, 1.0, 2.0); Assert.assertFalse(gene.isValid()); }
@Test public void doubleGeneIntegerInteger() { DoubleGene gene = DoubleGene.of(-10.567, 10.567); assertEquals(gene.getMin(), -10.567); assertEquals(gene.getMax(), 10.567); }
/** * Create a new random {@code DoubleGene}. It is guaranteed that the value * of the {@code DoubleGene} lies in the interval [min, max). * * @since 3.2 * * @param range the double range to use * @return a new {@code DoubleGene} with the given parameter * @throws NullPointerException if the given {@code range} is {@code null}. */ public static DoubleGene of(final DoubleRange range) { return of(nextDouble(range.getMin(), range.getMax(), getRandom()), range); }
@Test public void createNumber() { DoubleGene gene = DoubleGene.of(1.2345, -1234.1234, 1234.1234); DoubleGene g2 = gene.newInstance(5.0); assertEquals(g2.getAllele().intValue(), 5); assertEquals(g2.getMin(), -1234.1234); assertEquals(g2.getMax(), 1234.1234); }
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); final MSeq<DoubleGene> genes = MSeq.ofLength(in.readInt()); reflect.setField(this, "_lengthRange", in.readObject()); reflect.setField(this, "_min", in.readDouble()); reflect.setField(this, "_max", in.readDouble()); for (int i = 0; i < genes.length(); ++i) { genes.set(i, DoubleGene.of(in.readDouble(), _min, _max)); } reflect.setField(this, "_genes", genes.toISeq()); }
static ISeq<DoubleGene> seq( final double min, final double max, final IntRange lengthRange ) { final Random r = getRandom(); return MSeq.<DoubleGene>ofLength(random.nextInt(lengthRange, r)) .fill(() -> of(nextDouble(min, max, r), min, max)) .toISeq(); }
@Test public void recombine() { final Factory<DoubleGene> factory = DoubleGene.of(0, 100); final MSeq<DoubleGene> v = MSeq.of(factory::newInstance, 10); final MSeq<DoubleGene> w = MSeq.of(factory::newInstance, 10); final IntermediateCrossover<DoubleGene, Double> recombinator = new IntermediateCrossover<>(0.1, 10); recombinator.crossover(v, w); Assert.assertTrue(v.forAll(DoubleGene::isValid)); Assert.assertTrue(w.forAll(DoubleGene::isValid)); }
@Test public void recombine() { final Factory<DoubleGene> factory = DoubleGene.of(0, 100); final MSeq<DoubleGene> v = MSeq.of(factory::newInstance, 10); final MSeq<DoubleGene> w = MSeq.of(factory::newInstance, 10); final LineCrossover<DoubleGene, Double> recombinator = new LineCrossover<>(); recombinator.crossover(v, w); Assert.assertTrue(v.forAll(DoubleGene::isValid)); Assert.assertTrue(w.forAll(DoubleGene::isValid)); }
public static Phenotype<DoubleGene, Double> newDoublePhenotype(final double value) { return Phenotype.of(Genotype.of( DoubleChromosome.of(DoubleGene.of(value, 0, 10))), 0, FF ).evaluate(); }
private static Phenotype<DoubleGene, Double> phenotype(final double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0.0, 1000.0))), 1, a -> a.getGene().getAllele() ); }
private static Phenotype<DoubleGene, Double> pt(double value) { return Phenotype.of( Genotype.of(DoubleChromosome.of(DoubleGene.of(value, 0, 10))), 0, gt -> gt.getGene().getAllele() ); }