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