PictureCopy implements FitnessFunction { public void getFitness(Individual i) { double result; String p = i.getPhenotype().getString(); if (ShapeGrammarSwingPainter.charCount(p, '(') != ShapeGrammarSwingPainter.charCount(p, ')') || ShapeGrammarSwingPainter.charCount(p, '[') != ShapeGrammarSwingPainter.charCount(p, ']') || (p.indexOf("sqr") == -1 && p.indexOf("tri") == -1 && p.indexOf("crcl") == -1)) { i.getFitness().setDouble(BasicFitness.DEFAULT_FITNESS); return; } picture.paint(i.getPhenotype().getString()); result = picture.evaluate(); picture.invalidate(); i.getFitness().setDouble(result); } PictureCopy();  PictureCopy(Properties p); void setProperties(Properties p); boolean canCache(); void getFitness(Individual i); void init(); void init(String name); static void main(String [] args); public Painting picture; }
@Test public void testGetFitness() { System.out.println("getFitness"); String target = "[ UpDwn_( -20 ) symX_( 100 ) tri ] gro_( 2 ) gro_( 2 ) UpDwn_( -20 ) UpDwn_( -20 ) LftRght_( -20 ) sqr"; String input = "UpDwn_( -20 ) symX_( 100 ) sqr"; Properties p = new Properties(); p.setProperty(Constants.TARGET_PHENOTYPE,target); PictureCopy instance = new PictureCopy(p); instance.init(); gei.getPhenotype().clear(); gei.getPhenotype().add(new Symbol("UpDwn_( -20 )", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("symX_( 100 )", Enums.SymbolType.TSymbol)); gei.getPhenotype().add(new Symbol("sqr", Enums.SymbolType.TSymbol)); System.out.println(gei.getPhenotype().getString()); instance.getFitness(gei); assertEquals(true,gei.getFitness().getDouble()==1655); }