// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   SymetricCMRunner.java

package drm.genetics.simple;

import drm.genetics.*;
import java.util.*;

public class SymetricCMRunner extends AbstractRunner
{

	protected double mutationProbability;
	protected double crossoverProbability;
	protected double bestPassPercentage;

	public SymetricCMRunner()
	{
		mutationProbability = 0.10000000000000001D;
		crossoverProbability = 0.90000000000000002D;
		bestPassPercentage = 0.10000000000000001D;
	}

	public void setMutationProbability(double mutationProbability)
	{
		this.mutationProbability = mutationProbability;
		crossoverProbability = 1.0D - mutationProbability;
	}

	public void setCrossoverProbability(double crossoverProbability)
	{
		setMutationProbability(1.0D - crossoverProbability);
	}

	public double getCrossoverProbability()
	{
		return crossoverProbability;
	}

	public double getMutationProbability()
	{
		return mutationProbability;
	}

	public void setBestPassPercentage(double bestPassPercentage)
	{
		this.bestPassPercentage = bestPassPercentage;
	}

	public double getBestPassPercentage()
	{
		return bestPassPercentage;
	}

	public void step()
	{
		try
		{
			int size = individuals.size();
			Vector newRase = new Vector(size);
			int howMuchBest = (int)Math.ceil(bestPassPercentage * (double)size);
			Individual ind2;
			for(Iterator it = individuals.iterator(); it.hasNext() && newRase.size() < howMuchBest; newRase.add(ind2))
			{
				Individual ind = (Individual)it.next();
				ind2 = (Individual)ind.getClass().newInstance();
				ind2.accessGenome(ind.getGenome());
			}

			while(newRase.size() < size) 
				if(individuals.size() <= 1 || random.nextDouble() < mutationProbability)
				{
					Individual ind = getRandomIndividual();
					task.getMutation().mutateSame(ind);
					newRase.add(ind);
				} else
				{
					Individual ind1 = getRandomIndividual();
					//Individual ind2 = getRandomIndividual();
					ind2 = getRandomIndividual();
					task.getCrossover().crossSame(ind1, ind2);
					newRase.add(ind1);
					if(newRase.size() < size)
						newRase.add(ind2);
				}
			individuals.clear();
			individuals.addAll(newRase);
			stepCount++;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
