/** * Create a new {@code Knapsack} definition with the given * * @param items the basic {@link Set} of knapsack items. * @param knapsackSize the maximal knapsack size * @throws NullPointerException if the {@code items} set is {@code null} */ public Knapsack(final ISeq<Item> items, final double knapsackSize) { _codec = Codecs.ofSubSet(items); _knapsackSize = knapsackSize; }
@Override public Codec<ISeq<Record>, BitGene> codec() { return Codecs.ofSubSet(_records); }
@Override public Codec<ISeq<Integer>, EnumGene<Integer>> codec() { return Codecs.ofSubSet(_basicSet, _size); }
@Test public void ofSubSet() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs.ofSubSet( ISeq.of("1", "2", "3", "4", "5"), 3 ); for (int i = 0; i < 100; ++i) { final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); final Chromosome<EnumGene<String>> ch = gt.getChromosome(); Assert.assertEquals(ch.length(), 3); Assert.assertTrue(ch.isValid()); final ISeq<String> permutation = codec.decoder().apply(gt); Assert.assertEquals(permutation.length(), 3); } }
.builder(fitness(kssize), Codecs.ofSubSet(items)) .populationSize(500) .survivorsSelector(new TournamentSelector<>(5))