// 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:   SimpleRunner.java

package drm.genetics.simple;

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

public class SimpleRunner extends AbstractRunner
{

	protected double mutationProbability;
	protected double crossoverProbability;
	protected double bestPassPercentage;
	public boolean DEBUG_BREEDER;

	public SimpleRunner()
	{
		mutationProbability = 0.10000000000000001D;
		crossoverProbability = 0.5D;
		bestPassPercentage = 0.10000000000000001D;
		DEBUG_BREEDER = false;
	}

	public void setMutationProbability(double mutationProbability)
	{
		this.mutationProbability = mutationProbability;
	}

	public void setCrossoverProbability(double crossoverProbability)
	{
		this.crossoverProbability = 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
		{
			if(DEBUG_BREEDER)
				System.out.print("breed: ");
			int size = individuals.size();
			Vector newRase = new Vector(size);
			int howMuchBest = (int)Math.ceil(bestPassPercentage * (double)size);
			for(Iterator it = individuals.iterator(); it.hasNext() && newRase.size() < howMuchBest;)
			{
				Individual ind = (Individual)it.next();
				newRase.add(ind.copy());
				if(DEBUG_BREEDER)
					System.out.print("E");
			}

			while(newRase.size() < size) 
				if(individuals.size() > 1)
				{
					Individual ind1 = getRandomIndividual();
					Individual ind2 = getRandomIndividual();
					if(DEBUG_BREEDER)
						System.out.print("[");
					if(random.nextDouble() < crossoverProbability)
					{
						task.getCrossover().crossSame(ind1, ind2);
						if(DEBUG_BREEDER)
							System.out.print("C");
					}
					if(random.nextDouble() < mutationProbability)
					{
						task.getMutation().mutateSame(ind1);
						if(DEBUG_BREEDER)
							System.out.print("M");
					}
					if(random.nextDouble() < mutationProbability)
					{
						task.getMutation().mutateSame(ind2);
						if(DEBUG_BREEDER)
							System.out.print("M");
					}
					newRase.add(ind1);
					if(newRase.size() < size)
						newRase.add(ind2);
					if(DEBUG_BREEDER)
						System.out.print("]");
				}
			if(DEBUG_BREEDER)
				System.out.println();
			individuals.clear();
			individuals.addAll(newRase);
			stepCount++;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
