NodalMutation extends MutationOperation { @Override public void doOperation(Individual operand) { ContextualDerivationTree tree = (ContextualDerivationTree) GenotypeHelper.buildDerivationTree(operand); GEChromosome chromosome = (GEChromosome)operand.getGenotype().get(0); if(tree!=null) { ArrayList<Integer> nodeCodonList = new ArrayList(tree.getNodeCodonList()); for(int i =0;i< nodeCodonList.size();i++) { int codonIndex = nodeCodonList.get(i); if(this.rng.nextBoolean(this.probability)) { chromosome.set(codonIndex, Math.abs(rng.nextInt())); } } ((GEIndividual)operand).invalidate(); tree = null; } } NodalMutation(RandomNumberGenerator rng, Properties p);  NodalMutation(double prob, RandomNumberGenerator rng); void setCreationOperation(CreationOperation creationOperation); @Override void doOperation(Individual operand); @Override void doOperation(List<Individual> operands);  }
@Test public void testDoOperation_Individual() { System.out.println("Nodal muation doOperation"); GEIndividual operand = IndividualMaker.makeIndividual(p); int[] chromosome = {0,1,2}; int[] expected = {0,1,111352301}; GEChromosome geChromosome = (GEChromosome)operand.getGenotype().get(0); geChromosome.setAll(chromosome); NodalMutation instance = new NodalMutation(0.5, new MersenneTwisterFast(2)); instance.doOperation(operand); JUnitHelper.checkArrays(expected, geChromosome.data); GEIndividualTest.testInvalidated(operand); geChromosome = null; try { instance.doOperation(operand); } catch(NullPointerException e) { assertTrue(true); } GEIndividualTest.testInvalidated(operand); }
@Test public void testDoOperation_codonList() { System.out.println("Nodal mutation codonlist"); GEIndividual operand = IndividualMaker.makeIndividual(p); int[] chromosome = {1,1,2,1,1,2,4,6,7,8,9,9,9,0,5,4,3}; GEChromosome geChromosome = (GEChromosome)operand.getGenotype().get(0); geChromosome.setAll(chromosome); ContextualDerivationTree tree = (ContextualDerivationTree) GenotypeHelper.buildDerivationTree(operand); System.out.println(tree.toString()); ArrayList<Integer> expected = tree.getNodeCodonList(); NodalMutation instance = new NodalMutation(0.5, new MersenneTwisterFast(2)); instance.doOperation(operand); tree = (ContextualDerivationTree) GenotypeHelper.buildDerivationTree(operand); ArrayList<Integer> result = tree.getNodeCodonList();; System.out.println("expected"+expected.toString()); System.out.println("result"+result.toString()); System.out.println(tree.toString()); JUnitHelper.checkArrays(expected, result); GEIndividualTest.testInvalidated(operand); }
@Test public void testDoOperation_GECodonValue() { GEIndividual operand = IndividualMaker.makeIndividual(p2); int[] chromosome = {1,2,1,1,2,2,0,0}; int[] expected = {1,2,1937831252,1,2,1748719057,0,0,}; GEChromosome geChromosome = (GEChromosome)operand.getGenotype().get(0); geChromosome.setAll(chromosome); System.out.println("Operand:"+operand); ContextualDerivationTree tree = (ContextualDerivationTree) GenotypeHelper.buildDerivationTree(operand); System.out.println("BEFORE "+operand.getGenotype()); NodalMutation instance = new NodalMutation(1.0, new MersenneTwisterFast(0)); instance.doOperation(operand); tree = (ContextualDerivationTree) GenotypeHelper.buildDerivationTree(operand); System.out.println("AFTER "+operand.getGenotype()); }