def testCrossover(self): print "\n\n----- testing single_crossover(tree1Node, tree2Node) -----" root = ScalarNode(self.methodFactory.getScalar('tr')) node1 = UnaryNode(self.methodFactory.getUnary('log')) node2 = BinaryNode(self.methodFactory.getBinary('multiply')) constant1 = DataNode(self.methodFactory.getData('1/N')) constant2 = DataNode(self.methodFactory.getData('e')) tree = AlgorithmTree(root) root.setChildren(node1) node1.setChildren(node2) node2.setChildren(constant1,constant2) print 'Trees before the swap (node,id): ' print 'Tree One:' self.testTree() print 'Tree Two:' tree() CGAGenerator.single_crossover(self.node2, node1) print 'Trees after the swap (node,id): ' print 'Tree One:' self.testTree() print 'Tree Two:' tree()
def setUp(self): self.methodFactory = CGAFunctions.DataMethodFactory() # set up the tree self.root = ScalarNode(self.methodFactory.getScalar('sum_ij')) self.node1 = BinaryNode(self.methodFactory.getBinary('add')) self.node2 = BinaryNode(self.methodFactory.getBinary('subtract')) self.node3 = UnaryNode(self.methodFactory.getUnary('log')) # immutable data - stored self.constant1 = DataNode(self.methodFactory.getData('e')) self.constant2 = DataNode(self.methodFactory.getData('pi')) self.constant3 = DataNode(self.methodFactory.getData('1/N')) # set up the tree self.testTree = AlgorithmTree(self.root) self.root.setChildren(self.node1) self.node1.setChildren(self.node2,self.node3) self.node2.setChildren(self.constant1,self.constant2) self.node3.setChildren(self.constant3)
class CGAGeneratorTests(unittest.TestCase): def setUp(self): self.methodFactory = CGAFunctions.DataMethodFactory() # set up the tree self.root = ScalarNode(self.methodFactory.getScalar('sum_ij')) self.node1 = BinaryNode(self.methodFactory.getBinary('add')) self.node2 = BinaryNode(self.methodFactory.getBinary('subtract')) self.node3 = UnaryNode(self.methodFactory.getUnary('log')) # immutable data - stored self.constant1 = DataNode(self.methodFactory.getData('e')) self.constant2 = DataNode(self.methodFactory.getData('pi')) self.constant3 = DataNode(self.methodFactory.getData('1/N')) # set up the tree self.testTree = AlgorithmTree(self.root) self.root.setChildren(self.node1) self.node1.setChildren(self.node2,self.node3) self.node2.setChildren(self.constant1,self.constant2) self.node3.setChildren(self.constant3) def testExtend(self): print "\n\n----- testing extension -----" print 'Tree before extension (node,id): ' self.testTree() newNode = BinaryNode(self.methodFactory.getBinary('multiply')) CGAGenerator._extend(self.constant1,newNode) print 'Tree after extension (node,id): ' self.testTree() def testGrow(self): print "\n\n----- testing grow(tree,tNode) -----" print "Tree before extension (node,id): " self.testTree() CGAGenerator.grow(self.testTree,self.constant2) print "Tree after extension (node,id): " self.testTree() def testDelete(self): print "\n\n----- testing deletion -----" print 'Tree before deletion (node, id): ' self.testTree() CGAGenerator._delete(self.node2) print 'Tree after deletion (node, id): ' self.testTree() def testPrune(self): print "\n\n----- testing prune(tree,node) -----" print "Tree before pruning (node,id): " self.testTree() CGAGenerator.prune(self.testTree,self.node3) print "Tree after pruning (node,id): " self.testTree() def testNonRootReplace(self): print "\n\n----- testing non-root replacement -----" print 'Tree before replacement (node,id): ' self.testTree() newNode = BinaryNode(self.methodFactory.getBinary('multiply')) CGAGenerator._replace(self.testTree, self.node1, newNode) print 'Tree after replacement (node,id): ' self.testTree() def testRootReplace(self): print "\n\n----- testing replacement of root node -----" print 'Tree before root replacement (node,id): ' self.testTree() print 'Root : ', self.testTree.root print 'Root header : ', self.testTree.root.getHeader() newNode = ScalarNode(self.methodFactory.getScalar('tr')) CGAGenerator._replace(self.testTree,self.testTree.root,newNode) print 'Tree after root replacement (node, id): ' self.testTree() print 'Root : ', self.testTree.root print 'Root header : ', self.testTree.root.getHeader() def testSwap(self): print "\n\n----- testing subtree swap -----" root = ScalarNode(self.methodFactory.getScalar('tr')) node1 = UnaryNode(self.methodFactory.getUnary('log')) node2 = BinaryNode(self.methodFactory.getBinary('multiply')) constant1 = DataNode(self.methodFactory.getData('1/N')) constant2 = DataNode(self.methodFactory.getData('e')) tree = AlgorithmTree(root) root.setChildren(node1) node1.setChildren(node2) node2.setChildren(constant1,constant2) print 'Trees before the swap (node,id): ' print 'Tree One:' self.testTree() print 'Tree Two:' tree() CGAGenerator._swap(self.node2, node1) print 'Trees after the swap (node,id): ' print 'Tree One:' self.testTree() print 'Tree Two:' tree() def testCrossover(self): print "\n\n----- testing single_crossover(tree1Node, tree2Node) -----" root = ScalarNode(self.methodFactory.getScalar('tr')) node1 = UnaryNode(self.methodFactory.getUnary('log')) node2 = BinaryNode(self.methodFactory.getBinary('multiply')) constant1 = DataNode(self.methodFactory.getData('1/N')) constant2 = DataNode(self.methodFactory.getData('e')) tree = AlgorithmTree(root) root.setChildren(node1) node1.setChildren(node2) node2.setChildren(constant1,constant2) print 'Trees before the swap (node,id): ' print 'Tree One:' self.testTree() print 'Tree Two:' tree() CGAGenerator.single_crossover(self.node2, node1) print 'Trees after the swap (node,id): ' print 'Tree One:' self.testTree() print 'Tree Two:' tree() def testGenerate(self): print "\n\n----- testing generate(tree) -----" tree = CGAGenerator.generate(3) tree() def testExpGenerate(self): print "\n\n----- testing expgenerate(tree) -----" tree = CGAGenerator.expgenerate(0.85) tree() def testPointMutate(self): print "\n\n----- testing point_mutate(tree,node) -----" print "Tree before mutation (node,id): " self.testTree() CGAGenerator.point_mutate(self.testTree,random.choice(self.testTree.getNodes())) print "Tree after mutation (node,id): " self.testTree() def testRepeatedPointMutate(self): print "\n\n----- testing repeated runs of point_mutate(tree,node) -----" print "Tree before mutation (node,id): " self.testTree() for i in xrange(0,10000): CGAGenerator.point_mutate(self.testTree, random.choice(self.testTree.getNodes())) print "Tree after mutation (node,id): " self.testTree() def testSpecialTreeGeneration(self): print "\n\n----- testing generation of 'special' trees -----" treeset = ['MI','OMES','DOES NOT EXIST'] for t in treeset: tree = CGAGenerator.generate_special_tree(t) if tree is not None: print '%s : %s', (t,tree.getString()) else: print 'Tree %s does not exist.' % t
def generate_special_tree(treename): """Generates and returns a "special" tree. Allowed treenames are: ['MI','OMES'].""" if treename == 'MI': root = ScalarNode(CGAFunctions.DataMethodFactory().getScalar('sum_ij')) n1 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('subtract')) n2 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) n3 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('add')) d1 = DataNode(CGAFunctions.DataMethodFactory().getData('p_ij')) n4 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('log')) n5 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) n6 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) d2 = DataNode(CGAFunctions.DataMethodFactory().getData('p_ij')) d3 = DataNode(CGAFunctions.DataMethodFactory().getData('1/N')) n7 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) d4 = DataNode(CGAFunctions.DataMethodFactory().getData('1/N')) n8 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) d5 = DataNode(CGAFunctions.DataMethodFactory().getData('p_i')) n9 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('log')) n10 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('transpose')) n11 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('log')) d6 = DataNode(CGAFunctions.DataMethodFactory().getData('p_i')) d7 = DataNode(CGAFunctions.DataMethodFactory().getData('p_j')) n12 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('transpose')) d8 = DataNode(CGAFunctions.DataMethodFactory().getData('p_j')) # set up the tree specialTree = AlgorithmTree(root) root.setChildren(n1) n1.setChildren(n2,n3) n2.setChildren(d1,n4) n4.setChildren(d2) n3.setChildren(n5,n6) n5.setChildren(d3,n7) n6.setChildren(d4,n8) n7.setChildren(d5,n9) n8.setChildren(n10,n11) n9.setChildren(d6) n10.setChildren(d7) n11.setChildren(n12) n12.setChildren(d8) elif treename == 'OMES': root = ScalarNode(CGAFunctions.DataMethodFactory().getScalar('sum_ij')) n1 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('divide')) n2 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('square')) n3 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) n4 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('subtract')) d1 = DataNode(CGAFunctions.DataMethodFactory().getData('p_i')) n5 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('transpose')) d2 = DataNode(CGAFunctions.DataMethodFactory().getData('p_ij')) n6 = BinaryNode(CGAFunctions.DataMethodFactory().getBinary('multiply')) d3 = DataNode(CGAFunctions.DataMethodFactory().getData('p_j')) d4 = DataNode(CGAFunctions.DataMethodFactory().getData('p_i')) n7 = UnaryNode(CGAFunctions.DataMethodFactory().getUnary('transpose')) d5 = DataNode(CGAFunctions.DataMethodFactory().getData('p_j')) # set up the tree specialTree = AlgorithmTree(root) root.setChildren(n1) n1.setChildren(n2,n3) n2.setChildren(n4) n3.setChildren(d1,n5) n4.setChildren(d2,n6) n5.setChildren(d3) n6.setChildren(d4,n7) n7.setChildren(d5) else: specialTree = None return specialTree