package ac.essex.ooechs.lcs.test;

import ac.essex.ooechs.lcs.Action;
import ac.essex.ooechs.lcs.Classifier;

import java.util.Vector;

/**
 * Represents a system prediction for each action represented in [M]
 * This particular implementation is a fitness-weighted average
 *
 * @author Olly Oechsle, University of Essex, Date: 14-Jan-2008
 * @version 1.0
 */
public class Prediction_OLD {

    protected double numerator = 0;
    protected double denominator = 0;
    protected double average = -1;

    protected Vector<Classifier> classifiers;
    protected Action action;

    public Prediction_OLD(Classifier classifier) {
        this.action = classifier.action;
        classifiers = new Vector<Classifier>();
        addRule(classifier);
    }

    public void addRule(Classifier classifier) {
        if (!classifier.action.equals(action)) {
            // make sure all rules have the same actio
            throw new RuntimeException("Attempt to add an invalid rule to the prediction");
        }
        numerator += (classifier.p * classifier.fitness * classifier.numerosity);
        denominator += classifier.fitness * classifier.numerosity;
        classifiers.add(classifier);
        average = -1;
    }

    /**
     * Gets the action associated with this particular prediction.
     */
    public Action getAction() {
        return action;
    }

    /**
     * Returns all the rules which recommend this particular action.
     */
    public Vector<Classifier> getRules() {
        return classifiers;
    }

    /**
     * Returns a fitness weighted average of the predictions
     * of the rules with this action in [M]
     */
    public double getPrediction() {
        if (average == -1) {
            average = numerator / denominator;
        }
        return average;
    }

}
