001/**
002 * InformationGain.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 * 29/10/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.extensions.recommendation.navigationByAsking;
010
011import java.util.ArrayList;
012import java.util.Collection;
013import java.util.Enumeration;
014import java.util.HashSet;
015import java.util.Hashtable;
016import java.util.List;
017
018import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
019import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
020import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
021import es.ucm.fdi.gaia.jcolibri.exception.ExecutionException;
022import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils;
023
024
025/**
026 * Selects an attribute with the highest information gain.
027 * <p>See:
028 * <p>R. Bergmann. Experience Management: Foundations, Development Methodology, 
029 * and Internet-Based Applications. Springer-Verlag New York, Inc.,Secaucus,  
030 * NJ, USA, 2002.
031 * <p>
032 * S. Schulz. CBR-works: A state-of-the-art shell for case-based application
033 * building. In E. Melis, editor, Proceedings of the 7th German Workshop on
034 * Case-Based Reasoning, GWCBR'99, W�rzburg, Germany, pages 166-175.
035 * University of W�rzburg, 1999.
036 * 
037 * @author Juan A. Recio-Garcia
038 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
039 * @version 1.0
040 *
041 */
042public class InformationGain implements SelectAttributeMethod {
043    /******************************************************************************/
044    /**                           STATIC METHODS                                 **/
045    /******************************************************************************/
046
047    private static ArrayList<Attribute> asked;
048        
049    /**
050     * Selects an attribute with the highest information gain.
051     * @param cases are the working cases
052     * @param init indicates if this is the first time that the algorithm is executed.
053     * This way, in following iterations past chosen attributes are not computed.
054     * @param completeSetOfCases is the original case set used when there are not attributes
055     * with any information gain. This way, the method starts again with all the cases. 
056     * @return the selected attribute or null if there are not more attributes to ask.
057     * @throws ExecutionException
058     */
059    public static Attribute getMoreIGattribute(Collection<CBRCase> cases, boolean init, Collection<CBRCase> completeSetOfCases) throws ExecutionException
060    {
061        if(cases.isEmpty())
062        {
063            cases.addAll(completeSetOfCases);
064            init = true;
065        }
066        if(init)
067            asked = new ArrayList<Attribute>();
068        if(asked ==null)
069            throw new ExecutionException("InformationGain method must be initialized each cycle");
070        CBRCase acase = cases.iterator().next();
071        List<Attribute> atts = AttributeUtils.getAttributes(acase.getDescription());
072        atts.remove(acase.getDescription().getIdAttribute());
073        
074        atts.removeAll(asked);
075        if(atts.isEmpty())
076        {
077            asked = new ArrayList<Attribute>();
078            atts = AttributeUtils.getAttributes(acase.getDescription());
079            atts.remove(acase.getDescription().getIdAttribute());
080        }
081        
082        System.out.println("Computing IG for "+cases.size()+" cases");
083        
084        double maxIG = 0;
085        Attribute maxIGatt = null;
086        for(Attribute a: atts)
087        {
088            double ig = computeIG(a,cases);
089            System.out.println("IG "+a.getName()+" --> "+ig);
090            if(ig>maxIG)
091            {
092                maxIG = ig;
093                maxIGatt = a;
094            }
095        }
096        
097        asked.add(maxIGatt);
098        return maxIGatt;
099    }
100    
101    /**
102     * Computes the IG for an attribute
103     */
104    private static double computeIG(Attribute a, Collection<CBRCase> cases)
105    {
106        Hashtable<Object,HashSet<CBRCase>> clases = new Hashtable<Object,HashSet<CBRCase>>();
107        for(CBRCase c: cases)
108        {
109            Object value = AttributeUtils.findValue(a, c.getDescription());
110            HashSet<CBRCase> set = clases.get(value);
111            if(set==null)
112            {
113                set = new HashSet<CBRCase>();
114                clases.put(value, set);
115            }
116            set.add(c);
117        }
118        
119        double casesSize = cases.size();
120        double res = 0;
121        for(Enumeration<HashSet<CBRCase>> en = clases.elements(); en.hasMoreElements();)
122        {
123            double setSize = en.nextElement().size();
124            double div = setSize/casesSize;
125            res+= div * (Math.log(div)/Math.log(2));
126        }
127        return -res;
128    }
129
130    /******************************************************************************/
131    /**                           OBJECT METHODS                                 **/
132    /******************************************************************************/
133    
134    /**
135     * Original set of cases, used when there is not IG
136     */
137    private Collection<CBRCase> completeSetOfCases;
138    
139    /**
140     * Constructor. 
141     * @param completeset is the original set of cases, used when there is not IG
142     */
143    public InformationGain(Collection<CBRCase> completeset)
144    {
145        completeSetOfCases = completeset;
146        asked = new ArrayList<Attribute>();
147    }
148    
149    /**
150     * Selects the attribute to be asked
151     * @param cases list of working cases
152     * @param query is the current query
153     * @return selected attribute
154     * @throws ExecutionException
155     */
156    public Attribute getAttribute(Collection<CBRCase> cases, CBRQuery query) throws ExecutionException
157    {
158            return getMoreIGattribute(cases, false, completeSetOfCases);
159
160    }
161}