Example #1
0
   def test_createAndLableTree(self):
       ''' Create new tree with new ids starting from 0'''
       print "---------- test createAndLabelTree 1--------"
       no1dN = []
       points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
       util.splitN(points, 0, 0, 6, no1dN)
       tree1 = kdtree.createNewTree(no1dN)
       
       label=0
       for n in kdtree.level_order(tree1):
           self.assertIsNotNone(kdtree.getNode(tree1, label), "1: node with label: "+ str(label) + " not found in tree")
           label+=1
       kdtree.visualize(tree1)   
           
       print "---------- test createAndLabelTree 2--------"
       no2dN = []
       points = numpy.array([[0.0, 0.01], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
       util.splitN(points, 0, 0, 6, no2dN)
       tree2 = kdtree.createNewTree(no2dN)
       kdtree.visualize(tree2)  
       
       label=0
       for n in kdtree.level_order(tree2):
           self.assertIsNotNone(kdtree.getNode(tree2, label), "2: node with label: "+ str(label) + " not found in tree")
           label+=1
 
       self.assertNotEqual(tree1, tree2, "trees have to be different")
 def test_compare_old_to_new_method_to_create_trees(self):
     """ 
     tree created with old method should be equal to tree created with new method
     """
     nodes = util.generate_sequence_of_points(2, 2)
     tree1 = kdtree.createNewTree(nodes)
     kdtree.visualize(tree1)
     
     sel_axis = (lambda axis: axis)
     tree2 = kdtree.createNewTree([[0.5, 0.5]],axis = 0, sel_axis= sel_axis)
     tree2.split2([0.25, 0.5], axis = 1)
     tree2.split2([0.75, 0.5], axis = 1)
     
     #left
     tree2.split2([0.25, 0.25], axis = 0, sel_axis = sel_axis)
     tree2.split2([0.25, 0.75], axis = 0, sel_axis = sel_axis)
      
     #right
     tree2.split2([0.75, 0.25], axis = 0, sel_axis = sel_axis)
     tree2.split2([0.75, 0.75], axis = 0, sel_axis = sel_axis)
     
     kdtree.visualize(tree2)
     
     for n in zip(kdtree.level_order(tree1), kdtree.level_order(tree2)):
         self.assertEqual(n[0].data, n[1].data, "elements not equal")
         
         if n[0].data is not None and n[1].data is not None:
             self.assertEqual(n[0].axis, n[1].axis, "elements not equal")
Example #3
0
 def test_add_node_and_pickle_tree(self):
     print "-------------- test_pickle_tree ---------------"
     nodes = []
     netDimension = 3
     levels = 3
     sequence = ["".join(seq) for seq in itertools.product("01", repeat=netDimension)]
     points_temp= numpy.array([list(s) for s in sequence])
     points = numpy.array([map(float, f) for f in points_temp])
     
     util.splitN(points, 0, 0, levels, nodes)
     
     tree = kdtree.createNewTree(nodes)
     
     for i in range(10):
         tree.split2([random.random(), random.random(), random.random()], axis=random.randint(0, netDimension-1))
     
     points_tree =  [(d.data, d.axis) for d in kdtree.level_order(tree) if d.data is not None]
     kdtree.visualize(tree)
     
     kdtree.save( tree, "save_tree_test.pkl" )
     tree_loaded = kdtree.load("save_tree_test.pkl")
     
     points_tree_loaded =  [(d.data, d.axis) for d in kdtree.level_order(tree_loaded) if d.data is not None]
     
     numpy.testing.assert_array_equal(points_tree, points_tree_loaded, "trees not equal?") 
Example #4
0
    def test_showQ(self):
        import matplotlib.pyplot as plt
        import time
          
        print "---------- DisplayTreeTest ----------"
        #plt.figure(self.fig_values.number)
        maxLevel=2
        no1dN = []
        points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
        util.splitN(points, 0, 0, maxLevel, no1dN)
        tree = kdtree.createNewTree(no1dN)
          
        numberOfStates= tree.getHighestNodeId
        numberOfActions = 4
        
        kdtree.visualize(tree)
        Q = numpy.ones((100,numberOfActions))
        
        n = tree.get_path_to_best_matching_node([0.75, 0.75])[-1]
        print n.label
        n.split2([0.85, 0.75], axis=0, sel_axis = (lambda axis: axis))
        kdtree.visualize(tree)  
        # only leaves are shown!
        # States are positioned like in the tree i.e. xy axes splits in tree represent xy position in coord system
        # 0, 0 is bottom left, x increases to the right 
        # action 0 is to the left
        # Q[State][action]
        Q[3][0] = 0 # bottom left, action left
#         Q[5][1] = 0.1 # above Q[2] (in y direction), right
#         Q[58][2] = 0.1 #right top corner, down
#         Q[4][0] = 0.5
        kdtree.plotQ2D(tree, min_coord=[0, 0], max_coord=[1, 1],Values = Q, plt=plt, plot="Q")
        time.sleep(5)  
Example #5
0
    def test_splitNode(self):
        ''' find the best matching node and split it, then find the best matching node again. 
            Check if point lies in new generated node'''
        print "---------- test splitNode --------"
        #create tree with 2 levels
        listSplitPoints = []
        points = numpy.array([[0.0, 0.0],[0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
        util.splitN(points, 0,0,5, listSplitPoints)
        tree2dN = kdtree.createNewTree(listSplitPoints)
        util.activate(tree2dN, 2)
        
        #points
        point1 = [0.9,0.1]
        point2 = [0.1,0.9]
        
        kdtree.visualize(tree2dN)

        # split
        print "found: ", tree2dN.get_path_to_best_matching_node(point1)[-1] 
        tree2dN.get_path_to_best_matching_node(point1)[-1].activate_subnodes()
        kdtree.visualize(tree2dN)
        tree2dN.get_path_to_best_matching_node(point1)[-1].activate_subnodes()
        kdtree.visualize(tree2dN)
        print "data: ",  tree2dN.get_path_to_best_matching_node(point1)[-1].data
        self.assertEqual( tree2dN.get_path_to_best_matching_node(point1)[-1].data, [0.875, 0.125], "wrong node")
        
        tree2dN.get_path_to_best_matching_node(point2)[-1].activate_subnodes()
        tree2dN.get_path_to_best_matching_node(point2)[-1].activate_subnodes()
        self.assertEqual( tree2dN.get_path_to_best_matching_node(point2)[-1].data, [0.125, 0.875], "wrong node")
        del tree2dN
 def test_add_empty_nodes_with_label_when_splitting(self):
     """
     When a node is split along a certain axis, then this split should be active immediately.
     Create tree, split right and left node, try to find matching node
     """
     print "----- test_add_empty_nodes_with_label_when_splitting -----"
     sel_axis = (lambda axis: axis)
     
     #create tree, first node splits in x direction
     tree = kdtree.createNewTree([[0.5, 0.5]],axis = 0, sel_axis= sel_axis)
     kdtree.visualize(tree)
     
     point_left = [0.4, 0.5]
     tree.split2(point_left, axis = 0)
     kdtree.visualize(tree)
      
     point1 = [0.3, 0.5]
     found_node = tree.get_path_to_leaf(point1)[-1]
     correct_node1 = 3
     self.assertEqual(found_node.label, correct_node1, "Not correct node found")
     
     point_right = [0.6, 0.5]
     tree.split2(point_right, axis = 1)
     kdtree.visualize(tree)
     
     point2 = [0.6, 0.7]
     found_node = tree.get_path_to_leaf(point2)[-1]
     correct_node2 = 6
     self.assertEqual(found_node.label, correct_node2, "Not correct node found")
             
     print "----- end: test_add_empty_nodes_with_label_when_splitting -----"
    def test_plotTree(self):
        # function to chose next spillting axis
        sel_axis = (lambda axis: axis)
        
        #create tree, first node splits in x direction
        tree = kdtree.createNewTree([[0.5, 0.5]],axis = 0, sel_axis= sel_axis)
        tree.split2([0.4, 0.5], axis = 0, sel_axis = sel_axis)
        
        #add right node root node and left node to new node
        tree.split2([0.6, 0.5], axis = 1, sel_axis = sel_axis)
        tree.split2([0.7, 0.4], axis = 0, sel_axis = sel_axis)
        
        print "node before: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label
        print "node before: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label
        #add a node
        tree.split2([0.3, 0.6], axis = 1, sel_axis = sel_axis)

        print "node after: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label
        print "node after: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label
        
        kdtree.visualize(tree)

#        img=mpimg.imread("test_unconstraint_tree.png")  
#        plt.imshow(img)
 
        # Compare to image test_unconstraint_tree.png
        kdtree.plot2D(tree, plt=plt)
Example #8
0
 def test_numberOfNodes(self):
     highestlevel = 4
     numberOfStates= 2**(highestlevel+2)-1
     no1dN = []
     points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
     util.splitN(points, 0, 0, highestlevel, no1dN)
     tree = kdtree.createNewTree(no1dN)
     self.assertEqual(tree.getHighestNodeId, numberOfStates, "created and expected number of states does not match")
Example #9
0
 def test_getNode(self):
     print "---------- test getNode --------"
     listSplitPoints = []
     points = numpy.array([[0.0, 0.0],[0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
     util.splitN(points, 0,0,6, listSplitPoints)
     tree = kdtree.createNewTree(listSplitPoints)
     util.activate(tree, 6)
     kdtree.visualize(tree)
     nodeLabel = 117
     node = kdtree.getNode(tree, nodeLabel)
     self.assertEqual( node.label, nodeLabel, "returned wrong node")
     del tree
Example #10
0
 def __init__(self):
     nodes = []
     points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
     util.splitN(points, 0, 0, 4, nodes)
     
     #print "nodes:", nodes  
     print "Number of node: ", len(nodes)
     self.tree = kdtree.createNewTree(nodes)
     
     util.activate(self.tree, 3)
     
     self.fig, self.ax = plt.subplots()
     self.fig2, self.ax2 = plt.subplots()
Example #11
0
    def test_create_tree_create_new_tree_with_data_from_first(self):
        nodes = []
        netDimension = 2
        levels = 3
        sequence = ["".join(seq) for seq in itertools.product("01", repeat=netDimension)]
        points_temp= numpy.array([list(s) for s in sequence])
        points = numpy.array([map(float, f) for f in points_temp])
        
        util.splitN(points, 0, 0, levels, nodes)
        
        #print "nodes:", nodes  
        tree = kdtree.createNewTree(nodes)
#        kdtree.visualize(tree)
         
        points_tree =  [(d.data, d.axis) for d in kdtree.level_order(tree) if d.data is not None]
        points_tree_copy = list(points_tree)
        
        tree1 = kdtree.createNewTree([d[0] for d in points_tree]) # points_tree1 is changed
#        kdtree.visualize(tree1)       
        
        points_tree2 =  [(d.data, d.axis) for d in kdtree.level_order(tree1) if d.data is not None]
        numpy.testing.assert_array_equal(points_tree_copy, points_tree2, "trees not equal?")
Example #12
0
    def test_numberOfActiveStates(self):
        """only temporary, active property will disapear in future"""
        highestlevel = 4
        numberOfStates= 2**(highestlevel+2)-1
        no1dN = []
        points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]])
        util.splitN(points, 0, 0, highestlevel, no1dN)
        tree = kdtree.createNewTree(no1dN)

        util.activate(tree, highestlevel+1)
         
        activeNodes = len([n for n in kdtree.level_order(tree) if n.active])
        print "activeNodes: ", activeNodes, "       numberOfStates: ", numberOfStates
        self.assertEqual(activeNodes, numberOfStates, "not the correct number of nodes active")
    def test_createTreeRoot(self):
        '''
        Created tree should have a root and two empty nodes with a label'''
        sel_axis = (lambda axis: axis)
        tree = kdtree.createNewTree([[0.5, 0.5, 0.5]],axis = 1, sel_axis= sel_axis)
#        kdtree.visualize(tree)
        self.assertTrue(tree.label == 2, "left label is not 2")
        self.assertTrue(tree.axis == 1, "left label is not 2")

        self.assertTrue(tree.left is not None, "left node of root is missing")
        self.assertTrue(tree.left.label == 0, "left label is not 0")
        
        self.assertTrue(tree.right is not None, "right node of root is missing")
        self.assertTrue(tree.right.label == 1, "right label is not 1")
 def test_createTree(self):
     '''
     Created tree and split left and right node'''
     sel_axis = (lambda axis: axis)
     tree = kdtree.createNewTree([[0.5, 0.5, 0.5]],axis = 1, sel_axis= sel_axis)
     
     #add right node
     point_right = [0.4, 0.5, 0.5]
     tree.split2(point_right, axis = 2)
     self.assertTrue(tree.right.data == point_right, "right node data not set")
     self.assertTrue(tree.right.axis == 2, "right node axis not set")
     self.assertTrue(tree.right.label == 1, "right node label wrong")
     self.assertTrue(tree.right.right is not None, "right node chikd missing")
     self.assertTrue(tree.right.left is not None, "right node chikd missing")
     
     #add left node
     point_left = [0.4, 0.4, 0.5]
     tree.split2(point_left, axis = 2)
     self.assertTrue(tree.left.data == point_left, "left node data not set")
     self.assertTrue(tree.left.axis == 2, "left node axis not set")
     self.assertTrue(tree.left.label == 0, "left node label wrong")
     self.assertTrue(tree.left.left is not None, "left node chikd missing")
     self.assertTrue(tree.left.left is not None, "left node chikd missing")