def test_connect_adds_mutual_link(self): self.equivalence_classes.add(1) self.equivalence_classes.add(2) self.equivalence_classes.connect(1, 2) source_node = self.equivalence_classes.get_node(1) self.assertIn(Node(2), source_node.destinations) destination_node = self.equivalence_classes.get_node(2) self.assertIn(Node(1), destination_node.sources)
def run(self): search = True steps = 0 temp = self.generator.copy(self.root.matrix[:]) state = Node(temp) frontier = [state] visited = {} while (search and len(frontier) > 0): steps += 1 node = frontier[0] node.step = steps node.name = f"Node: {str(node.step)}" # TODO: review if it returns the node in that position # to attach it directly to node var frontier.pop(0) if not list(visited.values()).__contains__(node): visited[node.step] = node childs = self.neighbors(node, list(visited.values()), frontier) for i in childs: i.parent_id = node.step if (node.verify_winner()): search = False for i in childs: frontier.append(i) print(f"Steps: {steps}") return self.path(node, visited)
def construct_tree(self, node): """ Construct the decision trees using ID3 algorithm :param node: current node :return: """ target_labels = node.get_target_labels() if __DEBUG__: print("target labels: {}".format(target_labels)) if len(target_labels) == 1: node.assign_label(target_labels[0]) if __DEBUG__: print("Creating an external pure node!") return else: instances = node.instances best_feature_name = get_best_feature(instances, self.target) node.set_feature_name(best_feature_name) if __DEBUG__: print("setting current node fet name: {}".format( node.feature_name)) for attr_value in instances[best_feature_name].unique(): if __DEBUG__: print("attr value: {}".format(attr_value)) child_instances = instances.loc[instances[best_feature_name] == attr_value] child_node = Node(child_instances, self.target) if __DEBUG__: print( "creating a new child node - adding it to parent node: {}" .format(node.feature_name)) node.add_child(attr_value, child_node) self.construct_tree(child_node)
def right(self, position, matrix): # This method moves rigth # pos[0] - X # pos[1] - Y state = self.generate_state([position[0], position[1]], [position[0], position[1] + 2], [position[0], position[1] + 4], matrix) return Node(state) if state is not None else None
def __base_node(self): senku = [["_", "_", "_", "_", "1", "_", "_", "_", "_"], ["_", "_", "_", "1", "_", "1", "_", "_", "_"], ["_", "_", "1", "_", "1", "_", "1", "_", "_"], ["_", "1", "_", "1", "_", "1", "_", "1", "_"], ["0", "_", "1", "_", "1", "_", "1", "_", "1"]] return Node(senku)
def general_search(problem, queueing_function): nodes = [Node(problem.initial_state)] visited = list() count = 0 while nodes: node = nodes.pop(0) # remove front visited.append(node) if problem.goal_test(node.state): return node, count nodes = queueing_function(nodes, node.expand(problem)) nodes = filter_visited(nodes, visited) count += 1 progress(count) return None, count
def test_node_equivalence(self): self.assertEqual(Node(1), Node(1)) self.assertNotEqual(Node(1), Node(2)) self.assertNotEqual(2, Node(2))
def test_node_is_hashable(self): test_set = set() test_set.add(Node(1)) self.assertIn(Node(1), test_set)
def __init__(self, instances, target_variable): self.root = Node(instances, target_variable) self.target = target_variable
def down_left(self, position, matrix): state = self.generate_state([position[0], position[1]], [position[0] + 1, position[1] - 1], [position[0] + 2, position[1] - 2], matrix) return Node(state) if state is not None else None
def up_right(self, position, matrix): state = self.generate_state([position[0], position[1]], [position[0] - 1, position[1] + 1], [position[0] - 2, position[1] + 2], matrix) return Node(state) if state is not None else None