def update_tree(self, new_subtree, leaf): # search the leaf # compare selector_new = new_subtree.find_leaf(leaf) new_list = self.tree.nodes selector_down = self.tree.find_leaf(leaf) selector_up = selector_down while selector_new is not None: while selector_up == selector_new and selector_up is not None: selector_new = selector_new.parent selector_down = selector_up # new_list.append(selector_down) selector_up = selector_up.parent if selector_new is not None: selector_down.unbind_parent() selector_down.bind_parent(Node(name=selector_new.name, parent=selector_up, altitude=selector_new.altitude)) selector_down = selector_down.parent new_list.append(selector_down) selector_new = selector_new.parent new_list.sort() self.tree = Tree(new_list) """return the subtree of the leaf"""
def merging(self): self.compute() self.new_tree = Tree(self.new_tree_nodes) update_bloc1 = self.new_tree.subtree(self.edge[0]) self.bloc_1.update_tree(update_bloc1, self.edge[0]) update_bloc2 = self.new_tree.subtree(self.edge[1]) self.bloc_2.update_tree(update_bloc2,self.edge[1])
def test_init(): node_root = Node() nodes = [Node(), Node(), Node(), node_root] tree_1 = Tree(nodes) assert tree_1.nodes is nodes assert tree_1.root is node_root
def test_sub_graph(): node_1 = Node(name="Node1") node_2 = Node(name="NodeD2", parent=node_1) node_3 = Node(name="NodeIII", parent=node_2) node_4 = Node(name="NodeKAT", parent=node_3) tree_1 = Tree([node_4, node_3, node_2, node_1]) assert tree_1.root is node_1 assert tree_1.root is not node_3
def do_QBT(self, border): self.sort() nodes = border.generate_leafs() for i, edge in enumerate(self): e1 = int_coords_ibloc_to_iimage(edge[0], border) e2 = int_coords_ibloc_to_iimage(edge[1], border) if nodes[edge[0]].root() is nodes[edge[1]].root(): continue nodes.append(Node(name=(e1, e2), altitude=self.weights[i], left=nodes[edge[0]].root(), right=nodes[edge[1]].root())) res = Tree(nodes) return res
class Block: """A block possess one tree and border coordinates """ def __init__(self, graph, border): self.border = border self.tree = graph.do_QBT(self.border) """the tree is updated to accept the merging tree""" def update_tree(self, new_subtree, leaf): # search the leaf # compare selector_new = new_subtree.find_leaf(leaf) new_list = self.tree.nodes selector_down = self.tree.find_leaf(leaf) selector_up = selector_down while selector_new is not None: while selector_up == selector_new and selector_up is not None: selector_new = selector_new.parent selector_down = selector_up # new_list.append(selector_down) selector_up = selector_up.parent if selector_new is not None: selector_down.unbind_parent() selector_down.bind_parent(Node(name=selector_new.name, parent=selector_up, altitude=selector_new.altitude)) selector_down = selector_down.parent new_list.append(selector_down) selector_new = selector_new.parent new_list.sort() self.tree = Tree(new_list) """return the subtree of the leaf""" def get_subtree(self, leaf_name): return self.tree.subtree(leaf_name)
class Server: def __init__(self, bloc_1, bloc_2, edge, edge_altitude): self.bloc_1 = bloc_1 self.bloc_2 = bloc_2 self.edge = edge self.edge_altitude = edge_altitude self.bloc_1 = bloc_1 self.bloc_2 = bloc_2 self.subtree_1 = bloc_1.get_subtree(edge[0]) self.subtree_2 = bloc_2.get_subtree(edge[1]) self.selector_1_down = self.subtree_1.nodes[0] self.selector_2_down = self.subtree_2.nodes[0] self.selector_1_up = self.selector_1_down.parent self.selector_2_up = self.selector_2_down.parent self.current_node = None self.node_created = False self.new_tree = None self.new_tree_nodes = [] def merging(self): self.compute() self.new_tree = Tree(self.new_tree_nodes) update_bloc1 = self.new_tree.subtree(self.edge[0]) self.bloc_1.update_tree(update_bloc1, self.edge[0]) update_bloc2 = self.new_tree.subtree(self.edge[1]) self.bloc_2.update_tree(update_bloc2,self.edge[1]) def compute(self): while self.selector_1_up is not self.selector_2_up: # If the node is not created if self.node_created is False: self.new_tree_nodes.append(self.selector_1_down) self.new_tree_nodes.append(self.selector_2_down) # If both selectors are roots of they respective trees, we create the node if self.selector_1_up.is_root() and self.selector_2_up.is_root(): self.create_node() # If the altitude of both selectorcurrent_nodes up are higher than the altitude of the edge to merge if self.selector_1_up.altitude > self.edge_altitude and self.selector_2_up.altitude > self.edge_altitude: self.create_node() self.update_selector_1() self.update_selector_2() # When the node was created if self.node_created is True: self.current_node.unbind_parent() self.current_node.bind_parent(self.second_min_selectors_up()) self.new_tree_nodes.append(self.current_node) self.second_update_selector_1() self.second_update_selector_2() if self.current_node.is_root() is False: self.current_node = self.current_node.parent print(self.current_node) def update_selector_1(self): # If the altitude of the selector up is lower than the altitude of the edge, we increment the selector if self.selector_1_up.altitude < self.edge_altitude: self.selector_1_down = self.selector_1_up if self.selector_1_up.parent is not None: self.selector_1_up = self.selector_1_up.parent def second_update_selector_1(self): self.selector_1_down = self.selector_1_up if self.selector_1_up.parent is not None: self.selector_1_up = self.selector_1_up.parent def update_selector_2(self): if self.selector_2_up.altitude < self.edge_altitude: self.selector_2_down = self.selector_2_up if self.selector_2_up.parent is not None: self.selector_2_up = self.selector_2_up.parent def second_update_selector_2(self): self.selector_2_down = self.selector_2_up if self.selector_2_up.parent is not None: self.selector_2_up = self.selector_2_up.parent def create_node(self): # STEP 1) Create the node and link the node to his parent and to his childs self.selector_1_down.unbind_parent() self.selector_2_down.unbind_parent() self.current_node = Node(name="NewNode", altitude=self.edge_altitude, parent=self.min_selectors_up(), left=self.selector_1_down, right=self.selector_2_down) self.node_created = True self.new_tree_nodes.append(self.current_node) self.current_node = self.current_node.parent # STEP 3) Update the good selector if self.selector_1_up is self.current_node: self.second_update_selector_1() elif self.selector_2_up is self.current_node: self.second_update_selector_2() def update_machins(self): # STEP 2) Delete the remainings links from the selectors self.selector_1_down.parent = self.current_node self.selector_2_down.parent = self.current_node self.selector_1_up.delete_child(self.selector_1_down) self.selector_2_up.delete_child(self.selector_2_down) self.current_node = self.current_node.parent if self.selector_1_up is self.current_node: self.second_update_selector_1() elif self.selector_2_up is self.current_node: self.second_update_selector_2() def min_selectors_up(self): # If both are root => root is the next selector if self.selector_1_up.altitude > self.selector_2_up.altitude: return self.selector_2_up else: return self.selector_1_up def second_min_selectors_up(self): if self.current_node.is_root() and self.current_node is self.selector_1_up: return self.selector_2_up if self.current_node.is_root() and self.current_node is self.selector_2_up: return self.selector_1_up if self.selector_1_up.altitude > self.selector_2_up.altitude: return self.selector_2_up else: return self.selector_1_up def max_selectors_up(self): # If both are root => root is the next selector if self.selector_1_up.altitude > self.selector_2_up.altitude: return self.selector_1_up else: return self.selector_2_up