001    package jcolibri.method.revise.classification;
002    
003    import java.util.Collection;
004    
005    import jcolibri.cbrcore.CBRCase;
006    import jcolibri.cbrcore.CBRCaseBase;
007    import jcolibri.cbrcore.CBRQuery;
008    import jcolibri.extensions.classification.ClassificationSolution;
009    import jcolibri.method.retrieve.RetrievalResult;
010    import jcolibri.method.retrieve.NNretrieval.NNScoringMethod;
011    import jcolibri.method.retrieve.selection.SelectCases;
012    import jcolibri.method.reuse.classification.KNNClassificationConfig;
013    import jcolibri.method.reuse.classification.KNNClassificationMethod;
014    
015    /**
016     * This class represents a decision-maker to decide if a query has been 
017     * correctly classified or not and to assign a cost to incorrectly 
018     * classified queries.
019     * 
020     * @author Derek Bridge
021     * @author Lisa Cummins
022     * 16/05/07
023     */
024    public class BasicClassificationOracle implements ClassificationOracle {
025        
026        /**
027         * Checks if the predicted solution is the correct solution
028         * for the given test case.
029         * @param predictedSolution the predicted solution.
030         * @param testCase the test case (query and correct solution).
031         * @return true if the predicted solution is the correct solution
032         * for the given test case, false if not. 
033         */
034        public boolean isCorrectPrediction(ClassificationSolution predictedSolution, CBRCase testCase)
035        {   
036            ClassificationSolution correctSolution = (ClassificationSolution)testCase.getSolution();
037            return isCorrectPrediction(predictedSolution, correctSolution);
038        }
039        
040        /**
041         * Checks if the predicted solution and the correct solution
042         * are the same.
043         * @param predictedSolution the predicted solution.
044         * @param correctSolution the correct solution.
045         * @return true if the predicted solution and the correct 
046         * solution are the same, false if not. 
047         */
048        public boolean isCorrectPrediction(ClassificationSolution predictedSolution, ClassificationSolution correctSolution)
049        {   
050            return predictedSolution.getClassification().equals(correctSolution.getClassification());
051        }
052        
053        /**
054         * Checks if the query is correctly classified by the given case-base and 
055         * similarity configuration.
056         * @param query the query to be tested.
057         * @param caseBase the case base to use to find the predicted solution.
058         * @param knnConfig the similarity configuration.
059         * @return true if the query is correctly classified by the given case-base and 
060         * similarity configuration, fasle otherwise.
061         */
062        public boolean isCorrectPrediction(CBRQuery query, CBRCaseBase caseBase, KNNClassificationConfig knnConfig)
063        {   Collection<CBRCase> cases = caseBase.getCases();
064            Collection<RetrievalResult> knn = NNScoringMethod.evaluateSimilarity(cases, query, knnConfig);
065            knn = SelectCases.selectTopKRR(knn, knnConfig.getK());
066            KNNClassificationMethod classifier = knnConfig.getClassificationMethod();
067            ClassificationSolution predictedSolution = classifier.getPredictedSolution(knn);
068            return isCorrectPrediction(predictedSolution, (CBRCase)query);
069        }
070    
071        /**
072             * Calculates the cost of the given solution as a prediction for the 
073             * solution of the given case. The cost returned is 0 for a correct
074             * prediction and 1 for a wrong prediction.
075         * @param predictedSolution the predicted solution.
076         * @param testCase the test case (query and correct solution).
077         * @return the cost of the prediction made (0 if the prediction
078         * is correct, 1 otherwise).
079         */
080        public double getPredictionCost(ClassificationSolution predictedSolution, CBRCase testCase)
081        {   
082            ClassificationSolution correctSolution = (ClassificationSolution)testCase.getSolution();
083                    return getPredictionCost(predictedSolution, correctSolution);
084        }
085    
086        /**
087             * Calculates the cost of the given test solution while bein compared
088             * to the given correct solution. The cost returned is 0 for a correct
089             * prediction and 1 for a wrong prediction.
090         * @param predictedSolution the predicted solution.
091         * @param correctSolution the correct solution.
092         * @return Returns the cost of the prediction made (0 if the prediction
093         * is correct, 1 otherwise).
094         */
095        public double getPredictionCost(ClassificationSolution predictedSolution, ClassificationSolution correctSolution)
096        {   
097            return isCorrectPrediction(predictedSolution, correctSolution) ? 0 : 1;
098        }
099        
100        /**
101         * Calculates the cost of the prediction made by the given case-base and
102         * similarity configuration for the solution of the query. The cost 
103         * returned is 0 for a correct prediction and 1 for a wrong prediction.
104         * @param query the query to be tested.
105         * @param caseBase the case base to use to find the predicted solution.
106         * @param knnConfig the similarity configuration.
107         * @return the cost of the prediction made (0 if the prediction
108         * is correct, 1 otherwise).
109         */
110        public double getPredictionCost(CBRQuery query, CBRCaseBase caseBase, KNNClassificationConfig knnConfig)
111        {   Collection<CBRCase> cases = caseBase.getCases();
112            Collection<RetrievalResult> knn = NNScoringMethod.evaluateSimilarity(cases, query, knnConfig);
113            knn = SelectCases.selectTopKRR(knn, knnConfig.getK());
114            KNNClassificationMethod classifier = knnConfig.getClassificationMethod();
115            ClassificationSolution predictedSolution = classifier.getPredictedSolution(knn);
116            return getPredictionCost(predictedSolution, (CBRCase)query);
117        }
118    }