import static io.jenetics.engine.EvolutionResult.toBestPhenotype;
import static io.jenetics.engine.Limits.bySteadyFitness;

import java.util.Random;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;

import io.jenetics.BitGene;
import io.jenetics.Mutator;
import io.jenetics.Phenotype;
import io.jenetics.RouletteWheelSelector;
import io.jenetics.SinglePointCrossover;
import io.jenetics.TournamentSelector;
import io.jenetics.engine.Codecs;
import io.jenetics.engine.Engine;
import io.jenetics.engine.EvolutionStatistics;
import io.jenetics.util.ISeq;
import io.jenetics.util.RandomRegistry;

// The main class.
public class Knapsack {

	// This class represents a knapsack item, with a specific
	// "size" and "value".
	final static class Item {
		public final double size;
		public final double value;

		Item(final double size, final double value) {
			this.size = size;
			this.value = value;
		}

		// Create a new random knapsack item.
		static Item random() {
			final Random r = RandomRegistry.random();
			return new Item(
				r.nextDouble()*100,
				r.nextDouble()*100
			);
		}

		// Collector for summing up the knapsack items.
		static Collector<Item, ?, Item> toSum() {
			return Collector.of(
				() -> new double[2],
				(a, b) -> {a[0] += b.size; a[1] += b.value;},
				(a, b) -> {a[0] += b[0]; a[1] += b[1]; return a;},
				r -> new Item(r[0], r[1])
			);
		}
	}

	// Creating the fitness function.
	static Function<ISeq<Item>, Double>
	fitness(final double size) {
		return items -> {
			final Item sum = items.stream().collect(Item.toSum());
			return sum.size <= size ? sum.value : 0;
		};
	}

	public static void main(final String[] args) {
		final int nitems = 15;
		final double kssize = nitems*100.0/3.0;

		final ISeq<Item> items =
			Stream.generate(Item::random)
				.limit(nitems)
				.collect(ISeq.toISeq());

		// Configure and build the evolution engine.
		final Engine<BitGene, Double> engine = Engine
			.builder(fitness(kssize), Codecs.ofSubSet(items))
			.populationSize(500)
			.survivorsSelector(new TournamentSelector<>(5))
			.offspringSelector(new RouletteWheelSelector<>())
			.alterers(
				new Mutator<>(0.115),
				new SinglePointCrossover<>(0.16))
			.build();

		// Create evolution statistics consumer.
		final EvolutionStatistics<Double, ?>
			statistics = EvolutionStatistics.ofNumber();

		final Phenotype<BitGene, Double> best = engine.stream()
			// Truncate the evolution stream after 7 "steady"
			// generations.
			.limit(bySteadyFitness(7))
			// The evolution will stop after maximal 100
			// generations.
			.limit(100)
			// Update the evaluation statistics after
			// each generation
			.peek(statistics)
			// Collect (reduce) the evolution stream to
			// its best phenotype.
			.collect(toBestPhenotype());

		System.out.println(statistics);
		System.out.println(best);
	}
}

@@ -446,32 +446,65 @@
 namentSelector;%0A
+import io.jenetics.engine.Codec;%0A
 import io.jeneti
@@ -1916,24 +1916,117 @@
 toISeq());%0A%0A
+%09%09// Defining the codec.%0A%09%09final Codec%3CISeq%3CItem%3E, BitGene%3E codec = Codecs.ofSubSet(items);%0A%0A
 %09%09// Configu
@@ -2137,38 +2137,21 @@
 ssize), 
-C
+c
 odec
-s.ofSubSet(items)
 )%0A%09%09%09.po
@@ -2937,65 +2937,377 @@
 %0A%0A%09%09
-System.out.println(statistics);%0A%09%09System.out.println(best
+final ISeq%3CItem%3E knapsack = codec.decode(best.genotype());%0A%0A%09%09System.out.println(statistics);%0A%09%09System.out.println(best);%0A%09%09System.out.println(%22%5Cn%5Cn%22);%0A%09%09System.out.printf(%22Genotype of best item: %25s%25n%22, best.genotype());%0A%0A%09%09final double fillSize = knapsack.stream()%0A%09%09%09.mapToDouble(it -%3E it.size)%0A%09%09%09.sum();%0A%0A%09%09System.out.printf(%22%25.2f%25%25 filled. %25n%22, 100*fillSize/kssize
 );%0A%09

Improve example code.