FullInitialiser extends GrowInitialiser { @Override public boolean grow(NimbleTree<Symbol> dt) { Rule rule; Iterator<Production> prodIt; ArrayList<Integer> possibleRules = new ArrayList<Integer>(); boolean recursiveRules; Production prod; int prodVal; boolean result; int newMaxDepth; Iterator<Symbol> symIt; Symbol symbol; try { if(dt.getCurrentNode().getData().getType() == Enums.SymbolType.TSymbol) { if(dt.getCurrentNode().getData().getSymbolString().contains("BRR")) { this.extraCodons++; } return true; } if(dt.getCurrentLevel() > this.maxDepth) { logger.warn("Too deep:"+dt.getCurrentLevel()+">"+this.maxDepth); return false; } rule = grammar.findRule(dt.getCurrentNode().getData()); if(rule!=null){ prodIt = rule.iterator(); possibleRules.clear(); int ii = 0; recursiveRules = false; while(prodIt.hasNext()) { prod = prodIt.next(); if((dt.getCurrentLevel()+prod.getMinimumDepth()) <= this.maxDepth) { if(!recursiveRules && prod.getRecursive()) { recursiveRules = true; possibleRules.clear(); } if(!recursiveRules || (recursiveRules && prod.getRecursive())) { possibleRules.add(ii); } } ii++; } if(possibleRules.isEmpty()) { logger.info("EmptyPossible rules:"+rule); return false; } else { prodVal = this.rng.nextInt(possibleRules.size()); int modVal = possibleRules.get(prodVal); int tmp1 = this.rng.nextInt((Integer.MAX_VALUE-rule.size())); int tmp; int mod = tmp1%rule.size(); int diff; if(mod>modVal) { diff = mod - modVal; tmp = tmp1 - diff; } else { diff = modVal - mod; tmp = tmp1 + diff; } int newMod = tmp%rule.size(); if(newMod!=modVal) { logger.info("modVal:"+modVal+" tmp1:"+tmp1+" mod:"+mod+" tmp:"+tmp+" rule.size():"+rule.size()+" newMod:"+newMod); } if(rule.size() > 1) { this.chromosome.add(tmp); prod = rule.get(possibleRules.get(prodVal)); } else { prod = rule.get(0); } } result = true; newMaxDepth = dt.getDepth(); symIt = prod.iterator(); while(symIt.hasNext() && result) { symbol = symIt.next(); dt.addChild(symbol); dt.setCurrentNode(dt.getCurrentNode().getEnd()); dt.setCurrentLevel(dt.getCurrentLevel() + 1); result = grow(dt); dt.setCurrentLevel(dt.getCurrentLevel() - 1); if(newMaxDepth < dt.getDepth()) { newMaxDepth = dt.getDepth(); } } chromosome.setValid(result); dt.setDepth(newMaxDepth); return result; } else { if(!checkGECodonValue(dt)) { throw new InitializationException("Non-existent rule, maybe GECODON not yet impelemnted"); } return true; } } catch(InitializationException e) { e.printStackTrace(); return false; } } FullInitialiser(RandomNumberGenerator rng, GEGrammar gegrammar, int maxDepth);  FullInitialiser(RandomNumberGenerator rng, GEGrammar gegrammar, Properties p); @Override boolean grow(NimbleTree<Symbol> dt);  }
@Test public void testGrow() { NimbleTree<Symbol> dt = new NimbleTree<Symbol>(); TreeNode<Symbol> tn = new TreeNode<Symbol>(); tn.setData(geg.getStartSymbol()); dt.populateStack(); dt.setRoot(tn); dt.setCurrentNode(dt.getRoot()); int expResult = dt.getDepth(); fi1.genotype = new Genotype(); fi1.chromosome = new GEChromosome(100); fi1.chromosome.setMaxChromosomeLength(1000); fi1.genotype.add(fi1.chromosome); boolean result = fi1.grow(dt); assertEquals(true, result); assertEquals(true, expResult < dt.getDepth()); }