package src.strategies;

import src.individuals.GAIndividual;

public class HillClimbing {
	/** The length of the genotype of the individuals **/
	private int m_GenotypeLength;
	/** The number of single optimization steps to be evaluated **/
	private int m_OptimizationSteps;
	/** The number of times the experiment is repeated **/
	private int m_MultiRuns;
	/** Number of steps needed to reach best result **/
	private int m_OptimizationStepsNeeded = 0;
	/** Current best individual **/
	private GAIndividual m_Best;

	/**
	 * This constructor sets up HillClimber
	 * 
	 * @param genotypeLength
	 *            The length of the genotype of the individuals
	 * @param optimizationSteps
	 *            The number of single optimization steps to be evaluated
	 *            (adjust as necessary)
	 * @param multiRuns
	 *            The number of times the experiment is repeated (at least 10)
	 */
	public HillClimbing(int genotypeLength, int optimizationSteps, int multiRuns) {
		m_OptimizationSteps = optimizationSteps;
		m_MultiRuns = multiRuns;
		m_GenotypeLength = genotypeLength;
	}

	/**
	 * This method will initialize the HillClimber
	 */
	public void initialize() {
		m_Best = new GAIndividual(m_GenotypeLength);
		m_OptimizationStepsNeeded = 0;
	}

	/**
	 * This method will optimize the defaultEvaulateAsMiniBits Problem. Use
	 * m_FitnessCallsNeeded to return the number of FitnessCalls (e.g., calling
	 * evaluateAsMaxiBits()) needed to find the optimum. The optimization should
	 * terminate after m_FitnessCalls.
	 */
	public void optimize() {
		for (int i = 0; i < m_OptimizationSteps; i++) {
			GAIndividual mutated = (GAIndividual) m_Best.clone();
			mutated.mutate();
			m_Best = (m_Best.evaluateAsMaxiBits() <= mutated
					.evaluateAsMaxiBits()) ? m_Best : mutated;

			if (m_Best.evaluateAsMaxiBits() != 0) {
				m_OptimizationStepsNeeded++;
			}
		}
	}

	/**
	 * This main method will start a simple hill climber. No arguments
	 * necessary.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO: parameters for the HillClimber, adjust these values as
		// necessary
		int genotypeLength = 50;
		int optimizationSteps = 1000;
		int multiRuns = 1000;
		HillClimbing program = new HillClimbing(genotypeLength,
				optimizationSteps, multiRuns);
		int TmpMeanCalls = 0, TmpMeanFitness = 0;
		// perform repeated optimization
		for (int i = 0; i < program.m_MultiRuns; i++) {
			program.initialize();
			program.optimize();
			TmpMeanCalls += program.m_OptimizationStepsNeeded;
			TmpMeanFitness += program.m_Best.evaluateAsMaxiBits();
		}
		TmpMeanCalls = TmpMeanCalls / program.m_MultiRuns;
		TmpMeanFitness = TmpMeanFitness / program.m_MultiRuns;
		System.out.println("(" + program.m_MultiRuns + "/"
				+ program.m_OptimizationSteps + ") Mean Fitness : "
				+ TmpMeanFitness + " Mean Calls needed: " + TmpMeanCalls);
	}
}
