def test_total_cost(self): board = BoardState((1, 1), { 'A': (0, 0), 'B': (2, 2), 'C': (2, 1) }, (3, 3)) node_1 = Node(board, 10, 0, Action.Unknown) node_2 = Node(board, 15, 0, Action.Right, node_1) node_3 = Node(board, 30, 0, Action.Down, node_2) node_4 = Node(board, 50, 0, Action.Down, node_3) solution = Solution(node_4) self.assertEqual(solution.get_total_cost(), node_4.cost)
def test_find_solution(self): board = BoardState((1, 1), { 'A': (0, 0), 'B': (2, 2), 'C': (2, 1) }, (3, 3)) node_1 = Node(board, 0, 0, Action.Unknown) node_2 = Node(board, 0, 0, Action.Right, node_1) node_3 = Node(board, 0, 0, Action.Down, node_2) node_4 = Node(board, 0, 0, Action.Down, node_3) solution = Solution(node_4) self.assertEqual(solution.get_actions(), ['Right', 'Down', 'Down'])
def expand(self, node): successors = [] for action in AbstractTreeSearch.ALL_ACTIONS: new_state = node.state.move(action) if (new_state is not None): new_node = Node(state=node.state.move(action), cost=0, depth=node.depth + 1, action=action, parent=node) new_node.cost = self.compute_cost(node, new_node) successors.append(new_node) return successors
def test_expand_generate_only_available_moves(self): board = BoardState((2, 1), { 'A': (0, 0), 'B': (0, 2), 'C': (2, 2) }, (3, 3)) node = Node(board, 0, 0, Action.Unknown) tree_search = AbstractTreeSearch(board, board) successors = tree_search.expand(node) self.assertTrue(len(successors) == 3) child_up = Node(board.move(Action.Up), 1, 1, Action.Up) child_down = Node(board.move(Action.Down), 1, 1, Action.Down) child_left = Node(board.move(Action.Left), 1, 1, Action.Left) self.assertTrue(child_up in successors) self.assertTrue(child_down in successors) self.assertTrue(child_left in successors)
def add_start_node(self): self.fringe.add([ Node(state=self.start_state, cost=0, depth=0, action=None, parent=None) ])
def add_start_node(self): self.fringe.add([ Node(state=self.start_state, cost=self.evaluator.estimate_cost_to_goal( self.start_state, self.goal_state), depth=0, action=None, parent=None) ])
def test_goal_check(self): start = BoardState((2, 2), { 'A': (0, 0), 'B': (0, 2), 'C': (2, 0) }, (3, 3)) goal = BoardState((1, 1), { 'A': (0, 0), 'B': (0, 2), 'C': (2, 0) }, (3, 3)) node_1 = Node(goal, 0, 0, Action.Unknown) node_2 = Node(goal, 100, 2, Action.Right) node_3 = Node(goal, 80, 5, Action.Left) tree_search = AbstractTreeSearch(start, goal) self.assertTrue(tree_search.goal_test(node_1)) self.assertTrue(tree_search.goal_test(node_2)) self.assertTrue(tree_search.goal_test(node_3))
def __init__(self, start_state, goal_state, fringe=Fringe()): self.start_state = start_state self.goal_state = goal_state self.fringe = fringe self.fringe.add([ Node(state=self.start_state, cost=0, depth=0, action=None, parent=None) ])
def test_expand_set_parent_node(self): board = BoardState((1, 1), { 'A': (0, 0), 'B': (0, 2), 'C': (2, 0) }, (3, 3)) node = Node(board, 0, 0, Action.Unknown) tree_search = AbstractTreeSearch(board, board) successors = tree_search.expand(node) self.assertTrue(len(successors) == 4) for s in successors: self.assertTrue(s.parent == node)
def __init__(self, start_state, goal_state, evaluator: CostEvaluator): self.start_state = start_state self.goal_state = goal_state self.fringe = CostOrderedFringe() self.evaluator = evaluator self.fringe.add([ Node(state=self.start_state, cost=self.evaluator.estimate_cost_to_goal( start_state, self.goal_state), depth=0, action=None, parent=None) ])
def test_expand_update_cost_and_depth(self): board = BoardState((1, 1), { 'A': (0, 0), 'B': (0, 2), 'C': (2, 0) }, (3, 3)) node = Node(board, 0, 0, Action.Unknown) tree_search = AbstractTreeSearch(board, board) c = tree_search.expand(node)[0] successors = tree_search.expand(c) for s in successors: self.assertTrue(s.cost == 2) self.assertTrue(s.depth == 2)
def __init__(self, start_state, goal_state, evaluator: CostEvaluator): self.start_state = start_state self.goal_state = goal_state self.fringe = ReverseFringe() self.evaluator = evaluator initial_node = Node(state=self.start_state, cost=self.evaluator.estimate_cost_to_goal( start_state, self.goal_state), depth=0, action=None, parent=None) self.cost_limit = initial_node.cost self.fringe.add([initial_node]) self.is_cost_exceed = False self.min_exceed_threshold = sys.maxsize