package ac.essex.gp;

import ac.essex.gp.interfaces.ConsoleInterface;
import ac.essex.gp.problems.JasmineSegmentationProblem;
import ac.essex.gp.problems.Problem;
import ac.essex.gp.problems.ShapeClassificationProblem;
import ac.essex.gp.params.GPParams;
import ac.essex.ooechs.imaging.commons.StatisticsSolver;

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

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </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 {

        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<ConsoleInterface> interfaces = new Vector<ConsoleInterface>(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++) {

            ConsoleInterface in = new ConsoleInterface(ConsoleInterface.SILENT);

            interfaces.add(in);

            Problem p = new ShapeClassificationProblem(new File("/home/ooechs/Desktop/JasmineProjects/ANPR.jasmine"));

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

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

            //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++) {
            ConsoleInterface 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");

        //}

    }


}
