001    package jcolibri.method.maintenance;
002    
003    import java.util.Collection;
004    import java.util.LinkedList;
005    
006    import jcolibri.cbrcore.CBRCase;
007    import jcolibri.method.retrieve.KNNretrieval.KNNConfig;
008    
009    /**
010     * Implements the RENN Maintenance Algorithm to remove 
011     * noise from the case base 
012     * 
013     * @author Lisa Cummins
014     */
015    public class RENNAlgorithm extends MaintenanceAlgorithm {
016    
017            /**
018             * Runs the RENN maintenance algorithm, returning the cases
019             * deleted by the algorithm
020             * @param cases The group of cases on which to perform maintenance
021             * @param simConfig The KNNConfig for these cases
022             * @return the list of cases deleted by the algorithm
023             */
024            public LinkedList<CBRCase> runMaintenance(Collection<CBRCase> cases, KNNConfig simConfig) 
025            {       /* RENN Algorithm:
026                     *
027                     * T: Training Set
028                     * 
029                     * Repeat
030                     *              changes = false
031                     *              For all x E T do
032                     *                      If x does not agree with the majority of its NN
033                     *                              T = T - {x}
034                     *                              changes = true
035                     *                      End-If
036                     *              End-For
037                     * Until not changes
038                     *
039                     * Return T     
040                     */
041                    
042                    LinkedList<CBRCase> localCases = new LinkedList<CBRCase>();
043                    
044                    for(CBRCase c: cases)
045                    {       localCases.add(c);
046                    }
047                    
048                    int k = 3;
049                    
050                    VotesCalculator vc = new VotesCalculator();
051                    LinkedList<CBRCase> allCasesToBeRemoved = new LinkedList<CBRCase>();
052    
053                    boolean changes = true;
054                    while(changes && localCases.size() > 1)
055                    {       changes = false;
056                                    
057                            for(int i=0; i<localCases.size(); )
058                            {       CBRCase c = localCases.remove(i);
059                                    
060                                    LinkedList<CBRCase> retrievedCases = vc.getkRetrievedCasesForQuery(localCases, c, simConfig, k);
061                                                                    
062                                    String predictedSolution = vc.getPredictedClass(retrievedCases);
063                                    String actualSolution = c.getSolution().toString();
064                                    
065                                    if(!actualSolution.equals(predictedSolution))
066                                    {       allCasesToBeRemoved.add(c);
067                                            changes = true;
068                                    }
069                                    else
070                                    {       localCases.add(i, c);
071                                            i++;
072                                    }
073                            }
074                    }
075    
076                    return allCasesToBeRemoved;
077            }
078    }