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.extensions.maintenance_evaluation.MaintenanceEvaluator;
009import es.ucm.fdi.gaia.jcolibri.util.FileIO;
010import es.ucm.fdi.gaia.jcolibri.util.ProgressController;
011
012import java.io.BufferedReader;
013import java.io.FileReader;
014import java.util.ArrayList;
015import java.util.Date;
016import java.util.Iterator;
017import java.util.List;
018
019
020/**
021 * This evaluation splits the case base in two sets: one used for testing where each
022 * case is used as query, and another that acts as normal case base.
023 * It uses queries from a file so that the evaluation can be repeated with the 
024 * same test/training set split.
025 * The generateSplit() method does the initial random split and saves the query set in a file. 
026 * Later, the  HoldOutfromFile() method uses that file to load the query set and 
027 * perform the evaluation.
028 * 
029 * @author Juan A. Recio Garc�a & Lisa Cummins
030 */
031public class MaintenanceSameSplitEvaluator extends MaintenanceEvaluator {
032
033        /**
034         * Perform HoldOut evaluation using the queries contained in the specified file.
035         * @param file the file containing the queries.
036         */
037        public void HoldOutfromFile(String file) 
038        {       try 
039                {       // Obtain the time
040                        long t = (new Date()).getTime();
041                        int numberOfCycles = 0;
042
043                        // Run the precycle to load the case base
044                        LogManager.getLogger().info("Running precycle()");
045                        CBRCaseBase caseBase = app.preCycle();
046
047                        if (!(caseBase instanceof CachedLinealCaseBase))
048                                LogManager.getLogger().warn("Evaluation should be executed using a cached case base");
049
050                        List<CBRCase> originalCases = new ArrayList<CBRCase>(caseBase.getCases());
051                        List<CBRCase> querySet = new ArrayList<CBRCase>();
052
053                        prepareCases(originalCases, querySet, file, caseBase);
054                        
055                        int totalSteps = querySet.size();
056                        ProgressController.init(getClass(), "Same Split - Hold Out Evaluation", totalSteps);
057                        
058                        // Run cycle for each case in querySet
059                        for (CBRCase c : querySet) {
060                                // Run the cycle
061                                LogManager.getLogger().info("Running cycle() " + numberOfCycles);
062
063                        //      report.storeQueryNum();
064
065                                app.cycle(c);
066
067                                ProgressController.step(getClass());
068                                numberOfCycles++;
069                        }
070
071                        ProgressController.finish(getClass());
072
073                        // Revert case base to original state
074                        caseBase.forgetCases(originalCases);
075                        caseBase.learnCases(originalCases);
076
077                        // Run the poscycle to finish the application
078                        LogManager.getLogger().info("Running postcycle()");
079                        app.postCycle();
080
081                        t = (new Date()).getTime() - t;
082
083                        // Obtain and complete the evaluation result
084                        report.setTotalTime(t);
085                        report.setNumberOfCycles(numberOfCycles);
086
087                } catch (Exception e) 
088                {       LogManager.getLogger().error(e);
089                }
090        }
091        
092        
093        /**
094         * Prepares the cases for evaluation by setting up test and training sets
095         * @param originalCases Complete original set of cases
096         * @param querySet Where queries are to be stored
097         * @param caseBase The case base
098         */
099        protected void prepareCases(List<CBRCase> originalCases, List<CBRCase> querySet,
100                String file, CBRCaseBase caseBase)
101        {       
102                ArrayList<CBRCase> caseBaseSet = new ArrayList<CBRCase>();
103
104            // Split the case base
105                splitCaseBaseFromFile(originalCases, querySet, caseBaseSet, file);
106
107                // Clear the caseBase
108                caseBase.forgetCases(originalCases);
109
110                // Set the cases that acts as case base in this repetition
111                caseBase.learnCases(caseBaseSet);
112                
113                if(this.simConfig != null && this.editMethod != null)
114                {       // Perform maintenance on this case base
115                        editCaseBase(caseBase);
116                }
117        }
118        
119        /**
120         * Splits the case base in two sets: queries and case base, with the
121         * queries contained in the given file
122         * 
123         * @param wholeCaseBase
124         *            Complete original case base
125         * @param querySet
126         *            Output param where queries are stored
127         * @param casebaseSet
128         *            Output param where case base is stored
129         * @param filename
130         *            File which contains the queries
131         */
132        public static void splitCaseBaseFromFile(List<CBRCase> wholeCaseBase,
133                List<CBRCase> querySet, List<CBRCase> casebaseSet, String filename)
134        {       querySet.clear();
135                casebaseSet.clear();
136                
137                casebaseSet.addAll(wholeCaseBase);
138                
139                try 
140                {       BufferedReader br = null;
141                        br = new BufferedReader(new FileReader(FileIO.findFile(filename).getFile()));
142                        //if (br == null)
143                        //      throw new Exception("Error opening file: " + filename);
144        
145                        String line = "";
146                        while ((line = br.readLine()) != null) 
147                        {       CBRCase c = null;
148                                int pos=0;
149                                boolean found = false;
150                                for(Iterator<CBRCase> iter = casebaseSet.iterator(); iter.hasNext() && (!found); )
151                                {       c = iter.next();
152                                        if(c.getID().toString().equals(line))
153                                                found = true;
154                                        else
155                                                pos++;
156                                }
157                                if(c==null)
158                                {       System.out.println("Case "+line+" not found into case base");
159                                        continue;
160                                }
161                                
162                                casebaseSet.remove(pos);
163                                querySet.add(c);
164                        }
165                        br.close();
166                } catch (Exception e)
167                {       System.out.println(e);
168                }
169        }
170}