001    /**
002     * BoundedRandomSelection.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     * 05/11/2007
008     */
009    package jcolibri.method.retrieve.selection.diversity;
010    
011    import java.util.ArrayList;
012    import java.util.Collection;
013    
014    import jcolibri.cbrcore.CBRCase;
015    import jcolibri.cbrcore.CBRQuery;
016    import jcolibri.method.retrieve.RetrievalResult;
017    import jcolibri.method.retrieve.selection.SelectCases;
018    
019    /**
020     * Is the simplest diversity strategy: select the k cases at random from a larger 
021     * set of the b·k most similar cases to the query.
022     * <p>See:
023     * <p>
024     * B. Smyth and P. McClave. Similarity vs. diversity. In ICCBR '01: Proceedings 
025     * of the 4th International Conference on Case-Based Reasoning, pages 347-361, 
026     * London, UK, 2001. Springer-Verlag.
027     * 
028     * @author Juan A. Recio-Garcia
029     * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
030     * @version 1.0
031     */
032    public class BoundedRandomSelection
033    {
034       
035       
036        /**
037         * Executes the algorithm.
038         */
039        public static Collection<CBRCase> boundedRandomSelection(Collection<RetrievalResult> cases, CBRQuery query, int k, int bound)
040        {
041            Collection<CBRCase> nn = SelectCases.selectTopK(cases, k*bound);
042            
043            Collection<CBRCase> res = new ArrayList<CBRCase>();
044            
045            for(int i=0 ; (i<k)&&(i<nn.size()); i++)
046            {
047                int chosen = (int)(Math.random()*nn.size());
048                CBRCase rr = (CBRCase)nn.toArray()[chosen];
049                res.add(rr);
050                nn.remove(rr);
051            }
052            
053            return res;
054        }
055    }