001package es.ucm.fdi.gaia.jcolibri.evaluation.evaluators;
002
003import org.apache.logging.log4j.LogManager;
004
005import es.ucm.fdi.gaia.jcolibri.casebase.CachedLinealCaseBase;
006import es.ucm.fdi.gaia.jcolibri.cbraplications.StandardCBRApplication;
007import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
008import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCaseBase;
009import es.ucm.fdi.gaia.jcolibri.evaluation.EvaluationReport;
010import es.ucm.fdi.gaia.jcolibri.evaluation.Evaluator;
011import es.ucm.fdi.gaia.jcolibri.exception.ExecutionException;
012
013import java.util.ArrayList;
014import java.util.Collection;
015import java.util.Date;
016import java.util.List;
017
018/**
019 * This evaluation method divides the case base into several random folds (indicated by the user). 
020 * For each fold, their cases are used as queries and the remaining folds are used together as case base. 
021 * This process is performed several times.
022 * 
023 * @author Juan A. Recio GarcĂ­a - GAIA http://gaia.fdi.ucm.es
024 * @version 2.0
025 */
026public class NFoldEvaluator extends Evaluator {
027
028        protected StandardCBRApplication app;
029
030        public void init(StandardCBRApplication cbrApp) {
031                
032                report = new EvaluationReport();
033                app = cbrApp;
034                try {
035                        app.configure();
036                } catch (ExecutionException e) {
037            LogManager.getLogger().error(e);
038                }
039        }
040
041        /**
042         * Executes the N-Fold evaluation.
043         * @param folds Number of folds (randomly generated).
044         * @param repetitions Number of repetitions
045         */
046    public void NFoldEvaluation(int folds, int repetitions)
047    {   
048        try
049        {
050            //Get the time
051            long t = (new Date()).getTime();
052            int numberOfCycles = 0;
053
054                // Run the precycle to load the case base
055            LogManager.getLogger().info("Running precycle()");
056                        CBRCaseBase caseBase = app.preCycle();
057
058                        if (!(caseBase instanceof CachedLinealCaseBase))
059                LogManager.getLogger().warn(
060                                                                "Evaluation should be executed using a cached case base");
061            
062            Collection<CBRCase> cases = new ArrayList<>(caseBase.getCases());
063            
064            //For each repetition
065            for(int r=0; r<repetitions; r++)
066            {
067                //Create the folds
068                createFolds(cases, folds);
069                
070                //For each fold
071                for(int f=0; f<folds; f++)
072                {
073                    ArrayList<CBRCase> querySet = new ArrayList<>();
074                    ArrayList<CBRCase> caseBaseSet = new ArrayList<>();
075
076                    //Obtain the query and casebase sets
077                    getFolds(f, querySet, caseBaseSet);
078                    
079                    //Clear the caseBase
080                    caseBase.forgetCases(cases);
081                    
082                    //Set the cases that acts as casebase in this cycle
083                    caseBase.learnCases(caseBaseSet);
084                    
085                    //Run cycle for each case in querySet (current fold)
086                    for(CBRCase c: querySet)
087                    {
088                        LogManager.getLogger().info(
089                                                                "Running cycle() " + numberOfCycles);
090                                                app.cycle(c);
091                        
092                        numberOfCycles++;
093                    }          
094                } 
095                
096            }
097
098                        //Revert case base to original state
099                        caseBase.forgetCases(cases);
100                        caseBase.learnCases(cases);
101            
102            //Run the poscycle to finish the application
103            LogManager.getLogger().info("Running postcycle()");
104                        app.postCycle();
105
106            
107            //Complete the evaluation result
108            report.setTotalTime(t);
109            report.setNumberOfCycles(numberOfCycles);
110            
111
112        } catch (Exception e) {
113            LogManager.getLogger().error(e);
114                }
115
116    }
117
118    
119    protected ArrayList<ArrayList<CBRCase>> _folds;
120
121    protected void createFolds(Collection<CBRCase> cases, int folds) {
122        _folds = new  ArrayList<ArrayList<CBRCase>>();
123        int foldsize = cases.size() / folds;
124        ArrayList<CBRCase> copy = new ArrayList<CBRCase>(cases);
125
126        for(int f=0; f<folds; f++) {
127
128            ArrayList<CBRCase> fold = new ArrayList<CBRCase>();
129            for(int i=0; (i<foldsize)&&(copy.size()>0); i++) {
130
131                int random = (int) (Math.random() * copy.size());
132                CBRCase _case = copy.get( random );
133                copy.remove(random);
134                fold.add(_case);
135            }
136            _folds.add(fold);
137        }
138    }
139    
140    protected void getFolds(int f, List<CBRCase> querySet, List<CBRCase> caseBaseSet) {
141        querySet.clear();
142        caseBaseSet.clear();
143        
144        querySet.addAll(_folds.get(f));
145        
146        for(int i=0; i<_folds.size(); i++)
147            if(i!=f)
148                caseBaseSet.addAll(_folds.get(i));
149    }
150    
151}