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; 009 010import java.util.ArrayList; 011import java.util.Collection; 012import java.util.Date; 013import java.util.List; 014 015 016/** 017 * This evaluation divides the case base into several random folds 018 * (indicated by the user). 019 * For each fold, their cases are used as queries and the remaining folds are 020 * used together as case base. Maintenance is performed on the case-base before 021 * running the queries. 022 * This process is performed several times. 023 * 024 * @author Lisa Cummins. 025 * @author Juan A. Recio Garc�a - GAIA http://gaia.fdi.ucm.es 026 */ 027public class MaintenanceNFoldEvaluator extends MaintenanceEvaluator 028{ 029 /** 030 * Executes the N-Fold evaluation. 031 * @param numFolds the number of randomly generated folds. 032 * @param repetitions the number of repetitions 033 */ 034 public void NFoldEvaluation(int numFolds, int repetitions) 035 { try 036 { //Get the time 037 long t = (new Date()).getTime(); 038 int numberOfCycles = 0; 039 040 // Run the precycle to load the case base 041 LogManager.getLogger().info("Running precycle()"); 042 CBRCaseBase caseBase = app.preCycle(); 043 044 if (!(caseBase instanceof CachedLinealCaseBase)) 045 LogManager.getLogger().warn("Evaluation should be executed using a cached case base"); 046 047 List<CBRCase> cases = new ArrayList<CBRCase>(caseBase.getCases()); 048 049 //For each repetition 050 for(int r=0; r<repetitions; r++) 051 { //Create the folds 052 ArrayList<ArrayList<CBRCase>> folds = createFolds(cases, numFolds); 053 054 //For each fold 055 for(int f=0; f<numFolds; f++) 056 { ArrayList<CBRCase> querySet = new ArrayList<CBRCase>(); 057 prepareCases(cases, querySet, f, caseBase, folds); 058 059 //Run cycle for each case in querySet (current fold) 060 for(CBRCase c: querySet) 061 { LogManager.getLogger().info( 062 "Running cycle() " + numberOfCycles); 063 app.cycle(c); 064 numberOfCycles++; 065 } 066 } 067 } 068 069 //Revert case base to original state 070 caseBase.forgetCases(cases); 071 caseBase.learnCases(cases); 072 073 //Run the poscycle to finish the application 074 LogManager.getLogger().info("Running postcycle()"); 075 app.postCycle(); 076 077 //Complete the evaluation result 078 report.setTotalTime(t); 079 report.setNumberOfCycles(numberOfCycles); 080 081 } catch (Exception e) 082 { LogManager.getLogger().error(e); 083 } 084 085 } 086 087 /** 088 * Prepares the cases for evaluation by setting up test and training sets 089 * @param originalCases Complete original set of cases 090 * @param querySet Where queries are to be stored 091 * @param fold The fold number 092 * @param caseBase The case base 093 */ 094 protected void prepareCases(Collection<CBRCase> originalCases, List<CBRCase> querySet, 095 int fold, CBRCaseBase caseBase, ArrayList<ArrayList<CBRCase>> folds) 096 { ArrayList<CBRCase> caseBaseSet = new ArrayList<CBRCase>(); 097 098 //Obtain the query and casebase sets 099 getFolds(fold, querySet, caseBaseSet, folds); 100 101 //Clear the caseBase 102 caseBase.forgetCases(originalCases); 103 104 //Set the cases that acts as casebase in this cycle 105 caseBase.learnCases(caseBaseSet); 106 107 if(this.simConfig != null && this.editMethod != null) 108 { // Perform maintenance on this case base 109 editCaseBase(caseBase); 110 } 111 } 112 113 /** 114 * Divides the given cases into the given number of folds. 115 * @param cases the original cases. 116 * @param numFolds the number of folds. 117 */ 118 protected ArrayList<ArrayList<CBRCase>> createFolds(Collection<CBRCase> cases, int numFolds) 119 { ArrayList<ArrayList<CBRCase>> folds = new ArrayList<ArrayList<CBRCase>>(); 120 int foldsize = cases.size() / numFolds; 121 ArrayList<CBRCase> copy = new ArrayList<CBRCase>(cases); 122 123 for(int f=0; f<numFolds; f++) 124 { ArrayList<CBRCase> fold = new ArrayList<CBRCase>(); 125 for(int i=0; (i<foldsize)&&(copy.size()>0); i++) 126 { int random = (int) (Math.random() * copy.size()); 127 CBRCase _case = copy.get( random ); 128 copy.remove(random); 129 fold.add(_case); 130 } 131 folds.add(fold); 132 } 133 return folds; 134 } 135 136 /** 137 * Clears the current query and case base sets and populates the query set with fold 138 * f and the case base set with the cases not contained in fold f. 139 * @param f the fold to use. 140 * @param querySet the set of queries. 141 * @param caseBaseSet the set of cases. 142 */ 143 public static void getFolds(int f, List<CBRCase> querySet, List<CBRCase> caseBaseSet, ArrayList<ArrayList<CBRCase>> folds) 144 { querySet.clear(); 145 caseBaseSet.clear(); 146 147 querySet.addAll(folds.get(f)); 148 149 for(int i=0; i<folds.size(); i++) 150 if(i!=f) 151 caseBaseSet.addAll(folds.get(i)); 152 } 153}