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_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?")
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)
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_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")
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
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()
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_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?")
# # path = Path(verts, codes) # # fig = plt.figure() # ax = fig.add_subplot(111) # patch = patches.PathPatch(path, facecolor='orange', lw=2) # ax.add_patch(patch) # ax.set_xlim(-2,2) # ax.set_ylim(-2,2) # plt.show() no2dN = [] 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, no2dN) tree2dN = kdtree.create(no2dN) util.activate(tree2dN, 4) kdtree.visualize(tree2dN) V = numpy.random.rand(len(no2dN),1) kdtree.plotQ2D(tree2dN, Values=V) #kdtree.plot2D(tree2dN) #kdtree.plot2DUpdate(tree2dN) # # # no3dN_test = []
# kdtree.visualize(tree) # no2dN = [] # points = numpy.array([[0.0, 0.0],[0.0 , 1.0], [1.0, 0.0], [1.0, 1.0]]) # util.splitN(points, 0,0,2, no2dN) # print "no2dN:", no2dN # print "Number of nodes2dN: ", len(no2dN) # tree2dN = kdtree.create(no2dN) # kdtree.visualize(tree2dN) # kdtree.plot2D(tree2dN) no3dN = [] #points = numpy.array([[0.0, 0.0, 0.0],[0.0 , 0.0, 1.0], [ 0.0, 1.0, 0.0], [ 0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) points = numpy.array([[0.0, 0.0, 0.0, 0.0],[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 1.0, 0.0], [1.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0]]) util.splitN(points, 0,0,5, no3dN) #print "no3dN:", no3dN print "Number of nodes3dN: ", len(no3dN) point = [1,0,0,0] tree3dN = kdtree.create(no3dN) util.activate(tree3dN, 2) print tree3dN.get_path_to_best_matching_node(point)[-1].label kdtree.visualize(tree3dN) #kdtree.plot2D(tree3dN) # # # no3dN_test = [] # points = numpy.array([[0.0, 0.0, 0.0],[0.0 , 0.0, 1.0], [ 0.0, 1.0, 0.0], [ 0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) # splitN_test(points, 0,0,7, no3dN_test) #
import kdtree import util import numpy import itertools import collections import time nodes = [] points = numpy.array([[0.0, 0.0, 0.0, 0.0],[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 1.0, 0.0], [1.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0]]) #util.splitN(points, 0, 0, 16, nodes) util.splitN(points, 0, 0, 4, nodes) #print "nodes:", nodes print "Number of node: ", len(nodes) tree = kdtree.create(nodes) #util.activate(tree, 16) kdtree.visualize(tree) print "done"