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_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")
# # 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 = [] # 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)
def update(self, network): result = self.bias for i in range(len(self.connections)): n = network[self.connections[i]] result += n.get_value(network) * self.weights[i] self.value = activate(result, self.activation)
# 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) # # assert all(x in no3dN_test for x in no3dN), "NOT EQUAL !!!" #kdtree.plot2D(tree) # print [ n.label for n in tree.get_path_to_best_matching_node((0.1,0.1))] # tree.get_path_to_best_matching_node((0.1,0.1))[-1].split()