<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;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javolution.context.ConcurrentContext;
import javolution.context.LocalContext;
import jsr166y.ForkJoinPool;

import org.jenetics.util.ConcurrentEvaluator;
import org.jenetics.util.Evaluator;
import org.jenetics.util.EvaluatorRegistry;
import org.jenetics.util.Factory;
import org.jenetics.util.ForkJoinEvaluator;
import org.jenetics.util.RandomRegistry;
import org.jenetics.util.SerialEvaluator;
import org.jenetics.util.ThreadedEvaluator;
import org.jscience.mathematics.number.Float64;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class GeneticAlgorithmTest {

	private static class FF implements FitnessFunction<Float64Gene, Float64> {
		private static final long serialVersionUID = 618089611921083000L;

		@Override
		public Float64 evaluate(final Genotype<Float64Gene> genotype) {
			return genotype.getGene().getAllele();
		}
	}
	
	@Test
	public void setGetAlterer() {
		final GeneticAlgorithm<Float64Gene, Float64> ga = 
			new GeneticAlgorithm<Float64Gene, Float64>(
					Genotype.valueOf(new Float64Chromosome(0, 1)), 
					new FF()
				);
		
		final Alterer<Float64Gene> alterer = new Mutator<Float64Gene>();
		ga.setAlterer(alterer);
		Assert.assertSame(ga.getAlterer(), alterer);
		
		ga.addAlterer(new MeanAlterer<Float64Gene>());
		Assert.assertNotSame(ga.getAlterer(), alterer);
		Assert.assertTrue(ga.getAlterer() instanceof CompositeAlterer<?>);
		Assert.assertEquals(((CompositeAlterer<?>)ga.getAlterer()).getAlterers().length(), 2);
		
		ga.addAlterer(new SwapMutator<Float64Gene>());
		Assert.assertTrue(ga.getAlterer() instanceof CompositeAlterer<?>);
		Assert.assertEquals(((CompositeAlterer<?>)ga.getAlterer()).getAlterers().length(), 3);
	}
	
	@Test
	public void optimize() {
		final int concurrency = ConcurrentContext.getConcurrency();
		ConcurrentContext.setConcurrency(0);
		LocalContext.enter();
		try {
			RandomRegistry.setRandom(new Random(12345));
			EvaluatorRegistry.setEvaluator(new SerialEvaluator());
			
			final Factory<Genotype<Float64Gene>> factory = Genotype.valueOf(new Float64Chromosome(0, 1));
			final FitnessFunction<Float64Gene, Float64> ff = new FF();
			
			final GeneticAlgorithm<Float64Gene, Float64> ga = GeneticAlgorithm.valueOf(factory, ff);
			ga.setPopulationSize(200);
			ga.setAlterer(new MeanAlterer<Float64Gene>());
			ga.setOffspringFraction(0.3);
			ga.setOffspringSelector(new RouletteWheelSelector<Float64Gene, Float64>());
			ga.setSurvivorSelector(new TournamentSelector<Float64Gene, Float64>());
			
			ga.setup();
			ga.evolve(100);
			
			Statistics<?, ?> s = ga.getBestStatistics();
			Reporter.log(s.toString());
			Assert.assertEquals(s.getAgeMean(), 0.0);
			Assert.assertEquals(s.getAgeVariance(), 0.0);
			Assert.assertEquals(s.getSamples(), 200);
			Assert.assertEquals(((Float64)s.getBestFitness()).doubleValue(), 0.99577149482586247, 0.00000001);
			Assert.assertEquals(((Float64)s.getWorstFitness()).doubleValue(), 0.0014983949586988565, 0.00000001);
			
			s = ga.getStatistics();
			Reporter.log(s.toString());
			Assert.assertEquals(s.getAgeMean(), 16.070000000000004, 0.000001);
			Assert.assertEquals(s.getAgeVariance(), 241.40211055276382, 0.000001);
			Assert.assertEquals(s.getSamples(), 200);
			Assert.assertEquals(((Float64)s.getBestFitness()).doubleValue(), 0.99577149482586247, 0.00000001);
			Assert.assertEquals(((Float64)s.getWorstFitness()).doubleValue(), 0.2861803193531154, 0.00000001);
		} finally {
			ConcurrentContext.setConcurrency(concurrency);
			LocalContext.exit();
		}
		
	}
	
	@Test(invocationCount = 10)
	public void evolveForkJoinPool() {
		Evaluator evaluator = EvaluatorRegistry.getEvaluator();
		final ForkJoinPool pool = new ForkJoinPool(10);
		EvaluatorRegistry.setEvaluator(new ForkJoinEvaluator(pool));
		
		try {
			final Factory<Genotype<Float64Gene>> factory = Genotype.valueOf(new Float64Chromosome(-1, 1));
			final FitnessFunction<Float64Gene, Float64> ff = new FF();
			
			final GeneticAlgorithm<Float64Gene, Float64> ga = GeneticAlgorithm.valueOf(factory, ff);
			ga.setPopulationSize(1000);
			ga.setAlterer(new MeanAlterer<Float64Gene>());<commit_msg>Incomplete - # 13: Code and test improvements https://sourceforge.net/apps/trac/jenetics/ticket/13<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;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javolution.context.ConcurrentContext;
import javolution.context.LocalContext;
import jsr166y.ForkJoinPool;

import org.jenetics.util.ConcurrentEvaluator;
import org.jenetics.util.Evaluator;
import org.jenetics.util.EvaluatorRegistry;
import org.jenetics.util.Factory;
import org.jenetics.util.ForkJoinEvaluator;
import org.jenetics.util.RandomRegistry;
import org.jenetics.util.SerialEvaluator;
import org.jenetics.util.ThreadedEvaluator;
import org.jscience.mathematics.number.Float64;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class GeneticAlgorithmTest {

	private static class FF implements FitnessFunction<Float64Gene, Float64> {
		private static final long serialVersionUID = 618089611921083000L;

		@Override
		public Float64 evaluate(final Genotype<Float64Gene> genotype) {
			return genotype.getGene().getAllele();
		}
	}
	
	@Test
	public void setGetAlterer() {
		final GeneticAlgorithm<Float64Gene, Float64> ga = 
			new GeneticAlgorithm<Float64Gene, Float64>(
					Genotype.valueOf(new Float64Chromosome(0, 1)), 
					new FF()
				);
		
		final Alterer<Float64Gene> alterer = new Mutator<Float64Gene>();
		ga.setAlterer(alterer);
		Assert.assertSame(ga.getAlterer(), alterer);
		
		ga.addAlterer(new MeanAlterer<Float64Gene>());
		Assert.assertNotSame(ga.getAlterer(), alterer);
		Assert.assertTrue(ga.getAlterer() instanceof CompositeAlterer<?>);
		Assert.assertEquals(((CompositeAlterer<?>)ga.getAlterer()).getAlterers().length(), 2);
		
		ga.addAlterer(new SwapMutator<Float64Gene>());
		Assert.assertTrue(ga.getAlterer() instanceof CompositeAlterer<?>);
		Assert.assertEquals(((CompositeAlterer<?>)ga.getAlterer()).getAlterers().length(), 3);
	}
	
	@Test
	public void optimize() {
		final int concurrency = ConcurrentContext.getConcurrency();
		ConcurrentContext.setConcurrency(0);
		LocalContext.enter();
		try {
			RandomRegistry.setRandom(new Random(12345));
			EvaluatorRegistry.setEvaluator(new SerialEvaluator());
			
			final Factory<Genotype<Float64Gene>> factory = Genotype.valueOf(new Float64Chromosome(0, 1));
			final FitnessFunction<Float64Gene, Float64> ff = new FF();
			
			final GeneticAlgorithm<Float64Gene, Float64> ga = GeneticAlgorithm.valueOf(factory, ff);
			ga.setPopulationSize(200);
			ga.setAlterer(new MeanAlterer<Float64Gene>());
			ga.setOffspringFraction(0.3);
			ga.setOffspringSelector(new RouletteWheelSelector<Float64Gene, Float64>());
			ga.setSurvivorSelector(new TournamentSelector<Float64Gene, Float64>());
			
			ga.setup();
			ga.evolve(100);
			
			Statistics<?, ?> s = ga.getBestStatistics();
			Reporter.log(s.toString());
			Assert.assertEquals(s.getAgeMean(), 0.0);
			Assert.assertEquals(s.getAgeVariance(), 0.0);
			Assert.assertEquals(s.getSamples(), 200);
			Assert.assertEquals(((Float64)s.getBestFitness()).doubleValue(), 0.9846666139422408, 0.00000001);
			Assert.assertEquals(((Float64)s.getWorstFitness()).doubleValue(), 0.0014983949586988565, 0.00000001);
			
			s = ga.getStatistics();
			Reporter.log(s.toString());
			Assert.assertEquals(s.getAgeMean(), 9.600000000000009, 0.000001);
			Assert.assertEquals(s.getAgeVariance(), 19.65829145728642, 0.000001);
			Assert.assertEquals(s.getSamples(), 200);
			Assert.assertEquals(((Float64)s.getBestFitness()).doubleValue(), 0.9807764890123456, 0.00000001);
			Assert.assertEquals(((Float64)s.getWorstFitness()).doubleValue(), 0.9807764890123456, 0.00000001);
		} finally {
			ConcurrentContext.setConcurrency(concurrency);
			LocalContext.exit();
		}
		
	}
	
	@Test(invocationCount = 10)
	public void evolveForkJoinPool() {
		Evaluator evaluator = EvaluatorRegistry.getEvaluator();
		final ForkJoinPool pool = new ForkJoinPool(10);
		EvaluatorRegistry.setEvaluator(new ForkJoinEvaluator(pool));
		
		try {
			final Factory<Genotype<Float64Gene>> factory = Genotype.valueOf(new Float64Chromosome(-1, 1));
			final FitnessFunction<Float64Gene, Float64> ff = new FF();
			
			final GeneticAlgorithm<Float64Gene, Float64> ga = GeneticAlgorithm.valueOf(factory, ff);
			ga.setPopulationSize(1000);
			ga.setAlterer(new MeanAlterer<Float64Gene>());
{'fraction_non_alphanumeric': 0.12378158417715529, 'fraction_numerical': 0.057348348632535254, 'mean_word_length': 5.2680901542111505, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 10, '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': '4224975', 'n_tokens_mistral': 4095, 'n_tokens_neox': 3299, 'n_words': 713}