001package es.ucm.fdi.gaia.jcolibri.method.maintenance.algorithms; 002 003 004import org.apache.logging.log4j.LogManager; 005 006import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase; 007import es.ucm.fdi.gaia.jcolibri.exception.InitializingException; 008import es.ucm.fdi.gaia.jcolibri.method.maintenance.AbstractCaseBaseEditMethod; 009import es.ucm.fdi.gaia.jcolibri.method.maintenance.CompetenceModel; 010import es.ucm.fdi.gaia.jcolibri.method.maintenance.solvesFunctions.CBESolvesFunction; 011import es.ucm.fdi.gaia.jcolibri.method.reuse.classification.KNNClassificationConfig; 012import es.ucm.fdi.gaia.jcolibri.util.ProgressController; 013 014import java.util.Collection; 015import java.util.LinkedList; 016import java.util.List; 017 018/** 019 * Provides the ability to run the RC case base editing algorithm 020 * on a case base to eliminate redundancy. 021 * 022 * @author Lisa Cummins 023 * @author Derek Bridge 024 * 18/05/07 025 */ 026public class RCRedundancyRemoval extends AbstractCaseBaseEditMethod { 027 028 /** 029 * Simulates the RC case base editing algorithm, returning the cases 030 * that would be deleted by the algorithm. 031 * @param cases The group of cases on which to perform editing. 032 * @param simConfig The similarity configuration for these cases. 033 * @return the list of cases that would be deleted by the 034 * RC algorithm. 035 */ 036 public List<CBRCase> retrieveCasesToDelete(Collection<CBRCase> cases, KNNClassificationConfig simConfig) 037 { /* 038 * RC Algorithm: 039 * 040 * T: Original training cases 041 * CM: Competence Model 042 * RC(c): Sum_c' E CoverageSet(C) (1/|ReachabilitySet(c')|) 043 * 044 * Edit(T,CM,RC): 045 * 046 * R-Set = RENN(T) {that is, repeated ENN} 047 * (Not included here, RENN performed separately) 048 * E-Set = {} 049 * While R-Set is not empty 050 * c = Next case in R-Set according to RC 051 * E-Set = E-Set U {c} 052 * R-Set = R-Set � CoverageSet(c) 053 * Update(CM) 054 * EndWhile 055 * 056 * Return (E-Set) 057 */ 058 ProgressController.init(this.getClass(),"RC Redundancy Removal", ProgressController.UNKNOWN_STEPS); 059 List<CBRCase> localCases = new LinkedList<CBRCase>(); 060 for(CBRCase c: cases) 061 { localCases.add(c); 062 } 063 064 CompetenceModel sc = new CompetenceModel(); 065 066 LinkedList<CBRCase> keepCases = new LinkedList<CBRCase>(); 067 068 while(localCases.size() > 0) 069 { double topRCScore = 0.0; 070 CBRCase topRCCase = null; 071 072 sc.computeCompetenceModel(new CBESolvesFunction(), simConfig, localCases); 073 074 try 075 { for(CBRCase c: localCases) 076 { double rcScore = 0.0; 077 Collection<CBRCase> cCov = sc.getCoverageSet(c); 078 for(CBRCase c1: cCov) 079 { rcScore += (1/(double)sc.getReachabilitySet(c1).size()); 080 } 081 if(rcScore > topRCScore) 082 { topRCScore = rcScore; 083 topRCCase = c; 084 } 085 } 086 087 keepCases.add(topRCCase); 088 089 Collection<CBRCase> cSet = sc.getCoverageSet(topRCCase); 090 Collection<CBRCase> toRemove = new LinkedList<CBRCase>(); 091 for(CBRCase c: cSet) 092 { toRemove.add(c); 093 } 094 localCases.removeAll(toRemove); 095 } catch (InitializingException e) 096 { LogManager.getLogger().error(e); 097 } 098 ProgressController.step(this.getClass()); 099 } 100 101 //Add all cases that are not being kept to the list of deleted cases 102 List<CBRCase> allCasesToBeRemoved = new LinkedList<CBRCase>(); 103 for(CBRCase c: cases) 104 { if(!keepCases.contains(c)) 105 allCasesToBeRemoved.add(c); 106 } 107 ProgressController.finish(this.getClass()); 108 return allCasesToBeRemoved; 109 } 110}