/** * Reverses the order of the elements this sequence (in place). * * @return this sequence with reverse order or the elements */ public default MSeq<T> reverse() { for (int i = 0, j = length() - 1; i < j; ++i, --j) { swap(i, j); } return this; }
static <T> void crossover( final MSeq<T> that, final MSeq<T> other, final int index ) { assert index >= 0 : format( "Crossover index must be within [0, %d) but was %d", that.length(), index ); that.swap(index, index + 1, other, index); }
/** * Randomize the {@code array} using the given {@link Random} object. The used * shuffling algorithm is from D. Knuth TAOCP, Seminumerical Algorithms, * Third edition, page 142, Algorithm S (Selection sampling technique). * * @param random the {@link Random} object to use for randomize. * @return this shuffled sequence * @throws NullPointerException if the random object is {@code null}. */ public default MSeq<T> shuffle(final Random random) { for (int j = length() - 1; j > 0; --j) { swap(j, random.nextInt(j + 1)); } return this; }
static <T> void crossover( final MSeq<T> that, final MSeq<T> other, final int[] indexes ) { for (int i = 0; i < indexes.length - 1; i += 2) { final int start = indexes[i]; final int end = indexes[i + 1]; that.swap(start, end, other, start); } if (indexes.length%2 == 1) { final int index = indexes[indexes.length - 1]; that.swap(index, min(that.length(), other.length()), other, index); } }
static <T> void crossover( final MSeq<T> that, final MSeq<T> other, final int index ) { assert index >= 0 : format( "Crossover index must be within [0, %d) but was %d", that.length(), index ); that.swap(index, min(that.length(), other.length()), other, index); }
public static <T> MSeq<T> shuffle(final MSeq<T> array, final Random random) { for (int j = array.length() - 1; j > 0; --j) { array.swap(j, random.nextInt(j + 1)); } return array; }
@Override protected int crossover(final MSeq<G> that, final MSeq<G> other) { final int length = min(that.length(), other.length()); return (int)indexes(RandomRegistry.getRandom(), length, _swapProbability) .peek(i -> that.swap(i, other)) .count(); }
@Override protected int crossover( final MSeq<EnumGene<T>> that, final MSeq<EnumGene<T>> other ) { if (that.length() != other.length()) { throw new IllegalArgumentException(format( "Required chromosomes with same length: %s != %s", that.length(), other.length() )); } if (that.length() >= 2) { final Random random = RandomRegistry.getRandom(); final int[] points = comb.subset(that.length(), 2, random); that.swap(points[0], points[1], other, points[0]); repair(that, other, points[0], points[1]); repair(other, that, points[0], points[1]); } return 1; }
@Test public void swapSamePosition() { MSeq<BitGene> seqWithFalse = newSeq(1); seqWithFalse.set(0, BitGene.FALSE); MSeq<BitGene> seqWithTrue = newSeq(1); seqWithTrue.set(0, BitGene.TRUE); seqWithFalse.swap(0, seqWithTrue); Assert.assertTrue(seqWithFalse.get(0).booleanValue()); Assert.assertFalse(seqWithTrue.get(0).booleanValue()); }
@Test(dataProvider = "sequences") public void swapIntInt(final MSeq<BitGene> seq) { for (int i = 0; i < seq.length() - 3; ++i) { final BitGene[] copy = seq.toArray(new BitGene[0]); final int j = i + 2; final BitGene vi = seq.get(i); final BitGene vj = seq.get(j); seq.swap(i, j); Assert.assertEquals(seq.get(i), vj); Assert.assertEquals(seq.get(j), vi); for (int k = 0; k < seq.length(); ++k) { if (k != i && k != j) { Assert.assertEquals(seq.get(k), copy[k]); } } } }
@Test(dataProvider = "sequences") public void swapIntInt(final MSeq<Integer> seq) { for (int i = 0; i < seq.length() - 3; ++i) { final Integer[] copy = seq.toArray(new Integer[0]); final int j = i + 2; final Integer vi = seq.get(i); final Integer vj = seq.get(j); seq.swap(i, j); Assert.assertEquals(seq.get(i), vj); Assert.assertEquals(seq.get(j), vi); for (int k = 0; k < seq.length(); ++k) { if (k != i && k != j) { Assert.assertEquals(seq.get(k), copy[k]); } } } }
/** * Swaps the genes in the given array, with the mutation probability of this * mutation. */ @Override protected MutatorResult<Chromosome<G>> mutate( final Chromosome<G> chromosome, final double p, final Random random ) { final MutatorResult<Chromosome<G>> result; if (chromosome.length() > 1) { final MSeq<G> genes = chromosome.toSeq().copy(); final int mutations = (int)indexes(random, genes.length(), p) .peek(i -> genes.swap(i, random.nextInt(genes.length()))) .count(); result = MutatorResult.of( chromosome.newInstance(genes.toISeq()), mutations ); } else { result = MutatorResult.of(chromosome); } return result; }
@Test(dataProvider = "sequences") public void swapIntIntMSeqInt(final MSeq<Integer> seq) { for (int start = 0; start < seq.length() - 3; ++start) { final long seed = random.seed(); final Random random = new Random(seed); final MSeq<Integer> other = newSeq(seq.length()); final MSeq<Integer> otherCopy = newSeq(seq.length()); for (int j = 0; j < other.length(); ++j) { other.set(j, random.nextInt()); otherCopy.set(j, other.get(j)); } final Integer[] copy = seq.toArray(new Integer[0]); final int end = start + 2; final int otherStart = 1; seq.swap(start, end, other, otherStart); for (int j = start; j < end; ++j) { Assert.assertEquals(seq.get(j), otherCopy.get(j + otherStart - start)); } for (int j = 0; j < (end - start); ++j) { Assert.assertEquals(other.get(j + otherStart), copy[j + start]); } } }
@Test(dataProvider = "sequences") public void swapIntIntMSeqInt(final MSeq<BitGene> seq) { for (int start = 0; start < seq.length() - 3; ++start) { final long seed = random.seed(); final Random random = new Random(seed); final MSeq<BitGene> other = newSeq(seq.length()); final MSeq<BitGene> otherCopy = newSeq(seq.length()); for (int j = 0; j < other.length(); ++j) { other.set(j, BitGene.of(random.nextBoolean())); otherCopy.set(j, other.get(j)); } final BitGene[] copy = seq.toArray(new BitGene[0]); final int end = start + 2; final int otherStart = 1; seq.swap(start, end, other, otherStart); for (int j = start; j < end; ++j) { Assert.assertEquals(seq.get(j), otherCopy.get(j + otherStart - start)); } for (int j = 0; j < (end - start); ++j) { Assert.assertEquals(other.get(j + otherStart), copy[j + start]); } } }