GrowInitialiser implements CreationOperation, Stochastic { public boolean grow(NimbleTree<Symbol> dt) { Rule rule; Iterator<Production> prodIt; ArrayList<Integer> possibleRules = new ArrayList<Integer>(); 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) { return false; } rule = grammar.findRule(dt.getCurrentNode().getData()); if (rule != null) { prodIt = rule.iterator(); possibleRules.clear(); int ii = 0; while (prodIt.hasNext()) { prod = prodIt.next(); if ((dt.getCurrentLevel() + prod.getMinimumDepth()) <= this.maxDepth) { possibleRules.add(ii); } ii++; } if (possibleRules.isEmpty()) { 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. Could not find" + dt.getCurrentNode().getData().getSymbolString()); } return true; } } catch(InitializationException e) { logger.error("Exception initializing", e); return false; } } GrowInitialiser(RandomNumberGenerator rng, GEGrammar gegrammar, int maxDepth);  GrowInitialiser(RandomNumberGenerator rng, GEGrammar gegrammar, Properties p); void setRNG(RandomNumberGenerator m); RandomNumberGenerator getRNG(); void setProperties(Properties p); Individual createIndividual(); int getMinDepth(); void setMinDepth(int minDepth); void setMaxDepth(int i); int getMaxDepth(); void doOperation(Individual operand); void doOperation(List<Individual> operands); Genotype getGenotype(int maxLength); boolean grow(NimbleTree<Symbol> dt);  }
@Test public void testGrow() { System.out.println("grow"); 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(); gi1.genotype = new Genotype(); gi1.chromosome = new GEChromosome(100); gi1.chromosome.setMaxChromosomeLength(1000); gi1.genotype.add(gi1.chromosome); boolean result = gi1.grow(dt); assertEquals(true, result); assertEquals(true, expResult < dt.getDepth()); }