@Override public String toString() { return Objects.toString(getAllele()); }
/** * Return a new enum gene with an allele randomly chosen from the given * valid alleles. * * @param <A> the allele type * @param validAlleles the array of valid alleles. * @return a new {@code EnumGene} with an randomly chosen allele from the * sequence of valid alleles * @throws IllegalArgumentException if the give valid alleles array is empty */ @SafeVarargs public static <A> EnumGene<A> of(final A... validAlleles) { return EnumGene.of(ISeq.of(validAlleles)); }
/** * 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); }
@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); } }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeObject(_validAlleles); for (EnumGene<?> gene : _genes) { out.writeInt(gene.getAlleleIndex()); } }
private PermutationChromosome( final ISeq<EnumGene<T>> genes, final Boolean valid ) { super(genes); assert !genes.isEmpty(); _validAlleles = genes.get(0).getValidAlleles(); _valid = valid; }
@Test public void ofPermutation() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs .ofPermutation(ISeq.of("foo", "bar", "zoo")); final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); final Function<Genotype<EnumGene<String>>, ISeq<String>> f = codec.decoder(); final ISeq<String> value = f.apply(gt); Assert.assertEquals(value.length(), gt.getChromosome().length()); for (int i = 0; i < value.length(); ++i) { Assert.assertEquals(value.get(i), gt.get(0, i).toString()); } }
/** * Check if this chromosome represents still a valid permutation (or subset) * of the given valid alleles. */ @Override public boolean isValid() { if (_valid == null) { final byte[] check = bit.newArray(_validAlleles.length()); _valid = _genes.forAll(g -> !getAndSet(check, g.getAlleleIndex())); } return _valid; }
@Override public EnumGene<Integer> newInstance() { return EnumGene.of(_alleles); }
@Override public String toString() { return _genes.asList().stream() .map(g -> g.getAllele().toString()) .collect(Collectors.joining("|")); }
/** * 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 writer for permutation-chromosomes. How to write the valid * alleles is defined by the given {@link Writer}. * * @param alleleWriter the allele writer * @param <A> the allele type * @return a new permutation chromosome writer * @throws NullPointerException if the given allele {@code writer} is * {@code null} */ public static <A> Writer<io.jenetics.PermutationChromosome<A>> writer(final Writer<? super A> alleleWriter) { return Writer.<io.jenetics.PermutationChromosome<A>>elem( ROOT_NAME, attr(LENGTH_NAME).map(io.jenetics.PermutationChromosome::length), elem(VALID_ALLELES_NAME, attr("type").map(PermutationChromosome::toAlleleTypeName), Writer.<A>elems(ALLELE_NAME, alleleWriter) .map(ch -> ch.getValidAlleles()) ), elem(ORDER_NAME, text()) .map(ch -> ch.stream() .map(g -> Integer.toString(g.getAlleleIndex())) .collect(Collectors.joining(" "))) ); }
@Test public void isValid() { final ISeq<Integer> alleles = IntStream.range(0, 100) .boxed() .collect(ISeq.toISeq()); final ISeq<EnumGene<Integer>> genes = IntStream.of(comb.subset(100, 10)) .mapToObj(i -> EnumGene.of(i, alleles)) .collect(ISeq.toISeq()); final PermutationChromosome<Integer> ch = new PermutationChromosome<>(genes); Assert.assertTrue(ch.isValid()); Assert.assertEquals(ch.length(), 10); }
private static String toAlleleTypeName( final io.jenetics.PermutationChromosome<?> ch ) { return ch.getGene().getAllele().getClass().getCanonicalName(); }
/** * 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)); }
@Test public void isNotValid() { final ISeq<Integer> alleles = IntStream.range(0, 100) .boxed() .collect(ISeq.toISeq()); final ISeq<EnumGene<Integer>> genes = IntStream.of(comb.subset(100, 10)) .mapToObj(i -> EnumGene.of(i%3, alleles)) .collect(ISeq.toISeq()); final PermutationChromosome<Integer> ch = new PermutationChromosome<>(genes); System.out.println(ch); Assert.assertFalse(ch.isValid()); Assert.assertEquals(ch.length(), 10); }
@Override public EnumGene<A> newInstance() { return new EnumGene<>( RandomRegistry.getRandom().nextInt(_validAlleles.length()), _validAlleles ); }
public static EnumGene<Long> nextEnumGeneLong() { return EnumGene.of(ISeq.of(random()::nextLong, 5)); }
/** * 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 ); }