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