001package es.ucm.fdi.gaia.jcolibri.method.maintenance.algorithms; 002 003import java.util.Collection; 004import java.util.LinkedList; 005import java.util.List; 006import java.util.ListIterator; 007 008import org.apache.log4j.LogManager; 009 010import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase; 011import es.ucm.fdi.gaia.jcolibri.extensions.classification.ClassificationSolution; 012import es.ucm.fdi.gaia.jcolibri.method.maintenance.AbstractCaseBaseEditMethod; 013import es.ucm.fdi.gaia.jcolibri.method.retrieve.RetrievalResult; 014import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.NNScoringMethod; 015import es.ucm.fdi.gaia.jcolibri.method.retrieve.selection.SelectCases; 016import es.ucm.fdi.gaia.jcolibri.method.reuse.classification.KNNClassificationConfig; 017import es.ucm.fdi.gaia.jcolibri.method.reuse.classification.KNNClassificationMethod; 018import es.ucm.fdi.gaia.jcolibri.method.revise.classification.BasicClassificationOracle; 019import es.ucm.fdi.gaia.jcolibri.method.revise.classification.ClassificationOracle; 020import es.ucm.fdi.gaia.jcolibri.util.ProgressController; 021 022 023/** 024 * Provides the ability to run the RENN case base editing algorithm 025 * on a case base to eliminate noise. 026 * 027 * @author Lisa Cummins 028 * @author Derek Bridge 029 * 18/05/07 030 */ 031public class RENNNoiseReduction extends AbstractCaseBaseEditMethod { 032 033 /** 034 * Simulates the RENN case base editing algorithm, returning the cases 035 * that would be deleted by the algorithm. 036 * @param cases The group of cases on which to perform editing. 037 * @param simConfig The similarity configuration for these cases. 038 * @return the list of cases that would be deleted by the 039 * RENN algorithm. 040 */ 041 public List<CBRCase> retrieveCasesToDelete(Collection<CBRCase> cases, KNNClassificationConfig simConfig) 042 { /* RENN Algorithm: 043 * 044 * T: Training Set 045 * 046 * Repeat 047 * changes = false 048 * For all x E T do 049 * If x does not agree with the majority of its NN 050 * T = T - {x} 051 * changes = true 052 * End-If 053 * End-For 054 * Until not changes 055 * 056 * Return T 057 */ 058 ProgressController.init(this.getClass(),"RENN Noise Reduction", ProgressController.UNKNOWN_STEPS); 059 List<CBRCase> localCases = new LinkedList<CBRCase>(); 060 061 for(CBRCase c: cases) 062 { localCases.add(c); 063 } 064 065 List<CBRCase> allCasesToBeRemoved = new LinkedList<CBRCase>(); 066 067 boolean changes = true; 068 while(changes && localCases.size() > 1) 069 { changes = false; 070 ListIterator<CBRCase> iter = localCases.listIterator(); 071 while (iter.hasNext()) 072 { CBRCase q = iter.next(); 073 iter.remove(); 074 Collection<RetrievalResult> knn = NNScoringMethod.evaluateSimilarity(localCases, q, simConfig); 075 knn = SelectCases.selectTopKRR(knn, simConfig.getK()); 076 try 077 { KNNClassificationMethod classifier = ((KNNClassificationConfig)simConfig).getClassificationMethod(); 078 ClassificationSolution predictedSolution = classifier.getPredictedSolution(knn); 079 ClassificationOracle oracle = new BasicClassificationOracle(); 080 if(!oracle.isCorrectPrediction(predictedSolution, q)) 081 { allCasesToBeRemoved.add(q); 082 changes = true; 083 } 084 else 085 { iter.add(q); 086 } 087 } catch(ClassCastException cce) 088 { LogManager.getLogger(RENNNoiseReduction.class).error(cce); 089 System.exit(0); 090 } 091 } 092 ProgressController.step(this.getClass()); 093 } 094 ProgressController.finish(this.getClass()); 095 return allCasesToBeRemoved; 096 } 097}