/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.example;

import static org.jenetics.util.math.random.nextDouble;

import java.util.Random;

import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.Chromosome;
import org.jenetics.GeneticAlgorithm;
import org.jenetics.Genotype;
import org.jenetics.Mutator;
import org.jenetics.NumberStatistics;
import org.jenetics.RouletteWheelSelector;
import org.jenetics.SinglePointCrossover;
import org.jenetics.TournamentSelector;
import org.jenetics.util.Factory;
import org.jenetics.util.Function;
import org.jenetics.util.RandomRegistry;

final class Item {
	public final double size;
	public final double value;

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

final class KnapsackFunction
	implements Function<Genotype<BitGene>, Double>
{
	private final Item[] items;
	private final double size;

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

	@Override
	public Double apply(final Genotype<BitGene> genotype) {
		final Chromosome<BitGene> ch = genotype.getChromosome();

		double size = 0;
		double value = 0;
		for (int i = 0, n = ch.length(); i < n; ++i) {
			if (ch.getGene(i).getBit()) {
				size += items[i].size;
				value += items[i].value;
			}
		}

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

public class Knapsack {

	private static KnapsackFunction FF(final int n, final double size) {
		final Random random = RandomRegistry.getRandom();
		final Item[] items = new Item[n];
		for (int i = 0; i < items.length; ++i) {
			items[i] = new Item(
				nextDouble(random, 1, 10),
				nextDouble(random, 1, 15)
			);
		}

		return new KnapsackFunction(items, size);
	}

	public static void main(String[] args) throws Exception {
		final KnapsackFunction ff = FF(15, 100);
		final Factory<Genotype<BitGene>> genotype = Genotype.of(
			BitChromosome.of(15, 0.5)
		);

		final GeneticAlgorithm<BitGene, Double> ga = new GeneticAlgorithm<>(
			genotype, ff
		);
		ga.setPopulationSize(500);
		ga.setStatisticsCalculator(
			new NumberStatistics.Calculator<BitGene, Double>()
		);
		ga.setSurvivorSelector(
			new TournamentSelector<BitGene, Double>(5)
		);
		ga.setOffspringSelector(
			new RouletteWheelSelector<BitGene, Double>()
		);
		ga.setAlterers(
			 new Mutator<BitGene>(0.115),
			 new SinglePointCrossover<BitGene>(0.16)
		);

		ga.setup();
		ga.evolve(100);
		System.out.println(ga.getBestStatistics());
		System.out.println(ga.getBestPhenotype());
	}
}

@@ -1298,30 +1298,106 @@
 cs.util.
-RandomRegistry
+LCG64ShiftRandom;%0Aimport org.jenetics.util.RandomRegistry;%0Aimport org.jenetics.util.Scoped
 ;%0A%0Afinal
@@ -1932,16 +1932,18 @@
 ome();%0A%0A
+%0A%0A
 %09%09double
@@ -2117,24 +2117,97 @@
 ;%0A%09%09%09%7D%0A%09%09%7D%0A%0A
+%09%09//System.out.println(ch + %22 --%3E %22 + (size %3C= this.size ? value : 0));%0A%0A
 %09%09return siz
@@ -2343,22 +2343,75 @@
 %09%09final 
+Item%5B%5D items = new Item%5Bn%5D;%0A%09%09try (Scoped%3C? extends 
 Random
+%3E
  random 
@@ -2411,16 +2411,21 @@
 random =
+%0A%09%09%09%09
  RandomR
@@ -2436,59 +2436,49 @@
 try.
-getRandom();%0A%09%09final Item%5B%5D items = new Item%5Bn%5D;
+scope(new LCG64ShiftRandom(123)))%0A%09%09%7B
 %0A%09%09
+%09
 for 
@@ -2517,16 +2517,17 @@
 i) %7B%0A%09%09%09
+%09
 items%5Bi%5D
@@ -2539,16 +2539,17 @@
 w Item(%0A
+%09
 %09%09%09%09next
@@ -2565,18 +2565,25 @@
 ndom
+.get()
 , 1, 
-10
+25
 ),%0A
+%09
 %09%09%09%09
@@ -2599,16 +2599,22 @@
 e(random
+.get()
 , 1, 15)
@@ -2617,19 +2617,25 @@
  15)%0A%09%09%09
+%09
 );%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09r

Improve Knapsack example.