package ac.essex.gp.ooechs.detection.evolved;

import ac.essex.ooechs.imaging.commons.HaarRegions;
import ac.essex.gp.ooechs.detection.gp.DetectionProblem;
import ac.essex.gp.ooechs.detection.gp.HaarTrainingSet;
import ac.essex.gp.multiclass.PCM;
import ac.essex.gp.multiclass.BetterDRS;

/**
 * <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: 22-Apr-2008
 * @version 1.0
 */
public class EvolvedFaceDetector {

PCM pcm = new BetterDRS(-4.0,951.6381225585938,new int[]{2,-1,-1,1,1,1,-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1,1,1,-1,1});;
public double calculate(HaarRegions image) {
    double node1 = image.getOneRectangleFeature(0, 24, 21, 19);
    double node12 = 3.0 + 0;
    double node15 = 5.0 + 0;
    double node11 = node12 - node15;
    double node9 = 3.0 + node11;
    double node19 = 0 + 2.0;
    double node18 = node19 + 0;
    double node8 = node9 * node18;
    double node6 = 2.0 * node8;
    System.out.println(node1 * node6);
    return pcm.getClassFromOutput(node1 * node6);
}

    public void test(DetectionProblem learner) {

        int correct = 0, mistakes = 0, total = 0;
        int returnedTrue = 0, returnedFalse = 0;

        for (int i = 0; i < learner.trainingData.size(); i++) {
            HaarTrainingSet haarTrainingSet = learner.trainingData.elementAt(i);

            if (haarTrainingSet.classID == DetectionProblem.OBJECT1) {
                haarTrainingSet.image.makeWindowFillImage(DetectionProblem.WINDOWBLOCKSX, DetectionProblem.WINDOWBLOCKSY);
            } else {
                int x = (int) (Math.random() * (haarTrainingSet.image.getWidth() - DetectionProblem.WINDOW_WIDTH));
                int y = (int) (Math.random() * (haarTrainingSet.image.getHeight() - DetectionProblem.WINDOW_HEIGHT));
                haarTrainingSet.image.setWindowPosition(x, y, DetectionProblem.WINDOW_WIDTH, DetectionProblem.WINDOW_HEIGHT, DetectionProblem.WINDOWBLOCKSX, DetectionProblem.WINDOWBLOCKSY);
            }

            int result = (int) calculate(haarTrainingSet.image);

            if (haarTrainingSet.classID == DetectionProblem.OBJECT1) {
                if (result == haarTrainingSet.classID) {
                    // if it is a face and classifier was correct
                    correct++;
                    returnedTrue++;
                } else {
                    // if it is a face and classifier is incorrect
                    mistakes++;
                    returnedFalse++;
                }
            } else {
                // cached output expected class is NOT face

                if (result == haarTrainingSet.classID) {
                    // if it isn't a face and classifier is correct
                    correct++;
                    returnedFalse++;
                } else {
                    // if it isn't a face and classifier is incorrect.
                    mistakes++;
                    returnedTrue++;
                }
            }

            total++;

        }

        System.out.println("Total: " + total);
        System.out.println("Correct: " + correct);
        System.out.println("Mistakes: " + mistakes);
        System.out.println("Returned true: " + returnedTrue);
        System.out.println("Returned false: " + returnedFalse);
        
    }

}
