/*
 * 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.engine.EvolutionResult.toBestPhenotype;

import java.util.stream.IntStream;

import org.jenetics.CharacterChromosome;
import org.jenetics.CharacterGene;
import org.jenetics.Genotype;
import org.jenetics.Mutator;
import org.jenetics.Phenotype;
import org.jenetics.SinglePointCrossover;
import org.jenetics.StochasticUniversalSelector;
import org.jenetics.TournamentSelector;
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 1.0
 * @version 3.5
 */
public class StringGenerator {

	private static final String TARGET_STRING = "jenetics";

	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(StringGenerator::evaluate, gtf)
			.populationSize(500)
			.survivorsSelector(new StochasticUniversalSelector<>())
			.offspringSelector(new TournamentSelector<>(5))
			.alterers(
				new Mutator<>(0.1),
				new SinglePointCrossover<>(0.5))
			.build();

		final Phenotype<CharacterGene, Integer> result = engine.stream()
			.limit(100)
			.collect(toBestPhenotype());

		System.out.println(result);
	}

}

@@ -1158,32 +1158,66 @@
 namentSelector;%0A
+import org.jenetics.engine.Codec;%0A
 import org.jenet
@@ -1259,20 +1259,22 @@
 ics.
-util.CharSeq
+engine.Problem
 ;%0Aim
@@ -1296,23 +1296,23 @@
 cs.util.
-Factory
+CharSeq
 ;%0A%0A/**%0A 
@@ -1536,134 +1536,93 @@
 tic 
-Integer evaluate(final Genotype%3CCharacterGene%3E gt) %7B%0A%09%09final CharSequence source = (CharSequence)gt.getChromosome();%0A%0A%09%09return
+final Problem%3CCharSequence, CharacterGene, Integer%3E PROBLEM =%0A%09%09Problem.of(%0A%09%09%09seq -%3E
  Int
@@ -1664,16 +1664,17 @@
 h())%0A%09%09%09
+%09
 .map(i -
@@ -1680,13 +1680,10 @@
 -%3E s
-ourc
 e
+q
 .cha
@@ -1732,169 +1732,34 @@
 %0A%09%09%09
+%09
 .sum()
-;%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) throws Exception %7B%0A%09%09final CharSeq chars = CharSeq.of(%22a-z%22);%0A%09%09final Factory%3CGenotype%3CCharacterGene%3E%3E gtf = 
+,%0A%09%09%09Codec.of(%0A%09%09%09%09
 Geno
@@ -1766,20 +1766,16 @@
 type.of(
-%0A%09%09%09
 new Char
@@ -1794,13 +1794,31 @@
 ome(
-chars
+%0A%09%09%09%09%09CharSeq.of(%22a-z%22)
 , TA
@@ -1841,15 +1841,118 @@
 th()
-)%0A%09%09);%0A
+%0A%09%09%09%09)),%0A%09%09%09%09gt -%3E (CharSequence)gt.getChromosome()%0A%09%09%09)%0A%09%09);%0A%0A%09public static void main(final String%5B%5D args) %7B
 %0A%09%09f
@@ -2002,20 +2002,16 @@
 = Engine
-%0A%09%09%09
 .builder
@@ -2015,38 +2015,15 @@
 der(
-StringGenerator::evaluate, gtf
+PROBLEM
 )%0A%09%09

Improve example code.