Beispiel #1
0
 def testIterativeDeepeningErr2(self):
     target = 10
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space, max_states_num=9)
     sol, err = solver.IterativeDeepening([tree_space.start_point])
     self.assertEqual(None, sol)
     self.assertEqual(2, err)
     self.assertEqual(9, solver.num_states_seen)
Beispiel #2
0
 def testIterativeDeepeningErr0(self):
     target = 10
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space, max_states_num=10)
     sol, err = solver.IterativeDeepening([tree_space.start_point])
     self.assertEqual(target, sol.state.val)
     self.assertEqual(0, err)
     self.assertEqual(10, solver.num_states_seen)
Beispiel #3
0
 def testDFSErr1B(self):
     target = 10
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space)
     sol, err = solver.DFS(tree_space.start_point, 2)
     self.assertEqual(None, sol)
     self.assertEqual(1, err)
     self.assertEqual(6, solver.num_states_seen)
Beispiel #4
0
 def testDFSErr0B(self):
     target = 10
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space)
     sol, err = solver.DFS(tree_space.start_point, 3)
     self.assertEqual(target, sol.state.val)
     self.assertEqual(0, err)
     self.assertEqual(8, solver.num_states_seen)
Beispiel #5
0
 def testBFSErr1(self):
     target = 17
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space,
                                 max_queue_size=8,
                                 max_states_num=16)
     sol, err = solver.BFS()
     self.assertEqual(None, sol)
     self.assertEqual(1, err)
     self.assertSequenceEqual([], [i.state.val for i in solver.bfs_queue])
     self.assertEqual(14, solver.num_states_seen)
Beispiel #6
0
 def testSearchErr0D(self):
     target = 10
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space,
                                 max_queue_size=1,
                                 max_states_num=16)
     sol, err = solver.Search()
     self.assertEqual(target, sol.state.val)
     self.assertEqual(0, err)
     self.assertSequenceEqual([0], [i.state.val for i in solver.bfs_queue])
     self.assertEqual(10, solver.num_states_seen)
Beispiel #7
0
 def testSearchErr2(self):
     target = 17
     tree_space = TreeSpace(TreeState(self.root), target)
     solver = IterativeDeepening(tree_space,
                                 max_queue_size=3,
                                 max_states_num=9)
     sol, err = solver.Search()
     self.assertEqual(None, sol)
     self.assertEqual(2, err)
     self.assertSequenceEqual([2, 3, 4],
                              [i.state.val for i in solver.bfs_queue])
     self.assertEqual(9, solver.num_states_seen)
def main():
    parser = argparse.ArgumentParser(
        description=("Using BFS with Iterative Deepening to solve "
                     "the post correspondence problem of dominos."))
    parser.add_argument("FILE", type=str, help="input file name.")
    parser.add_argument("-d",
                        "--debug",
                        action="store_true",
                        help="show all the debug logging infomation.")
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        help="track the state changes towards the solution.")
    args = parser.parse_args()
    fname = args.FILE
    if args.debug:
        logging.getLogger().setLevel(logging.INFO)

    max_queue_size, max_states_num, dominos = LoadFile(fname)

    domino_space = DominoSpace(dominos=dominos)
    solver = IterativeDeepening(domino_space,
                                max_queue_size=max_queue_size,
                                max_states_num=max_states_num)
    sol, err = solver.Search()
    logging.info("%r, %r", sol, err)
    print(ERR_MESSAGE[err])
    if sol:
        print("Solution:\n\t%s" % sol)
    if args.verbose and sol:
        print("Path towards solution state:\n\t", end="")
        path_to_sol = domino_space.Replay(sol)
        print(" => ".join(["{}".format(s.state) for s in path_to_sol]))
    if args.verbose:
        all_states = solver.seen_bfs_states.keys(
        ) + solver.seen_dfs_states.keys()
        print("All %d states explored:\n\t" % len(all_states), end="")
        print(" ".join(["{}".format(s) for s in all_states]))