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