/*
 * 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.tool.problem;

import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.Genotype;
import org.jenetics.engine.Codec;
import org.jenetics.engine.Problem;
import org.jenetics.util.ISeq;

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

	Problem<ISeq<BitGene>, BitGene, Integer> ONES_COUNTING =
		Problem.of(
			// Function<ISeq<BitGene>, Integer>
			genes -> (int)genes.stream().filter(BitGene::getBit).count(),
			Codec.of(
				// Factory<Genotype<BitGene>>
				Genotype.of(BitChromosome.of(20, 0.15)),
				// Function<Genotype<BitGene>, <BitGene>>
				gt -> gt.getChromosome().toSeq()
			)
		);

}

@@ -770,16 +770,53 @@
 oblem;%0A%0A
+import java.util.function.Function;%0A%0A
 import o
@@ -935,16 +935,95 @@
 .Codec;%0A
+import org.jenetics.engine.Engine;%0Aimport org.jenetics.engine.EvolutionResult;%0A
 import o
@@ -1086,16 +1086,50 @@
 q;%0A%0A/**%0A
+ * Full Ones-Counting example.%0A *%0A
  * @auth
@@ -1216,17 +1216,17 @@
 rsion 3.
-5
+6
 %0A * @sin
@@ -1266,12 +1266,19 @@
 ing 
-%7B%0A%0A%09
+implements 
 Prob
@@ -1318,43 +1318,432 @@
 er%3E 
-ONES_COUNTING =%0A%09%09Problem.of(%0A%09%09%09//
+%7B%0A%0A%09private final int _length;%0A%09private final double _onesProbability;%0A%0A%09/**%0A%09 * Create a new Ones-Counting example with the given parameters.%0A%09 *%0A%09 * @param length the length of the ones-vector%0A%09 * @param onesProbability the probability of ones in the created vector%0A%09 */%0A%09public OnesCounting(final int length, final double onesProbability) %7B%0A%09%09_length = length;%0A%09%09_onesProbability = onesProbability;%0A%09%7D%0A%0A%09@Override%0A%09public
  Fun
@@ -1771,20 +1771,38 @@
 Integer%3E
-%0A%09%09%09
+ fitness() %7B%0A%09%09return 
 genes -%3E
@@ -1857,194 +1857,564 @@
 nt()
-,%0A%09%09%09Codec.of(%0A%09%09%09%09// Factory%3CGenotype%3CBitGene%3E%3E%0A%09%09%09%09Genotype.of(BitChromosome.of(20, 0.15)),%0A%09%09%09%09// F
+;%0A%09%7D%0A%0A%09@Override%0A%09public Codec%3CISeq%3CBitGene%3E, BitGene%3E codec() %7B%0A%09%09return Codec.of(%0A%09%09%09Genotype.of(BitChromosome.of(_length, _onesProbability)),%0A%09%09%09gt -%3E gt.getChromosome().toSeq()%0A%09%09);%0A%09%7D%0A%0A%09public static void main(final String%5B%5D args) %7B%0A%09%09final OnesCo
 un
-c
 ti
-on%3CGenotype%3CBitGene%3E, %3CBitGene%3E%3E%0A%09%09%09%09gt -%3E gt.getChromosome().toSeq
+ng problem = new OnesCounting(15, 0.13);%0A%09%09final Engine%3CBitGene, Integer%3E engine = Engine.builder(problem).build();%0A%0A%09%09final ISeq%3CBitGene%3E result = problem.codec().decoder().apply(%0A%09%09%09engine.stream()%0A%09%09%09%09.limit(10)%0A%09%09%09%09.collect(EvolutionResult.toBestGenotype
 ()
+)
 %0A%09%09
-%09)%0A%09%09);
+);%0A%0A%09%09System.out.println(result);%0A%09%7D
 %0A%0A%7D%0A

Improve 'OnesCounting' example.