Exemplo n.º 1
0
	def generate(number=10,r=0.5):
		"""Function to generate and return a random tree with fixed number of internal nodes added.  
		The root has to be a scalarizing node.  Biasing of the tree towards (or away from) binary
		nodes can be controlled with the parameter r."""
		root = ScalarNode(CGAFunctions.DataMethodFactory().getScalar())
		tree = AlgorithmTree(root)
		while number > 0:
			tNode = random.choice(tree.getTermini())
			fNode = CGAGenerator._getRandomFunctionalNode(r)
			CGAGenerator._extend(tNode,fNode)
			number -= 1
		return tree	
Exemplo n.º 2
0
	def expgenerate(p,r=0.5):
		"""Generates and returns a random tree.  Current tree is extended at a *single* node with
		probability p, and process terminates with probability 1-p."""
		root = ScalarNode(CGAFunctions.DataMethodFactory().getScalar())
		tree = AlgorithmTree(root)
		extend = True
		while extend:
			tNode = random.choice(tree.getTermini())
			fNode = CGAGenerator._getRandomFunctionalNode(r)
			CGAGenerator._extend(tNode,fNode)
			if uniform() < p:
				extend = True
			else:
				extend = False
		return tree
Exemplo n.º 3
0
	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)
Exemplo n.º 4
0
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