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