/** * Returns the path from the root, to get to this node. The last element in * the path is this node. * * @return an array of TreeNode objects giving the path, where the * first element in the path is the root and the last * element is this node. */ public default ISeq<T> getPath() { return Trees.pathToRoot(Trees.<V, T>self(this), 0).toISeq(); }
@Test public void toListIteratorChange() { final MSeq<Integer> mseq = newSeq(20); final ISeq<Integer> iseq1 = mseq.toISeq(); final ListIterator<Integer> it = mseq.listIterator(); it.next(); it.set(300); final ISeq<Integer> iseq2 = mseq.toISeq(); Assert.assertNotEquals(iseq1, iseq2, "ISeq instances must not be equal."); }
@SuppressWarnings("unchecked") private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); _validAlleles = (ISeq<T>)in.readObject(); final MSeq<EnumGene<T>> genes = MSeq.ofLength(_validAlleles.length()); for (int i = 0; i < _validAlleles.length(); ++i) { genes.set(i, new EnumGene<>(in.readInt(), _validAlleles)); } reflect.setField(this, "_genes", genes.toISeq()); }
static <A> ISeq<AnyGene<A>> seq( final IntRange lengthRange, final Supplier<? extends A> supplier, final Predicate<? super A> validator ) { return MSeq.<AnyGene<A>>ofLength(random.nextInt(lengthRange, getRandom())) .fill(() -> of(supplier.get(), supplier, validator)) .toISeq(); }
static ISeq<PolygonGene> seq(final int polygonCount, final int polygonLength) { return MSeq.<PolygonGene>ofLength(polygonCount) .fill(() -> of(Polygon.newRandom(polygonLength))) .toISeq(); }
static ISeq<CharacterGene> seq( final CharSeq chars, final IntRange lengthRange ) { final Random r = RandomRegistry.getRandom(); return MSeq.<CharacterGene>ofLength(random.nextInt(lengthRange, r)) .fill(() -> new CharacterGene(chars, r.nextInt(chars.length()))) .toISeq(); }
@Test public void seqTypes() { final BitChromosome c = BitChromosome.of(100, 0.3); Assert.assertEquals(c.toSeq().getClass(), BitGeneISeq.class); Assert.assertEquals(c.toSeq().copy().getClass(), BitGeneMSeq.class); Assert.assertEquals(c.toSeq().copy().toISeq().getClass(), BitGeneISeq.class); }
static ISeq<BigIntegerGene> seq( final BigInteger minimum, final BigInteger maximum, final int length ) { require.positive(length); final Random r = getRandom(); return MSeq.<BigIntegerGene>ofLength(length) .fill(() -> new BigIntegerGene( random.nextBigInteger(minimum, maximum, r), minimum, maximum)) .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(); }
static ISeq<IntegerGene> seq( final int min, final int max, final IntRange lengthRange ) { final Random r = getRandom(); return MSeq.<IntegerGene>ofLength(random.nextInt(lengthRange, r)) .fill(() -> new IntegerGene(nextInt(r, min, max), min, max)) .toISeq(); }
static ISeq<LongGene> seq( final long min, final long max, final IntRange lengthRange ) { final Random r = getRandom(); return MSeq.<LongGene>ofLength(random.nextInt(lengthRange, r)) .fill(() -> LongGene.of(nextLong(r, min, max), min, max)) .toISeq(); }
@Test(dataProvider = "sequences") public void reverse(final MSeq<Integer> seq) { final ISeq<Integer> original = seq.toISeq(); seq.reverse(); Assert.assertNotEquals(seq, original); for (int i = 0; i < seq.length(); ++i) { Assert.assertEquals(seq.get(i), original.get(seq.length() - i - 1)); } Assert.assertEquals(original, seq.reverse()); }
@Test(expectedExceptions = IndexOutOfBoundsException.class) public void valueOfIndexOutOfBounds2() { final int length = 100; final ISeq<Integer> alleles = MSeq.<Integer>ofLength(length).fill(Int()).toISeq(); new EnumGene<>(-1, alleles); }
@Test(expectedExceptions = IllegalArgumentException.class) public void valueOfZeroLength() { final int length = 0; final ISeq<Integer> alleles = MSeq.<Integer>ofLength(length).fill(Int()).toISeq(); EnumGene.of(alleles); }
public static ISeq<Phenotype<DoubleGene, Double>> newDoublePopulation( final int length, final double min, final double max ) { final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(length); for (int i = 0; i < length; ++i) { population.set(i, newDoublePhenotype(min, max)); } return population.toISeq(); }
@Test(expectedExceptions = IndexOutOfBoundsException.class) public void valueOfIndexOutOfBounds1() { final int length = 100; final ISeq<Integer> alleles = MSeq.<Integer>ofLength(length).fill(Int()).toISeq(); new EnumGene<>(length + 1, alleles); }
@Test public void invalidChromosome() { final ISeq<Integer> alleles = ISeq.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); final EnumGene<Integer> gene = new EnumGene<>(3, alleles); final ISeq<EnumGene<Integer>> genes = MSeq.<EnumGene<Integer>>ofLength(10) .fill(() -> gene) .toISeq(); final PermutationChromosome<Integer> chromosome = new PermutationChromosome<>(genes); Assert.assertFalse(chromosome.isValid()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void selectNegativeCountArgument() { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0.0, 1.0)); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(2); for (int i = 0, n = 2; i < n; ++i) { population.set(i, Phenotype.of(gtf.newInstance(), 12, TestUtils.FF)); } selector().select(population.toISeq(), -1, Optimize.MAXIMUM); }
@Test public void crossoverWithIllegalChromosome() { 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)); pmco.crossover(that, other); }