@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()); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void set() { final MSeq<Integer> proxy = MSeq.ofLength(1000); final List<Integer> list = new SeqList<>(proxy); list.set(34, 23); }
@Override public <B> Seq<B> map(final Function<? super T, ? extends B> mapper) { requireNonNull(mapper); final MSeq<B> result = MSeq.ofLength(length()); for (int i = 0; i < length(); ++i) { result.set(i, mapper.apply(get(i))); } return result.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(); }
@Test public void size() { final MSeq<Integer> proxy = MSeq.ofLength(1000); final List<Integer> list = new SeqList<>(proxy); Assert.assertEquals(list.size(), proxy.length()); }
static ISeq<PolygonGene> seq(final int polygonCount, final int polygonLength) { return MSeq.<PolygonGene>ofLength(polygonCount) .fill(() -> of(Polygon.newRandom(polygonLength))) .toISeq(); }
@DataProvider(name = "numberOfCrossoverPoints") public Iterator<Object[]> getNumberOfCrossoverPoints() { return MSeq.<Object[]>ofLength(11).fill(new Supplier<Object[]>() { private int point = 0; @Override public Object[] get() { return new Object[]{++point}; } }).iterator(); }
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(); }
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<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<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(); }
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 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); }
@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 validPredicate() { final MSeq<Verifiable> array = MSeq.ofLength(100); for (int i = 0; i < array.length(); ++i) { array.set(i, () -> true); } Assert.assertEquals(array.indexWhere(o -> !o.isValid()), -1); array.set(77, () -> false); Assert.assertEquals(array.indexWhere(o -> !o.isValid()), 77); }
@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); }