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