/** * Randomize the {@code array} using the {@link Random} object currently * registered in the {@link RandomRegistry} class. The used shuffling * algorithm is from D. Knuth TAOCP, Seminumerical Algorithms, Third edition, * page 142, Algorithm S (Selection sampling technique). * * @return this shuffled sequence */ public default MSeq<T> shuffle() { return shuffle(RandomRegistry.getRandom()); }
@Test(dataProvider = "sequences") public void sort(final MSeq<Integer> seq) { seq.shuffle(new Random(23)); Assert.assertFalse(seq.isSorted()); seq.sort(); Assert.assertTrue(seq.isSorted()); }
@Test public void toSeqMSeqChange() { final MSeq<Integer> mseq = newSeq(20); final ISeq<Integer> iseq1 = mseq.toISeq(); mseq.shuffle(); final ISeq<Integer> iseq2 = mseq.toISeq(); Assert.assertNotEquals(iseq1, iseq2, "ISeq instances must not be equal."); }
@Test(dataProvider = "sequences") public void sortWithComparator(final MSeq<Integer> seq) { seq.shuffle(new Random(23)); Assert.assertFalse(seq.isSorted()); seq.sort((a, b) -> b.compareTo(a)); Assert.assertTrue(seq.isSorted((a, b) -> b.compareTo(a))); Assert.assertTrue(seq.reverse().isSorted()); }
@Test(dataProvider = "sequences") public void sortWithStart(final MSeq<Integer> seq) { seq.shuffle(new Random(23)); Assert.assertFalse(seq.isSorted()); seq.sort(2); Assert.assertTrue(seq.subSeq(2).isSorted()); Assert.assertFalse(seq.isSorted()); }
@Test(dataProvider = "sequences") public void sortWithEnd(final MSeq<Integer> seq) { seq.shuffle(new Random(23)); Assert.assertFalse(seq.isSorted()); seq.sort(0, seq.length() - 2); Assert.assertTrue(seq.subSeq(0, seq.length() - 2).isSorted()); Assert.assertFalse(seq.isSorted()); }
@Test public void probabilities() { final ISeq<Phenotype<DoubleGene, Double>> population = TestUtils .newDoublePopulation(100).copy() .shuffle() .toISeq(); final S selector = factory().newInstance(); final double[] props = selector.probabilities(population, 23); Assert.assertEquals(props.length, population.size()); // if (isSorted()) { // assertSortedDescending(population); // assertSortedDescending(props); // } Assert.assertEquals(sum(props), 1.0, 0.000001); assertPositive(props); }
@Test(invocationCount = 20) public void frontMax() { final Random random = new Random(); final ISeq<Vec<double[]>> rank0 = frontMax(20, 50, random); final ISeq<Vec<double[]>> rank1 = frontMax(15, 50, random); final ISeq<Vec<double[]>> rank2 = frontMax(10, 50, random); final ISeq<Vec<double[]>> rank3 = frontMax(5, 50, random); final ISeq<Vec<double[]>> rank4 = frontMax(1, 25, random); final ISeq<Vec<double[]>> points = rank0 .append(rank1) .append(rank2) .append(rank3) .append(rank4) .copy().shuffle().toISeq(); final ISeq<Vec<double[]>> front = Pareto.front(points); Assert.assertEquals(new HashSet<>(front.asList()), new HashSet<>(rank0.asList())); }
@Test public void bestWorstPhenotype() { final int length = 100; final Function<Genotype<IntegerGene>, Integer> ff = gt -> gt.getGene().getAllele(); final MSeq<Phenotype<IntegerGene, Integer>> population = MSeq.ofLength(length); for (int i = 0; i < length; ++i) { final Genotype<IntegerGene> gt = Genotype.of(IntegerChromosome.of( IntegerGene.of(i, 0, length) )); population.set(i, Phenotype.of(gt, 1, ff)); } population.shuffle(RandomRegistry.getRandom()); final EvolutionResult<IntegerGene, Integer> maxResult = EvolutionResult.of( Optimize.MAXIMUM, population.toISeq(), 0, 0, EvolutionDurations.ZERO, 0, 0, 0 ); Assert.assertEquals(maxResult.getBestFitness().intValue(), length - 1); Assert.assertEquals(maxResult.getWorstFitness().intValue(), 0); final EvolutionResult<IntegerGene, Integer> minResult = EvolutionResult.of( Optimize.MINIMUM, population.toISeq(), 0, 0, EvolutionDurations.ZERO, 0, 0, 0 ); Assert.assertEquals(minResult.getBestFitness().intValue(), 0); Assert.assertEquals(minResult.getWorstFitness().intValue(), length - 1); }
@Test(invocationCount = 10) public void crossover() { final PartiallyMatchedCrossover<Integer, Double> pmco = new PartiallyMatchedCrossover<>(1); final int length = 1000; final MSeq<Integer> alleles = MSeq.<Integer>ofLength(length).fill(Int()); final ISeq<Integer> ialleles = alleles.toISeq(); final MSeq<EnumGene<Integer>> that = alleles.map(i -> new EnumGene<>(i, ialleles)); final MSeq<EnumGene<Integer>> other = alleles.map(i -> new EnumGene<>(i, ialleles)); that.shuffle(); other.shuffle(); final PermutationChromosome<Integer> thatChrom1 = new PermutationChromosome<>(that.toISeq()); Assert.assertTrue(thatChrom1.isValid(), "thatChrom1 not valid"); final PermutationChromosome<Integer> otherChrom1 = new PermutationChromosome<>(other.toISeq()); Assert.assertTrue(otherChrom1.isValid(), "otherChrom1 not valid"); pmco.crossover(that, other); final PermutationChromosome<Integer> thatChrom2 = new PermutationChromosome<>(that.toISeq()); Assert.assertTrue(thatChrom2.isValid(), "thatChrom2 not valid: " + thatChrom2.toSeq()); final PermutationChromosome<Integer> otherChrom2 = new PermutationChromosome<>(other.toISeq()); Assert.assertTrue(otherChrom2.isValid(), "otherChrom2 not valid: " + otherChrom2.toSeq()); Assert.assertFalse(thatChrom1.equals(thatChrom2), "That chromosome must not be equal"); Assert.assertFalse(otherChrom1.equals(otherChrom2), "That chromosome must not be equal"); }
private static EvolutionResult<IntegerGene, Integer> newResult( final Optimize opt, final int value ) { final int length = 1000; final Function<Genotype<IntegerGene>, Integer> ff = gt -> gt.getGene().getAllele(); final MSeq<Phenotype<IntegerGene, Integer>> pop = MSeq.ofLength(length); for (int i = 0; i < length; ++i) { final Genotype<IntegerGene> gt = Genotype.of(IntegerChromosome.of( IntegerGene.of(value, 0, length) )); pop.set(i, Phenotype.of(gt, 1, ff)); } pop.shuffle(RandomRegistry.getRandom()); return EvolutionResult .of(opt, pop.toISeq(), 0, 0, EvolutionDurations.ZERO, 0, 0, 0); }