001/** 002 * SimilarityInfluence.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-Garc�a. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 30/10/2007 008 */ 009package es.ucm.fdi.gaia.jcolibri.extensions.recommendation.navigationByAsking; 010 011import java.util.ArrayList; 012import java.util.Collection; 013import java.util.HashSet; 014import java.util.Hashtable; 015import java.util.List; 016 017import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute; 018import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase; 019import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery; 020import es.ucm.fdi.gaia.jcolibri.exception.ExecutionException; 021import es.ucm.fdi.gaia.jcolibri.method.retrieve.RetrievalResult; 022import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.NNConfig; 023import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.NNScoringMethod; 024import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils; 025import es.ucm.fdi.gaia.jcolibri.util.CopyUtils; 026import es.ucm.fdi.gaia.jcolibri.util.ProgressController; 027 028/** 029 * Selects the attribute that has the highest infuence on the KNN similarity. 030 * The inuence on the similarity can be measured by the expected variance 031 * of the similarities of a set of selected cases. 032 * 033 * This method is not recommended with large case bases. 034 * 035 * <p>See: 036 * <p> 037 * R. Bergmann. Experience Management: Foundations, Development Methodology, 038 * and Internet-Based Applications. Springer-Verlag New York, Inc.,Secaucus, 039 * NJ, USA, 2002. 040 * <p> 041 * A. Kohlmaier, S. Schmitt, and R. Bergmann. A similarity-based approach to 042 * attribute selection in user-adaptive sales dialogs. In D. W. Aha and I. Watson, 043 * editors, Proceedings of the 4th International Conference on Case-Based 044 * Reasoning, pages 306320, Seattle, Washington, 2001. Springer-Verlag. 045 * <p> 046 * S. Schmitt, P. Dopichaj, and P. Dom�nguez-Mar�n. Entropy-based vs. 047 * similarity-inuenced: Attribute selection methods for dialogs tested on different 048 * electronic commerce domains. In S. Craw and A. Preece, editors, 049 * Proceedings of the 6th European Conference on Case-Based Reasoning, pages 050 * 380-394, Aberdeen, Scotland, 2002. Springer-Verlag. 051 * 052 * @author Juan A. Recio-Garcia 053 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge. 054 * @version 1.0 055 * 056 */ 057public class SimilarityInfluence implements SelectAttributeMethod 058{ 059 private static ArrayList<Attribute> asked; 060 061 /******************************************************************************/ 062 /** STATIC METHODS **/ 063 /******************************************************************************/ 064 065 /** 066 * Selects the attribute with more expected influence in the NN scoring. 067 * @param cases Set of working cases 068 * @param query Query to compare with the cases 069 * @param simConfig is the NN similiarity configuration 070 * @param init indicates if this is the first time that the algorithm is executed. 071 * This way, in following iterations past chosen attributes are not computed. 072 * @return the selected attribute or null if there are not more attributes to ask. 073 */ 074 public static Attribute getMoreSimVarAttribute(Collection<CBRCase> cases, CBRQuery query, NNConfig simConfig, boolean init) throws ExecutionException 075 { 076 if(init) 077 asked = new ArrayList<Attribute>(); 078 if(asked ==null) 079 throw new ExecutionException("Similarity Influence method must be initialized each cycle"); 080 CBRCase acase = cases.iterator().next(); 081 List<Attribute> atts = AttributeUtils.getAttributes(acase.getDescription()); 082 atts.remove(acase.getDescription().getIdAttribute()); 083 084 atts.removeAll(asked); 085 if(atts.isEmpty()) 086 { 087 asked = new ArrayList<Attribute>(); 088 atts = AttributeUtils.getAttributes(acase.getDescription()); 089 } 090 091 ProgressController.init(SimilarityInfluence.class,"Similarity Influence selection", ProgressController.UNKNOWN_STEPS); 092 System.out.println("Computing SimVar for "+cases.size()+" cases"); 093 094 double maxSimVar = 0; 095 Attribute maxSimVaratt = null; 096 for(Attribute a: atts) 097 { 098 double simVar = computeSimVar(a,cases,query,simConfig); 099 System.out.println("SimVar("+a.getName()+") = "+simVar); 100 if(simVar>maxSimVar) 101 { 102 maxSimVar = simVar; 103 maxSimVaratt = a; 104 } 105 } 106 107 ProgressController.finish(SimilarityInfluence.class); 108 109 asked.add(maxSimVaratt); 110 return maxSimVaratt; 111 } 112 113 /** 114 * Computes the simVar of an attribute with respect to a set of cases and a query. 115 */ 116 private static double computeSimVar(Attribute a, Collection<CBRCase> cases, CBRQuery query, NNConfig simConfig) 117 { 118 double Csize = cases.size(); 119 120 Hashtable<Object,HashSet<CBRCase>> clases = new Hashtable<Object,HashSet<CBRCase>>(); 121 for(CBRCase c: cases) 122 { 123 Object value = AttributeUtils.findValue(a, c.getDescription()); 124 HashSet<CBRCase> set = clases.get(value); 125 if(set==null) 126 { 127 set = new HashSet<CBRCase>(); 128 clases.put(value, set); 129 } 130 set.add(c); 131 } 132 133 134 double simVar = 0; 135 for(Object v : clases.keySet()) 136 { 137 double pv = ((double)clases.get(v).size()) / Csize; 138 139 CBRQuery newQuery = new CBRQuery(); 140 newQuery.setDescription(CopyUtils.copyCaseComponent(query.getDescription())); 141 AttributeUtils.setValue(a, newQuery, v); 142 double var = computeVar(newQuery, cases, simConfig); 143 simVar += (pv * var); 144 ProgressController.step(SimilarityInfluence.class); 145 146 } 147 148 return simVar; 149 } 150 151 /** 152 * Computes the Var formulae 153 */ 154 private static double computeVar(CBRQuery query, Collection<CBRCase> cases, NNConfig simConfig) 155 { 156 List<RetrievalResult> sim = NNScoringMethod.evaluateSimilarity(cases, query, simConfig); 157 158 double niu = 0; 159 for(RetrievalResult rr : sim) 160 niu += rr.getEval(); 161 niu = niu / sim.size(); 162 163 double res = 0; 164 for(RetrievalResult rr : sim) 165 res += ( (rr.getEval()-niu)*(rr.getEval()-niu) ); 166 167 168 return res / ((double)cases.size()); 169 } 170 171 /******************************************************************************/ 172 /** OBJECT METHODS **/ 173 /******************************************************************************/ 174 175 /** KNN configuration */ 176 private NNConfig simConfig; 177 178 /** 179 * Constructor 180 * @param simConfig is the KNN configuration 181 */ 182 public SimilarityInfluence(NNConfig simConfig) 183 { 184 this.simConfig = simConfig; 185 } 186 187 /** 188 * Selects the attribute to be asked 189 * @param cases list of working cases 190 * @param query is the current query 191 * @return selected attribute 192 * @throws ExecutionException 193 */ 194 public Attribute getAttribute(Collection<CBRCase> cases, CBRQuery query) throws ExecutionException 195 { 196 return getMoreSimVarAttribute(cases, query, simConfig, false); 197 } 198}