001    /**
002     * LeaveOneOutEvaluator.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * @author Lisa Cummins.
006     * GAIA - Group for Artificial Intelligence Applications
007     * http://gaia.fdi.ucm.es
008     * 07/05/2007
009     */
010    package jcolibri.extensions.evaluation.evaluators;
011    
012    import java.util.ArrayList;
013    import java.util.Date;
014    
015    import jcolibri.cbrcore.CBRCase;
016    import jcolibri.cbrcore.CBRCaseBase;
017    import jcolibri.exception.ExecutionException;
018    import jcolibri.extensions.evaluation.MaintenanceEvaluator;
019    
020    import org.apache.commons.logging.LogFactory;
021    
022    /**
023     * This methods uses all the cases as queries. 
024     * It executes so cycles as cases in the case base. 
025     * In each cycle one case is used as query.  
026     * 
027     * @author Juan A. Recio García - GAIA http://gaia.fdi.ucm.es
028     * @author Lisa Cummins.
029     * @version 2.0
030     */
031    public class MaintenanceLeaveOneOutEvaluator extends MaintenanceEvaluator 
032    {
033            /**
034             * Performs the Leave-One-Out evaluation. 
035             * For each case in the case base,  remove that case from the case base and use it as query for that cycle.
036             */
037            public void LeaveOneOut() 
038            {       try 
039                    {       java.util.ArrayList<CBRCase> aux = new java.util.ArrayList<CBRCase>();
040    
041                            long t = (new Date()).getTime();
042                            int numberOfCycles = 0;
043    
044                            // Run the precycle to load the case base
045                            LogFactory.getLog(this.getClass()).info("Running precycle()");
046                            CBRCaseBase caseBase = app.preCycle();
047    
048                            if (!(caseBase instanceof jcolibri.casebase.CachedLinealCaseBase))
049                                    LogFactory.getLog(this.getClass()).warn(
050                                            "Evaluation should be executed using a cached case base");
051    
052                            prepareCases(caseBase);
053    
054                            ArrayList<CBRCase> cases = new ArrayList<CBRCase>(caseBase.getCases());     
055    
056                            jcolibri.util.ProgressController.init(getClass(),"LeaveOneOut Evaluation", cases.size());
057                            
058                            //For each case in the case base
059                            for(CBRCase _case : cases) 
060                            {       //Delete the case in the case base
061                                    aux.clear();
062                                    aux.add(_case);
063                                    caseBase.forgetCases(aux);
064    
065                                    //Run the cycle
066                                    LogFactory.getLog(this.getClass()).info(
067                                            "Running cycle() " + numberOfCycles);
068    
069                                    app.cycle(_case);
070    
071                                    //Recover case base
072                                    caseBase.learnCases(aux);
073    
074                                    numberOfCycles++;
075                                    jcolibri.util.ProgressController.step(getClass());
076                            }
077    
078                            //Run PostCycle
079                            LogFactory.getLog(this.getClass()).info("Running postcycle()");
080                            app.postCycle();
081    
082                            jcolibri.util.ProgressController.finish(getClass());
083                            
084                            t = (new Date()).getTime() - t;
085    
086                            //complete evaluation report
087                            report.setTotalTime(t);
088                            report.setNumberOfCycles(numberOfCycles);
089    
090                    } catch (ExecutionException e) {
091                            LogFactory.getLog(this.getClass()).error(e);
092                    }
093            }
094    
095            /**
096             * Prepares the cases for evaluation
097             * @param caseBase the case base
098             */
099            protected void prepareCases(CBRCaseBase caseBase)
100            {       if(this.simConfig != null && this.editMethod != null)
101                    {       // Perform maintenance on this case base
102                            editCaseBase(caseBase);
103                    }
104            }
105    }