/** * Create a new enum gene from the given valid genes and the chosen allele * index. * * @since 3.4 * * @param <A> the allele type * @param alleleIndex the index of the allele for this gene. * @param validAlleles the sequence of valid alleles. * @return a new {@code EnumGene} with the given with the allele * {@code validAlleles.get(alleleIndex)} * @throws IllegalArgumentException if the give valid alleles sequence is * empty * @throws NullPointerException if the valid alleles seq is {@code null}. */ public static <A> EnumGene<A> of( final int alleleIndex, final ISeq<? extends A> validAlleles ) { return new EnumGene<>(alleleIndex, validAlleles); }
/** * Create a new gene from the given {@code value} and the gene context. * * @since 1.6 * * @param value the value of the new gene. * @return a new gene with the given value. */ public EnumGene<A> newInstance(final A value) { return new EnumGene<>( _validAlleles.indexOf(value), _validAlleles ); }
/** * Create a new enum gene from the given valid genes and the chosen allele * index. * * @param <A> the allele type * @param alleleIndex the index of the allele for this gene. * @param validAlleles the array of valid alleles. * @return a new {@code EnumGene} with the given with the allele * {@code validAlleles[alleleIndex]} * @throws java.lang.IllegalArgumentException if the give valid alleles * array is empty of the allele index is out of range. */ @SafeVarargs public static <A> EnumGene<A> of( final int alleleIndex, final A... validAlleles ) { return new EnumGene<>(alleleIndex, ISeq.of(validAlleles)); }
@Override public EnumGene<A> newInstance() { return new EnumGene<>( RandomRegistry.getRandom().nextInt(_validAlleles.length()), _validAlleles ); }
/** * Return a new enum gene with an allele randomly chosen from the given * valid alleles. * * @param <A> the allele type * @param validAlleles the sequence of valid alleles. * @return a new {@code EnumGene} with an randomly chosen allele from the * sequence of valid alleles * @throws java.lang.IllegalArgumentException if the give valid alleles * sequence is empty * @throws NullPointerException if the valid alleles seq is {@code null}. */ public static <A> EnumGene<A> of(final ISeq<? extends A> validAlleles) { return new EnumGene<>( RandomRegistry.getRandom().nextInt(validAlleles.length()), validAlleles ); }
@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 = 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 public void valueOf() { final int length = 100; final ISeq<Integer> alleles = MSeq.<Integer>ofLength(length).fill(Int()).toISeq(); Assert.assertEquals(alleles.length(), length); for (int i = 0; i < alleles.length(); ++i) { Assert.assertEquals(alleles.get(i), Integer.valueOf(i)); } for (int i = 0; i < alleles.length(); ++i) { Assert.assertEquals(new EnumGene<>(i, alleles).getAllele(), Integer.valueOf(i)); Assert.assertSame(new EnumGene<>(i, alleles).getValidAlleles(), 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 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); }
@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(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"); }