001 package jcolibri.method.maintenance; 002 003 import java.util.Collection; 004 import java.util.HashMap; 005 import java.util.Iterator; 006 import java.util.LinkedList; 007 import java.util.Set; 008 009 import jcolibri.cbrcore.CBRCase; 010 import jcolibri.method.retrieve.KNNretrieval.KNNConfig; 011 012 /** 013 * Calculates coverage, reachability and liability sets using 014 * both the CRR definition and the ICF definition 015 * 016 * @author Lisa Cummins 017 */ 018 public class SetCalculator { 019 020 public static final String COVERAGE_SET = "Coverage Set"; 021 022 public static final String REACHABILITY_SET = "Reachability Set"; 023 024 public static final String LIABILITY_SET = "Liability Set"; 025 026 /** 027 * Calculates coverage, reachability and liability sets 028 * @return the coverage, reachability and liability sets of 029 * the given case base according to the CRR definition 030 */ 031 public HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> calculateCRRSets(Collection<CBRCase> cases, KNNConfig simConfig) 032 { VotesCalculator vc = new VotesCalculator(); 033 HashMap<CBRCase, LinkedList<CBRCase>> retrievedCases = 034 vc.getkRetrievedCasesForWholeCB(cases, simConfig, 3); 035 HashMap<CBRCase, String> votes = vc.getPredictedClasses(retrievedCases); 036 HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> sets = 037 calculateCRRSets(retrievedCases, votes); 038 039 return sets; 040 } 041 042 043 /** 044 * Calculates coverage, reachability and liability sets 045 * @return the coverage, reachability and liability sets of 046 * the given case base according to the ICF definition 047 */ 048 public HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> calculateICFSets(LinkedList<CBRCase> cases, KNNConfig simConfig) 049 { VotesCalculator vc = new VotesCalculator(); 050 HashMap<CBRCase, LinkedList<CBRCase>> retrievedCases = 051 vc.getAllRetrievedCasesForWholeCB(cases, simConfig); 052 HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> sets = calculateICFSets(retrievedCases); 053 return sets; 054 } 055 056 /** 057 * Calculates the Coverage, Reachability and Liability Sets of the 058 * given cases according to the CRR definition 059 */ 060 private HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> calculateCRRSets( 061 HashMap<CBRCase, LinkedList<CBRCase>> retrievedCases, HashMap<CBRCase, String> votes) 062 { Set<CBRCase> cases = retrievedCases.keySet(); 063 HashMap<CBRCase, LinkedList<CBRCase>> coverageSet = new HashMap<CBRCase, LinkedList<CBRCase>>(); 064 HashMap<CBRCase, LinkedList<CBRCase>> reachabilitySet = new HashMap<CBRCase, LinkedList<CBRCase>>(); 065 HashMap<CBRCase, LinkedList<CBRCase>> liabilitySet = new HashMap<CBRCase, LinkedList<CBRCase>>(); 066 067 //Add each case to its own coverage and reachability sets 068 for(Iterator<CBRCase> caseIter=cases.iterator(); caseIter.hasNext(); ) 069 { CBRCase c = caseIter.next(); 070 LinkedList<CBRCase> selfCCoverage = new LinkedList<CBRCase>(); 071 LinkedList<CBRCase> selfRCoverage = new LinkedList<CBRCase>(); 072 selfCCoverage.add(c); 073 selfRCoverage.add(c); 074 coverageSet.put(c, selfCCoverage); 075 reachabilitySet.put(c, selfRCoverage); 076 } 077 078 for(Iterator<CBRCase> caseIter=cases.iterator(); caseIter.hasNext(); ) 079 { CBRCase c = caseIter.next(); 080 String actualSolution = c.getSolution().toString(); 081 String predictedSolution = votes.get(c); 082 LinkedList<CBRCase> retrieved = retrievedCases.get(c); 083 084 if(actualSolution.equals(predictedSolution)) 085 { LinkedList<CBRCase> casesReached = reachabilitySet.get(c); 086 for(Iterator<CBRCase> coverageIter=retrieved.iterator(); coverageIter.hasNext(); ) 087 { CBRCase c1 = coverageIter.next(); 088 String c1Solution = c1.getSolution().toString(); 089 090 if(c1Solution.equals(actualSolution)) 091 { LinkedList<CBRCase> casesCovered = coverageSet.get(c1); 092 if(!casesCovered.contains(c)) 093 casesCovered.add(c); 094 coverageSet.put(c1, casesCovered); 095 if(!casesReached.contains(c1)) 096 casesReached.add(c1); 097 } 098 } 099 reachabilitySet.put(c, casesReached); 100 } 101 else 102 { for(Iterator<CBRCase> covIter=retrieved.iterator(); covIter.hasNext(); ) 103 { CBRCase c1 = covIter.next(); 104 String covSolution = c1.getSolution().toString(); 105 if(!covSolution.equals(actualSolution)) 106 { LinkedList<CBRCase> liabilityCases = liabilitySet.get(c1); 107 if(liabilityCases == null) 108 { liabilityCases = new LinkedList<CBRCase>(); 109 } 110 liabilityCases.add(c); 111 liabilitySet.put(c1, liabilityCases); 112 } 113 } 114 } 115 } 116 117 HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> sets = 118 new HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>>(); 119 sets.put(COVERAGE_SET, coverageSet); 120 sets.put(REACHABILITY_SET, reachabilitySet); 121 sets.put(LIABILITY_SET, liabilitySet); 122 return sets; 123 } 124 125 /** 126 * Calculates the Coverage, and Reachability Sets of the 127 * given cases according to the ICF definition 128 */ 129 private HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> calculateICFSets( 130 HashMap<CBRCase, LinkedList<CBRCase>> retrievedCases) 131 { Set<CBRCase> cases = retrievedCases.keySet(); 132 HashMap<CBRCase, LinkedList<CBRCase>> coverageSet = new HashMap<CBRCase, LinkedList<CBRCase>>(); 133 HashMap<CBRCase, LinkedList<CBRCase>> reachabilitySet = new HashMap<CBRCase, LinkedList<CBRCase>>(); 134 135 for(Iterator<CBRCase> caseIter=cases.iterator(); caseIter.hasNext(); ) 136 { CBRCase c = caseIter.next(); 137 String cSolution = c.getSolution().toString(); 138 139 LinkedList<CBRCase> retrievedForC = retrievedCases.get(c); 140 //Add the case itself to be the first retrieved case so that it will 141 //be included in the coverage and reachability sets 142 retrievedForC.addFirst(c); 143 144 LinkedList<CBRCase> reachability = new LinkedList<CBRCase>(); 145 for(Iterator<CBRCase> retIter = retrievedForC.iterator(); retIter.hasNext(); ) 146 { CBRCase retrieved = retIter.next(); 147 String retSolution = retrieved.getSolution().toString(); 148 if(cSolution.equals(retSolution)) 149 { reachability.add(retrieved); 150 LinkedList<CBRCase> coverage = coverageSet.get(retrieved); 151 if(coverage == null) 152 { coverage = new LinkedList<CBRCase>(); 153 } 154 coverage.add(c); 155 coverageSet.put(retrieved, coverage); 156 } 157 else 158 { break; 159 } 160 } 161 reachabilitySet.put(c, reachability); 162 } 163 164 HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>> sets = 165 new HashMap<String, HashMap<CBRCase, LinkedList<CBRCase>>>(); 166 sets.put(COVERAGE_SET, coverageSet); 167 sets.put(REACHABILITY_SET, reachabilitySet); 168 return sets; 169 } 170 }