001/** 002 * CollaborativeRetrievalMethod.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 * 11/11/2007 008 */ 009package es.ucm.fdi.gaia.jcolibri.extensions.recommendation.collaborative; 010 011import org.apache.logging.log4j.LogManager; 012 013import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase; 014import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery; 015import es.ucm.fdi.gaia.jcolibri.method.retrieve.RetrievalResult; 016 017import java.util.ArrayList; 018import java.util.Iterator; 019import java.util.List; 020 021 022/** 023 * This method returns cases depending on the recommendations of other users. 024 * <br> 025 * It uses a PearsonMatrix Case base to compute the similarity among neighbors. 026 * Then, cases are scored according to a rating that is estimated using the following 027 * formula:<br> 028 * <img src="collaborativerating.jpg"/> 029 * <p> 030 * See:<p> 031 * J. Kelleher and D. Bridge. An accurate and scalable collaborative recommender. 032 * Articial Intelligence Review, 21(3-4):193-213, 2004. 033 * 034 * 035 * @author Juan A. Recio-Garcia 036 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge. 037 * @version 1.0 038 */ 039public class CollaborativeRetrievalMethod { 040 /** 041 * Returns a list of cases scored following the collaborative recommendation formulae. 042 * @param cb is the case base that contains the cases 043 * @param id of the user 044 * @param kItems is the number of items/ratings to return 045 * @param kUsers defines the number of users taken into account to score the cases. 046 */ 047 public static List<RetrievalResult> getRecommendation(PearsonMatrixCaseBase cb, CBRQuery query, int kUsers) 048 { 049 ArrayList<RetrievalResult> result = new ArrayList<RetrievalResult>(); 050 051 int id = (Integer)query.getID(); 052 List<MatrixCaseBase.SimilarTuple> simil = cb.getSimilar(id); 053 054 if(simil == null) 055 { 056 LogManager.getLogger().error("Id "+id+" does not exists"); 057 return result; 058 } 059 060 061 ArrayList<MatrixCaseBase.SimilarTuple> select = new ArrayList<MatrixCaseBase.SimilarTuple>(); 062 int i=0; 063 for(Iterator<MatrixCaseBase.SimilarTuple> iter = simil.iterator(); (iter.hasNext() && i<kUsers); i++) 064 select.add(iter.next()); 065 066 067 /////// debug 068 System.out.println("\nQuery: "+ cb.getDescription(id)); 069 System.out.println(cb.getRatingTuples(id).size()+" Ratings: "+cb.getRatingTuples(id)); 070 System.out.println("\nSimilar ratings:"); 071 for(MatrixCaseBase.SimilarTuple st: select) 072 { 073 System.out.print(st.getSimilarity()+" <--- "); 074 System.out.println(cb.getDescription(st.getSimilarId())); 075 System.out.println(cb.getRatingTuples(st.getSimilarId()).size()+" Ratings: "+cb.getRatingTuples(st.getSimilarId())); 076 } 077 ///////////// 078 079 for(Integer solId : cb.getSolutions()) 080 { 081 double mean = cb.getAverage(id); 082 double acum = 0; 083 double simacum = 0; 084 for(MatrixCaseBase.SimilarTuple st : select) 085 { 086 int other = st.getSimilarId(); 087 double rating = findRating(cb, other, solId); 088 double otherMean = cb.getAverage(other); 089 acum += ((rating - otherMean) * st.getSimilarity()); 090 simacum += st.getSimilarity(); 091 } 092 double res = mean + (acum/simacum); 093 094 CBRCase c = new CBRCase(); 095 c.setDescription(cb.getDescription(id)); 096 c.setSolution(cb.getSolution(solId)); 097 098 result.add(new RetrievalResult(c,res)); 099 } 100 101 java.util.Collections.sort(result); 102 103 return result; 104 } 105 106 private static double findRating(PearsonMatrixCaseBase cb, int descId, int solId) 107 { 108 for(MatrixCaseBase.RatingTuple rt: cb.getRatingTuples(descId)) 109 { 110 if(rt.getSolutionId() == solId) 111 return rt.getRating(); 112 } 113 return 0; 114 } 115}