<commit_before>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 * 	 <NAME> (<EMAIL>)
 * 	 
 */
package org.jenetics.examples;

import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.sin;

import java.io.Serializable;

import org.jenetics.Alterer;
import org.jenetics.Chromosome;
import org.jenetics.CompositeAlterer;
import org.jenetics.GeneticAlgorithm;
import org.jenetics.Genotype;
import org.jenetics.Integer64Gene;
import org.jenetics.Optimize;
import org.jenetics.PartiallyMatchedCrossover;
import org.jenetics.PermutationChromosome;
import org.jenetics.PermutationGene;
import org.jenetics.SwapMutator;
import org.jenetics.util.Factory;
import org.jenetics.util.Function;

/**
 * The classical <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem">TSP</a>.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME>ötter</a>
 * @version $Id$
 */
public class TravelingSalesman {
	
	private static class FF 
		implements Function<Genotype<PermutationGene<Integer>>, Double>,
					Serializable
	{
		private static final long serialVersionUID = 1L;
		
		private final double[][] _adjacence;
		
		public FF(final double[][] adjacence) {
			_adjacence = adjacence;
		}
		
		@Override
		public Double apply(final Genotype<PermutationGene<Integer>> genotype) {
			final Chromosome<PermutationGene<Integer>> path = genotype.getChromosome();
			
			double length = 0.0;
			for (int i = 0, n = path.length(); i < n; ++i) {
				final int from = path.getGene(i).getAllele();
				final int to = path.getGene((i + 1)%n).getAllele();
				length += _adjacence[from][to];
			}
			return length;
		}
		
		@Override
		public String toString() {
			return "Point distance";
		}
	}
	
	public static void main(String[] args) {
		final int stops = 20;
		
		final Function<Genotype<PermutationGene<Integer>>, Double> ff = new FF(adjacencyMatrix(stops));
		final Factory<Genotype<PermutationGene<Integer>>> gtf = Genotype.valueOf(
			PermutationChromosome.valueOf(stops)
		);
		final GeneticAlgorithm<PermutationGene<Integer>, Double> 
			ga = new GeneticAlgorithm<>(gtf, ff, Optimize.MINIMUM);
		ga.setPopulationSize(300);
		ga.setAlterer(new SwapMutator<PermutationGene<Integer>>(0.2));
		
		Alterer<PermutationGene<Integer>> a = new PartiallyMatchedCrossover<>(0.3);
		ga.addAlterer(a);
		
        //ga.setSelectors(new org.jenetics.MonteCarloSelector<IntegerGene, Double>());
        //ga.setAlterer(new org.jenetics.NullAlterer<IntegerGene>());
        
		final int generations = 500;
		
		GAUtils.printConfig(
				"Traveling salesman", 
				ga, 
				generations, 
				((CompositeAlterer<?>)ga.getAlterer()).getAlterers().toArray()
			);
		
		GAUtils.execute(ga, generations, 50);
        
//		try {
//			XMLSerializer.write(ga.getPopulation(), new FileOutputStream("/home/franzw/population.xml"));
//			ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream("/home/franzw/population.obj"));
//			oout.writeObject(ga.getPopulation());
//			oout.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
	}
	
	/**
	 * All points in the created adjacency matrix lie on a circle. So it is easy 
	 * to check the quality of the solution found by the GA.
	 */
	private static double[][] adjacencyMatrix(int stops) {<commit_msg>Fix TravelingSalesman example.<commit_after>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 * 	 <NAME> (<EMAIL>)
 * 	 
 */
package org.jenetics.examples;

import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.sin;

import java.io.Serializable;

import org.jenetics.Chromosome;
import org.jenetics.CompositeAlterer;
import org.jenetics.GeneticAlgorithm;
import org.jenetics.Genotype;
import org.jenetics.Optimize;
import org.jenetics.PartiallyMatchedCrossover;
import org.jenetics.PermutationChromosome;
import org.jenetics.PermutationGene;
import org.jenetics.SwapMutator;
import org.jenetics.util.Factory;
import org.jenetics.util.Function;

/**
 * The classical <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem">TSP</a>.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class TravelingSalesman {
	
	private static class FF 
		implements Function<Genotype<PermutationGene<Integer>>, Double>,
					Serializable
	{
		private static final long serialVersionUID = 1L;
		
		private final double[][] _adjacence;
		
		public FF(final double[][] adjacence) {
			_adjacence = adjacence;
		}
		
		@Override
		public Double apply(final Genotype<PermutationGene<Integer>> genotype) {
			final Chromosome<PermutationGene<Integer>> path = genotype.getChromosome();
			
			double length = 0.0;
			for (int i = 0, n = path.length(); i < n; ++i) {
				final int from = path.getGene(i).getAllele();
				final int to = path.getGene((i + 1)%n).getAllele();
				length += _adjacence[from][to];
			}
			return length;
		}
		
		@Override
		public String toString() {
			return "Point distance";
		}
	}
	
	public static void main(String[] args) {
		final int stops = 20;
		
		final Function<Genotype<PermutationGene<Integer>>, Double> ff = new FF(adjacencyMatrix(stops));
		final Factory<Genotype<PermutationGene<Integer>>> gtf = Genotype.valueOf(
			PermutationChromosome.valueOf(stops)
		);
		final GeneticAlgorithm<PermutationGene<Integer>, Double> 
			ga = new GeneticAlgorithm<>(gtf, ff, Optimize.MINIMUM);
		ga.setPopulationSize(300);
		ga.setAlterer(new SwapMutator<PermutationGene<Integer>>(0.2));
		ga.addAlterer(new PartiallyMatchedCrossover<Integer>(0.3));
        
		final int generations = 500;
		
		GAUtils.printConfig(
				"Traveling salesman", 
				ga, 
				generations, 
				((CompositeAlterer<?>)ga.getAlterer()).getAlterers().toArray()
			);
		
		GAUtils.execute(ga, generations, 50);
        
//		try {
//			XMLSerializer.write(ga.getPopulation(), new FileOutputStream("/home/franzw/population.xml"));
//			ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream("/home/franzw/population.obj"));
//			oout.writeObject(ga.getPopulation());
//			oout.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
	}
	
	/**
	 * All points in the created adjacency matrix lie on a circle. So it is easy 
	 * to check the quality of the solution found by the GA.
	 */
	private static double[][] adjacencyMatrix(int stops) {
{'fraction_non_alphanumeric': 0.11925560197493354, 'fraction_numerical': 0.008355488036460312, 'mean_word_length': 4.067350865939705, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6742343', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2426, 'n_words': 682}