/*
 * 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 java.awt.Point;
import java.util.stream.IntStream;

import org.jenetics.EnumGene;
import org.jenetics.Optimize;
import org.jenetics.engine.Codec;
import org.jenetics.engine.Engine;
import org.jenetics.engine.EvolutionResult;
import org.jenetics.engine.codecs;
import org.jenetics.util.ISeq;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version 3.6
 * @since 3.6
 */
public class TSM {
	// The locations to visit.
	static final ISeq<Point> POINTS = ISeq.of(
		new Point(0, 0), new Point(1, 2), new Point(4, 5) // ...
	);

	// The permutation codec.
	static final Codec<ISeq<Point>, EnumGene<Point>> CODEC =
		codecs.ofPermutation(POINTS);

	// The fitness function (in the problem domain).
	static double dist(final ISeq<Point> p) {
		return IntStream.range(0, p.length())
			.mapToDouble(i -> p.get(i).distance(p.get(i + i%p.length())))
			.sum();
	}

	// The evolution engine.
	static final Engine<EnumGene<Point>, Double> ENGINE = Engine
		.builder(TSM::dist, CODEC)
		.optimize(Optimize.MINIMUM)
		.build();

	// Find the solution.
	public static void main(final String[] args) {
		final ISeq<Point> result = CODEC.decoder().apply(
			ENGINE.stream()
				.limit(10)
				.collect(EvolutionResult.toBestGenotype())
		);

		System.out.println(result);
	}
}

@@ -1939,34 +1939,8 @@
 t = 
-CODEC.decoder().apply(%0A%09%09%09
 ENGI
@@ -1954,17 +1954,16 @@
 am()%0A%09%09%09
-%09
 .limit(1
@@ -1965,17 +1965,16 @@
 mit(10)%0A
-%09
 %09%09%09.coll
@@ -2003,22 +2003,21 @@
 Best
-Genotype())%0A%09%09
+Result(CODEC)
 );%0A%0A

Improve TSM example.