001 /** 002 * LeaveOneOutEvaluator.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-García. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 07/05/2007 008 */ 009 package jcolibri.evaluation.evaluators; 010 011 import java.util.ArrayList; 012 import java.util.Date; 013 014 import jcolibri.cbraplications.StandardCBRApplication; 015 import jcolibri.cbrcore.CBRCase; 016 import jcolibri.cbrcore.CBRCaseBase; 017 import jcolibri.evaluation.EvaluationReport; 018 import jcolibri.evaluation.Evaluator; 019 import jcolibri.exception.ExecutionException; 020 021 import org.apache.commons.logging.LogFactory; 022 023 /** 024 * This methods uses all the cases as queries. 025 * It executes so cycles as cases in the case base. 026 * In each cycle one case is used as query. 027 * 028 * @author Juan A. Recio García - GAIA http://gaia.fdi.ucm.es 029 * @version 2.0 030 */ 031 public class LeaveOneOutEvaluator extends Evaluator { 032 033 protected StandardCBRApplication app; 034 035 public void init(StandardCBRApplication cbrApp) { 036 037 report = new EvaluationReport(); 038 app = cbrApp; 039 try { 040 app.configure(); 041 } catch (ExecutionException e) { 042 LogFactory.getLog(this.getClass()).error(e); 043 } 044 } 045 046 /** 047 * Performs the Leave-One-Out evaluation. 048 * For each case in the case base, remove that case from the case base and use it as query for that cycle. 049 */ 050 public void LeaveOneOut() { 051 try { 052 java.util.ArrayList<CBRCase> aux = new java.util.ArrayList<CBRCase>(); 053 054 long t = (new Date()).getTime(); 055 int numberOfCycles = 0; 056 057 // Run the precycle to load the case base 058 LogFactory.getLog(this.getClass()).info("Running precycle()"); 059 CBRCaseBase caseBase = app.preCycle(); 060 061 if (!(caseBase instanceof jcolibri.casebase.CachedLinealCaseBase)) 062 LogFactory 063 .getLog(this.getClass()) 064 .warn( 065 "Evaluation should be executed using a cached case base"); 066 067 068 ArrayList<CBRCase> cases = new ArrayList<CBRCase>(caseBase.getCases()); 069 070 jcolibri.util.ProgressController.init(getClass(),"LeaveOneOut Evaluation", cases.size()); 071 072 //For each case in the case base 073 for(CBRCase _case : cases) { 074 075 //Delete the case in the case base 076 aux.clear(); 077 aux.add(_case); 078 caseBase.forgetCases(aux); 079 080 //Run the cycle 081 LogFactory.getLog(this.getClass()).info( 082 "Running cycle() " + numberOfCycles); 083 app.cycle(_case); 084 085 //Recover case base 086 caseBase.learnCases(aux); 087 088 numberOfCycles++; 089 jcolibri.util.ProgressController.step(getClass()); 090 } 091 092 //Run PostCycle 093 LogFactory.getLog(this.getClass()).info("Running postcycle()"); 094 app.postCycle(); 095 096 jcolibri.util.ProgressController.finish(getClass()); 097 098 t = (new Date()).getTime() - t; 099 100 //complete evaluation report 101 report.setTotalTime(t); 102 report.setNumberOfCycles(numberOfCycles); 103 104 } catch (ExecutionException e) { 105 LogFactory.getLog(this.getClass()).error(e); 106 e.printStackTrace(); 107 } 108 } 109 110 //----------------------------------------------------------------------------// 111 112 113 114 }