SymbolicRegressionIntrepreter implements FitnessFunction, Stochastic { public void getFitness(final Individual ind) { double fitness = 0; try { final String phenotype = ind.getPhenotype().getString(); this.program = phenotype.split(" "); if (this.range != null && this.range.isRandom()) { this.samples = this.range.getSamples(); } for (int i = 0; i < samples.length; i++) { for (int j = 0; j < x.length; j++) { x[j] = samples[i][j]; } this.programCounter = 0; final double runValue = this.run(); fitness += Math.pow(runValue - calculated_target[i], 2.0); } } 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; } ind.getFitness().setDouble(fitness); } SymbolicRegressionIntrepreter(); Range getRange(); void setRange(Range range); void calculateTarget(final String target); void setProperties(Properties p); RandomNumberGenerator getRNG(); void setRNG(RandomNumberGenerator m); void getFitness(final Individual ind); boolean canCache(); void setSamples(double[][] samples); void setX(double[] x);  }
@Test public void testGetFitness() { System.out.println("*SymbolicRegressionInterpreterTest: getFitness"); gei.getPhenotype().add(new Symbol("+", Enums.SymbolType.TSymbol)); try{ instance.getFitness(gei); } catch(ArrayIndexOutOfBoundsException e) { assertTrue(true); } assertEquals(true, BasicFitness.DEFAULT_FITNESS == gei.getFitness().getDouble()); gei = IndividualMaker.makeIndividual(); gei.getPhenotype().add(new Symbol("++", Enums.SymbolType.TSymbol)); try{ instance.getFitness(gei); } catch(IllegalArgumentException e) { assertTrue(true); } assertEquals(true, BasicFitness.DEFAULT_FITNESS==gei.getFitness().getDouble()); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("+", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x0", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x1", Enums.SymbolType.TSymbol)); double expected = (5 - 6) * (5 - 6) + (7 - 12) * (7 - 12); instance.calculateTarget("* x0 x1"); instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("-", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("-1.5", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("0.5", Enums.SymbolType.TSymbol)); expected = Math.pow(-2 - 6,2) + Math.pow(-2 - 12, 2); instance.calculateTarget("* x0 x1"); instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("/", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("2", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("0", Enums.SymbolType.TSymbol)); expected = Math.pow(2 - 6,2) + Math.pow(2 - 12,2); instance.calculateTarget("* x0 x1"); instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("+", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("*", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x0", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("-", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x1", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x0", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x0", Enums.SymbolType.TSymbol)); expected = Math.pow(4 - 6,2) + Math.pow(6 - 12,2); instance.calculateTarget("* x0 x1"); instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("/", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("rnd", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x0", Enums.SymbolType.TSymbol)); MersenneTwisterFast mft = new MersenneTwisterFast(0); MersenneTwisterFast mft2 = new MersenneTwisterFast(0); instance.setRNG(mft2); double rndDouble = mft.nextDouble(); double rndDouble2 = mft.nextDouble(); expected = Math.pow(rndDouble/2 - 6, 2) + Math.pow(rndDouble2/3 - 12,2); instance.calculateTarget("* x0 x1"); instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("+", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x0", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("x1", Enums.SymbolType.TSymbol)); expected = 138.85324825; instance.setRange(new Range("x0 eq rnd(0,2,1); x1 eq rnd(0,2,1)", new MersenneTwisterFast(0))); instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); expected = 135.8672430; instance.getFitness(gei); assertEquals(expected, gei.getFitness().getDouble(),0.00001); }