import static org.jenetics.engine.EvolutionResult.toBestPhenotype;
import static org.jenetics.engine.limit.bySteadyFitness;

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

import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.Genotype;
import org.jenetics.Mutator;
import org.jenetics.Phenotype;
import org.jenetics.RouletteWheelSelector;
import org.jenetics.SinglePointCrossover;
import org.jenetics.TournamentSelector;
import org.jenetics.engine.Engine;
import org.jenetics.engine.EvolutionStatistics;
import org.jenetics.util.RandomRegistry;

// This class represents a knapsack item, with a specific
// "size" and "value".
final 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.getRandom();
		return new Item(r.nextDouble()*100, r.nextDouble()*100);
	}

	// Create a new 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])
		);
	}
}

// The knapsack fitness function class, which is parametrized with
// the available items and the size of the knapsack.
final class FF
	implements Function<Genotype<BitGene>, Double>
{
	private final Item[] items;
	private final double size;

	public FF(final Item[] items, final double size) {
		this.items = items;
		this.size = size;
	}

	@Override
	public Double apply(final Genotype<BitGene> gt) {
		final Item sum = ((BitChromosome)gt.getChromosome()).ones()
			.mapToObj(i -> items[i])
			.collect(Item.toSum());

		return sum.size <= this.size ? sum.value : 0;
	}
}

// The main class.
public class Knapsack {

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

		final FF ff = new FF(
			Stream.generate(Item::random)
				.limit(nitems)
				.toArray(Item[]::new),
			kssize
		);

		// Configure and build the evolution engine.
		final Engine<BitGene, Double> engine = Engine
			.builder(ff, BitChromosome.of(nitems, 0.5))
			.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);
	}
}

@@ -274,76 +274,11 @@
 .Bit
-Chromosome;%0Aimport org.jenetics.BitGene;%0Aimport org.jenetics.Genotyp
+Gen
 e;%0Ai
@@ -544,16 +544,82 @@
 istics;%0A
+import org.jenetics.engine.codecs;%0Aimport org.jenetics.util.ISeq;%0A
 import o
@@ -1380,404 +1380,174 @@
 The 
-knapsack fitness function class, which is parametrized with%0A// the available items and the size of the knapsack.%0Afinal class FF%0A%09implements Function%3CGenotype%3CBitGene%3E, Double%3E%0A%7B%0A%09private final Item%5B%5D items;%0A%09private final double size;%0A%0A%09public FF(final Item%5B%5D items, final double size) %7B%0A%09%09this.items = items;%0A%09%09this.size = size;%0A%09%7D%0A%0A%09@Override%0A%09public Double apply(final Genotype%3CBitGene%3E gt)
+main class.%0Apublic class Knapsack %7B%0A%0A%09// Creating the fitness function.%0A%09public static Function%3CISeq%3CItem%3E, Double%3E fitness(final double size) %7B%0A%09%09return items -%3E
  %7B%0A
+%09
 %09%09fi
@@ -1565,82 +1565,22 @@
 m = 
-((BitChromosome)gt.getChromosome()).ones()%0A%09%09%09.mapToObj(i -%3E items%5Bi%5D)%0A%09%09%09
+items.stream()
 .col
@@ -1599,19 +1599,19 @@
 Sum());%0A
-%0A
 %09%09
+%09
 return s
@@ -1621,21 +1621,16 @@
 size %3C= 
-this.
 size ? s
@@ -1648,55 +1648,14 @@
 0;%0A%09
-%7D%0A%7D%0A%0A// The main class.%0Apublic class Knapsack %7B
+%09%7D;%0A%09%7D
 %0A%0A%09p
@@ -1780,23 +1780,26 @@
 nal 
-FF ff = new FF(
+ISeq%3CItem%3E items =
 %0A%09%09%09
@@ -1856,42 +1856,29 @@
 %09%09%09.
-toArray(Item%5B%5D::new),%0A%09%09%09kssize%0A%09%09
+collect(ISeq.toISeq()
 );%0A%0A
@@ -1989,39 +1989,45 @@
 er(f
-f, BitChromosome.of(nitems, 0.5
+itness(kssize), codecs.ofSubSet(items
 ))%0A%09

Simplify 'Knapsack' example.