package org.jenetix;

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

import java.util.stream.IntStream;

import org.jenetics.CharacterChromosome;
import org.jenetics.CharacterGene;
import org.jenetics.Genotype;
import org.jenetics.Phenotype;
import org.jenetics.engine.Engine;
import org.jenetics.util.CharSeq;
import org.jenetics.util.Factory;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @since 3.5
 * @version 3.5
 */
public class WeaselProgram {

	private static final String TARGET_STRING = "methinks it is like a weasel";

	private static Integer evaluate(final Genotype<CharacterGene> gt) {
		final CharSequence source = (CharSequence)gt.getChromosome();

		return IntStream.range(0, TARGET_STRING.length())
			.map(i -> source.charAt(i) == TARGET_STRING.charAt(i) ? 1 : 0)
			.sum();
	}

	public static void main(String[] args) throws Exception {
		final CharSeq chars = CharSeq.of("a-z ");
		final Factory<Genotype<CharacterGene>> gtf = Genotype.of(
			new CharacterChromosome(chars, TARGET_STRING.length())
		);

		final Engine<CharacterGene, Integer> engine = Engine
			.builder(WeaselProgram::evaluate, gtf)
			.populationSize(200)
			.selector(new WeaselSelector<>())
			.offspringFraction(1)
			.alterers(new WeaselMutator<>(0.05))
			.build();

		final Phenotype<CharacterGene, Integer> result = engine.stream()
			.limit(50)
			.peek(r -> System.out.println(r.getBestPhenotype()))
			.collect(toBestPhenotype());

		System.out.println(result);
	}

}

@@ -549,47 +549,40 @@
 RGET
-_STRING = %22methinks it is like a weasel
+ = %22METHINKS IT IS LIKE A WEASEL
 %22;%0A%0A
@@ -601,23 +601,16 @@
 tic 
-Integer evaluat
+int scor
 e(fi
@@ -734,39 +734,32 @@
 .range(0, TARGET
-_STRING
 .length())%0A%09%09%09.m
@@ -792,23 +792,16 @@
 = TARGET
-_STRING
 .charAt(
@@ -926,11 +926,11 @@
 of(%22
-a-z
+A-Z
  %22);
@@ -1034,15 +1034,8 @@
 RGET
-_STRING
 .len
@@ -1133,15 +1133,12 @@
 am::
-evaluat
+scor
 e, g
@@ -1164,10 +1164,10 @@
 ize(
-20
+15
 0)%0A%09
@@ -1395,16 +1395,54 @@
 println(
+%0A%09%09%09%09r.getTotalGenerations() + %22: %22 + 
 r.getBes

Improve 'WeaselProgram' class.