package ac.essex.gp;

import ac.essex.gp.interfaces.console.ConsoleListener;
import ac.essex.gp.problems.Problem;
import ac.essex.gp.params.GPParams;
import ac.essex.ooechs.imaging.commons.StatisticsSolver;

import java.util.Vector;
import java.text.DecimalFormat;

/**
 *
 * <p>
 * Allows you to run the same problem several times so that you can get statistics
 * </p>
 *
 *
 * @author Olly Oechsle, University of Essex, Date: 30-Jan-2007
 * @version 1.0
 */
public class EvolveBatch {

    public static final int NUM_RUNS = 10;
    public static final int NUM_GENERATIONS = 200;

    public static void main(String[] args) throws Exception {
        //Problem p = new JasmineDecisionTreeProblem(new File("/home/ooechs/Desktop/JasmineProjects/ANPR.jasmine"));
        //new EvolveBath.run(p);
    }

    public void run(Problem p) {

        System.out.println("--------------------------------------------------------------");
        System.out.println("sxGP EVOLVE MULTIRUN v1.01");
        System.out.println("Hits Mean, Hits Std Dev, Size Mean, Time Mean");

        //for (double pXover = 0.6; pXover >= 0; pXover -= 0.1)
        //for (double pMutate = 0.6; pMutate >= 0; pMutate -= 0.1) {

        Vector<ConsoleListener> interfaces = new Vector<ConsoleListener>(NUM_RUNS);

        StatisticsSolver hits = new StatisticsSolver(NUM_RUNS);
        StatisticsSolver size = new StatisticsSolver(NUM_RUNS);
        StatisticsSolver time = new StatisticsSolver(NUM_RUNS);

        for (int i = 0; i < NUM_RUNS; i++) {

            ConsoleListener in = new ac.essex.gp.interfaces.console.ConsoleListener(ac.essex.gp.interfaces.console.ConsoleListener.SILENT);

            interfaces.add(in);

            GPParams params = new GPParams();
            params.setGenerations(NUM_GENERATIONS);
            //params.setCrossoverProbability(pXover);
            //params.setPointMutationProbability(pMutate);

            Evolve e = new Evolve(p, in, params);

            //System.out.println("RUN " + i + " of " + NUM_RUNS);
            System.out.print(".");

            e.run();

            hits.addData(in.getBestIndividual().getHits());
            size.addData(in.getBestIndividual().getTreeSize());
            time.addData(in.getTotalTime());


        }

        //System.out.println("\nMULTIRUN RESULTS for problem");
        //System.out.println("CROSSOVER: " + pXover);
        //System.out.println("MUTATION: " + pMutate);
        DecimalFormat f = new DecimalFormat("0.0");
        System.out.println("\nRun, Hits, Size, Total Time");

        for (int i = 0; i < interfaces.size(); i++) {
            ConsoleListener consoleInterface = interfaces.elementAt(i);
            System.out.print(i);
            System.out.print(", " + consoleInterface.getBestIndividual().getHits());
            System.out.print(", " + consoleInterface.getBestIndividual().getTreeSize());
            System.out.print(", " + consoleInterface.getTotalTime() + "\n");
        }
        System.out.println();
        //System.out.print(f.format(pXover));
        //System.out.print(", " + f.format(pMutate));
        System.out.println("\nAvg Hits, Hits Std Dev, Avg Size, Avg Time");
        System.out.print(", " + f.format(hits.getMean()));
        System.out.print(", " + f.format(hits.getStandardDeviation()));
        System.out.print(", " + f.format(size.getMean()));
        System.out.print(", " + f.format(time.getMean()) + "\n");

        //}

    }


}
