IntermediateCrossover extends Crossover<G, C> { @Override protected int crossover(final MSeq<G> v, final MSeq<G> w) { final Random random = RandomRegistry.random(); final double min = v.get(0).min().doubleValue(); final double max = v.get(0).max().doubleValue(); for (int i = 0, n = min(v.length(), w.length()); i < n; ++i) { final var g1 = v.get(i); final var g2 = w.get(i); if (g1.isValid() && g2.isValid()) { final double vi = g1.doubleValue(); final double wi = g2.doubleValue(); double t, s; do { final double a = nextDouble(-_p, 1 + _p, random); final double b = nextDouble(-_p, 1 + _p, random); t = a*vi + (1 - a)*wi; s = b*wi + (1 - b)*vi; } while (t < min || s < min || t >= max || s >= max); v.set(i, v.get(i).newInstance(t)); w.set(i, w.get(i).newInstance(s)); } } return 2; } IntermediateCrossover(final double probability, final double p);  IntermediateCrossover(final double probability);  IntermediateCrossover(); @Override String toString();  }
@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); Assert.assertTrue(v.forAll(DoubleGene::isValid)); Assert.assertTrue(w.forAll(DoubleGene::isValid)); final var co = new IntermediateCrossover<DoubleGene, Double>(0.1, 0); co.crossover(v, w); Assert.assertTrue(v.forAll(DoubleGene::isValid)); Assert.assertTrue(w.forAll(DoubleGene::isValid)); }
@Test(timeOut = 1000, dataProvider = "recombinations") public void recombineEqualIndividuals( final double min, final double max, final double v1, final double v2 ) { final MSeq<DoubleGene> u = MSeq.of( DoubleGene.of(v1, min, max), DoubleGene.of(v1, min, max), DoubleGene.of(v1, min, max), DoubleGene.of(v1, min, max), DoubleGene.of(v1, min, max) ); final MSeq<DoubleGene> v = MSeq.of( DoubleGene.of(v2, min, max), DoubleGene.of(v2, min, max), DoubleGene.of(v2, min, max), DoubleGene.of(v2, min, max), DoubleGene.of(v2, min, max) ); final var recombinator = new IntermediateCrossover<DoubleGene, Double>(); recombinator.crossover(v, u); }