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

package lt.ktu.gmj.methods;

import drm.genetics.simple.SimpleRunner;
import java.util.TreeSet;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.*;

// Referenced classes of package lt.ktu.gmj.methods:
//			MethodGATask, MethodGAIndividual

public class MethodGA extends AbstractMethod
{

	protected int iterations;
	public double minValue;
	public double mutation;
	public double crossover;
	public double elites;
	public int maxIterations;
	public int indCount;
	public int mutationType;
	public double percentMutating;
	public int mutationLevel;
	public int crossoverType;
	public int crossoverStyle;
	public int crossoverLevel;

	public MethodGA()
	{
		iterations = 0;
		minValue = (-1.0D / 0.0D);
		mutation = 0.29999999999999999D;
		crossover = 0.5D;
		elites = 0.01D;
		maxIterations = 100;
		indCount = 10;
		mutationType = 0;
		percentMutating = 0.050000000000000003D;
		mutationLevel = 2;
		crossoverType = 0;
		crossoverStyle = 0;
		crossoverLevel = 2;
	}

	public int iterations()
	{
		return iterations;
	}

	public void customize(PropertyManager manager)
	{
		manager.add(new IntProperty("Iterations", new FieldProvider(this, "maxIterations")));
		manager.add(new DoubleProperty("Stop if <=", new FieldProvider(this, "minValue")));
		manager.add(new IntProperty("Individuals", new FieldProvider(this, "indCount")));
		manager.add(new DoubleProperty("Mutation prob.", new FieldProvider(this, "mutation")));
		manager.add(new DoubleProperty("Crossover prob.", new FieldProvider(this, "crossover")));
		manager.add(new DoubleProperty("Elites perc.", new FieldProvider(this, "elites")));
		manager.add(new ChoiceProperty("Mutation type:", new FieldProvider(this, "mutationType"), new String[] {
			"Single gene mutation", "Simple partial mutation", "Searching best mutation"
		}));
		manager.add(new DoubleProperty("Mutating part", new FieldProvider(this, "percentMutating")));
		manager.add(new IntProperty("Mutation best search level", new FieldProvider(this, "mutationLevel")));
		manager.add(new ChoiceProperty("Crossover type:", new FieldProvider(this, "crossoverType"), new String[] {
			"Simple crossover", "Searching best crossover"
		}));
		manager.add(new ChoiceProperty("Crossover style:", new FieldProvider(this, "crossoverStyle"), new String[] {
			"Single point crossover", "Two points crossover", "Random crossover"
		}));
		manager.add(new IntProperty("Crossover best search level", new FieldProvider(this, "crossoverLevel")));
		manager.add(new Header("", "Mantas Balnys, KTU"));
	}

	public Result run(ResultLogger log, Task task)
	{
		SimpleRunner runner = new SimpleRunner();
		MethodGATask taskGA = new MethodGATask(task, indCount, mutationType, percentMutating, mutationLevel, crossoverType, crossoverStyle, crossoverLevel);
		taskGA.indCount = indCount;
		runner.init(taskGA);
		runner.setMutationProbability(mutation);
		runner.setCrossoverProbability(crossover);
		runner.setBestPassPercentage(elites);
		Result result = null;
		boolean testi = true;
		for(iterations = 0; iterations < maxIterations && testi; iterations++)
		{
			runner.step();
			MethodGAIndividual best = (MethodGAIndividual)((TreeSet)runner.accessIndivids()).first();
			result = new Result(runner.getAge(), best.getPoint(), new SimpleResult(-best.getWeight()));
			if(result.value.fx() <= minValue)
				testi = false;
			log.log(result);
		}

		return result;
	}
}
