001package es.ucm.fdi.gaia.jcolibri.method.maintenance.solvesFunctions;
002
003import java.util.Collection;
004import java.util.Iterator;
005import java.util.LinkedList;
006
007import org.apache.logging.log4j.LogManager;
008
009import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
010import es.ucm.fdi.gaia.jcolibri.extensions.classification.ClassificationSolution;
011import es.ucm.fdi.gaia.jcolibri.method.maintenance.SolvesFunction;
012import es.ucm.fdi.gaia.jcolibri.method.retrieve.RetrievalResult;
013import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.NNScoringMethod;
014import es.ucm.fdi.gaia.jcolibri.method.reuse.classification.KNNClassificationConfig;
015import es.ucm.fdi.gaia.jcolibri.method.reuse.classification.KNNClassificationMethod;
016import es.ucm.fdi.gaia.jcolibri.method.revise.classification.BasicClassificationOracle;
017import es.ucm.fdi.gaia.jcolibri.method.revise.classification.ClassificationOracle;
018
019/**
020 * Provides the solves function that Sarah-Jane Delaney uses
021 * in her case-base editing (CBE) algorithms which will 
022 * decide which cases solve a query. 
023 * 
024 * @author Lisa Cummins
025 * @author Derek Bridge
026 * 22/05/07
027 */
028public class CBESolvesFunction extends SolvesFunction
029{
030        /**
031         * Sets the cases that either solve q or contribute to its 
032         * misclassification.
033         * A case solves a query if the query is correctly classified
034         * by its nearest neighbours and the solution of the case
035         * agrees with the solution of the query. 
036         * A case misclassifies a query if the query is incorrectly
037         * classified by its nearest neighbours and the solution of the
038         * case disagrees with the solution of the query. 
039         * 
040         * @param q the query
041         * @param cases from which to find the cases which solve
042         * and classify the query. These include the query itself. 
043         * @param knnConfig the similarity configuration
044         */
045        public void setCasesThatSolveAndMisclassifyQ(CBRCase q, Collection<CBRCase> cases, KNNClassificationConfig knnConfig)
046        {
047                solveQ = new LinkedList<CBRCase>(); //It will always contain at least the query itself
048                misclassifyQ = null;
049                
050                /* q is regarded to solve itself regardless of whether it is
051                 * correctly or incorrectly classified by its nearest neighbours
052                 * and so we add it to its solveQ set.
053                 */  
054                solveQ.add(q);
055                
056                /* Because q is included in the cases, we retrieve k+1 neighbours and then 
057                 * either remove q, or, if q is not contained in the retrieved cases, we  
058                 * remove the last case of those retrieved
059                 */
060                knnConfig.setK(knnConfig.getK()+1);
061                Collection<RetrievalResult> knnResults = NNScoringMethod.evaluateSimilarity(cases, q, knnConfig);
062                //Collection<CBRCase> knn = SelectCases.selectTopK(knnResults, knnConfig.getK());
063                knnConfig.setK(knnConfig.getK()-1); 
064                RetrievalResult result = null;
065                boolean qFound = false;
066                
067                for(Iterator<RetrievalResult> cIter = knnResults.iterator(); cIter.hasNext() && !qFound; )
068                {       result = cIter.next();
069                        if(result.get_case().equals(q))
070                        {       knnResults.remove(result);                          
071                                qFound = true;
072                        }
073                }
074                //if(!qFound)
075                //{     knn.remove(result);
076                //}
077                try 
078                {       KNNClassificationMethod classifier = ((KNNClassificationConfig)knnConfig).getClassificationMethod();
079                        ClassificationSolution predictedSolution = classifier.getPredictedSolution(knnResults);
080        
081                        ClassificationOracle oracle = new BasicClassificationOracle();
082                        boolean correct = oracle.isCorrectPrediction(predictedSolution, q);
083                        if(correct)
084                        {       for(RetrievalResult res: knnResults)
085                                {       CBRCase c = res.get_case();
086                                        if(oracle.isCorrectPrediction((ClassificationSolution)c.getSolution(), q))
087                                        {       solveQ.add(c);
088                                        }
089                                }
090                        }
091                        else
092                        {       misclassifyQ = new LinkedList<CBRCase>();
093                                for(RetrievalResult res: knnResults)
094                                {       CBRCase c = res.get_case();
095                                        if(!oracle.isCorrectPrediction((ClassificationSolution)c.getSolution(), q))
096                                        {       misclassifyQ.add(c);
097                                        }
098                                }
099                                
100                        }
101                } catch(ClassCastException cce)
102                {       LogManager.getLogger().error(cce);
103                        System.exit(0);
104                }
105
106        }
107}