SantaFeAntTrailInterpreter implements FitnessFunction { public void getFitness(Individual individual) { trail = getTrail(this.trail_type); double fitness = trail.getFood(); try { phenotype = individual.getPhenotype(); if (this.phenotype.size() > 0) { while (trail.get_Energy() > 0) { this.programCounter = 0; run(); } fitness = trail.getFitness(); } else { throw new IllegalArgumentException("Bad phenotype size"); } } catch (IllegalArgumentException e) { System.err.println(this.getClass().getName() + " Error geting fitness:" + e + "\n assining DEFAULT"); fitness = BasicFitness.DEFAULT_FITNESS; } catch (ArrayIndexOutOfBoundsException e) { System.err.println(this.getClass().getName() + " Error geting fitness:" + e + "\n assining DEFAULT"); fitness = BasicFitness.DEFAULT_FITNESS; } individual.getFitness().setDouble(fitness); } SantaFeAntTrailInterpreter(); void setTrail_type(String trail_type); void getFitness(Individual individual); boolean canCache(); void setProperties(Properties p);  }
@Test public void testGetFitness() { System.out.println("*SantaFeAntTrailInterpreter: getFitness"); GEIndividual individual = IndividualMaker.makeIndividual(); Phenotype p = IndividualMaker.getPhenotype("move();"); individual.setPhenotype(p); SantaFeAntTrailInterpreter instance = new SantaFeAntTrailInterpreter(); instance.setTrail_type("geva.FitnessEvaluation.SantaFeAntTrail.Trail"); instance.getFitness(individual); assertEquals((int) individual.getFitness().getDouble(), 86); individual = IndividualMaker.makeIndividual(); p.clear(); p.add(new Symbol(SantaFeAntTrailInterpreter.IF,Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.IF, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.MOVE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.ELSE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.LEFT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.ELSE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.RIGHT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); individual.setPhenotype(p); instance = new SantaFeAntTrailInterpreter(); instance.setTrail_type("geva.FitnessEvaluation.SantaFeAntTrail.Trail"); instance.getFitness(individual); assertEquals((int) individual.getFitness().getDouble(), 78); individual = IndividualMaker.makeIndividual(); p.clear(); p.add(new Symbol(SantaFeAntTrailInterpreter.IF,Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.MOVE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.ELSE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.LEFT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.MOVE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.RIGHT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); individual.setPhenotype(p); instance = new SantaFeAntTrailInterpreter(); instance.setTrail_type("geva.FitnessEvaluation.SantaFeAntTrail.Trail"); instance.getFitness(individual); assertEquals((int) individual.getFitness().getDouble(), 72); individual = IndividualMaker.makeIndividual(); p.clear(); p.add(new Symbol(SantaFeAntTrailInterpreter.IF,Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.MOVE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.ELSE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.IF,Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.RIGHT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.LEFT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.ELSE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.RIGHT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.IF,Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.RIGHT, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.ELSE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.MOVE, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); p.add(new Symbol(SantaFeAntTrailInterpreter.END_IF, Enums.SymbolType.TSymbol)); individual.setPhenotype(p); instance = new SantaFeAntTrailInterpreter(); instance.setTrail_type("geva.FitnessEvaluation.SantaFeAntTrail.Trail"); instance.getFitness(individual); assertEquals((int) individual.getFitness().getDouble(), 85); }