Esempio n. 1
0
class TestMethods(unittest.TestCase):

    offTwoPuzzle = puzzle8.state([3,4,5,2,0,6,1,8,7])

    def testNumWrongTiles(self):
        self.assertEqual(search.numWrongTiles(puzzle8.solution()),0)
        self.assertEqual(search.numWrongTiles(puzzle8.randomState(1)),1)
        self.assertEqual(search.numWrongTiles(self.offTwoPuzzle),8)

    def testManhattanDistance(self):
        self.assertEqual(search.manhattanDistance(puzzle8.solution()),0)
        self.assertEqual(search.manhattanDistance(puzzle8.randomState(1)),1)
        self.assertEqual(search.manhattanDistance(self.offTwoPuzzle),16)

    def testItdeep(self):
        self.assertEqual(search.itdeep(puzzle8.solution()),[])
        self.assertEqual(len(search.itdeep(puzzle8.randomState(1))),1)
        testPuzzle = puzzle8.state([1,2,0,8,6,3,7,5,4])
        self.assertEqual(search.itdeep(testPuzzle),[5,8,7,4])

        random.seed(12345)
        randomMoves = 32
        testPuzzle2 = puzzle8.randomState(randomMoves)
        puzzle8.display(testPuzzle2)
        solnPath = search.itdeep(testPuzzle2)
        self.assertLessEqual(len(solnPath),randomMoves)

    def testAstar(self):
        random.seed(12345)
        randomMoves = 32
        testPuzzle2 = puzzle8.randomState(randomMoves)
        puzzle8.display(testPuzzle2)
        solnPath = search.astar(testPuzzle2, search.manhattanDistance)
        self.assertLessEqual(len(solnPath),randomMoves)
Esempio n. 2
0
    def testItdeep(self):
        self.assertEqual(search.itdeep(puzzle8.solution()),[])
        self.assertEqual(len(search.itdeep(puzzle8.randomState(1))),1)
        testPuzzle = puzzle8.state([1,2,0,8,6,3,7,5,4])
        self.assertEqual(search.itdeep(testPuzzle),[5,8,7,4])

        random.seed(12345)
        randomMoves = 32
        testPuzzle2 = puzzle8.randomState(randomMoves)
        puzzle8.display(testPuzzle2)
        solnPath = search.itdeep(testPuzzle2)
        self.assertLessEqual(len(solnPath),randomMoves)
Esempio n. 3
0
    def testAstar(self):
        #random.seed(12345)
        solnPath = []
        solnPath2 = []
        randomMoves = 40
        while True:
            testPuzzle2 = puzzle8.randomState(randomMoves)
            then = time()
            solnPath = search.astar(testPuzzle2, search.manhattanDistance)
            then1 = time()
            #solnPath3 = search.itdeep(testPuzzle2)
            then2 = time()
            solnPath2 = search.astar(testPuzzle2, search.numWrongTiles)
            then3 = time()
            print("took {0} moves, astar manhattan took {1} sec, "
             "astar numWrong took {2} sec"
             .format(len(solnPath), round(then1-then,3), \
                round(then3-then2,3)))
            self.assertEqual(len(solnPath), len(solnPath2))

        solnPath2 = search.astar(testPuzzle2, search.numWrongTiles)
        print(testPuzzle2)
        self.assertEqual(solnPath, solnPath2)
        self.assertLessEqual(len(solnPath), randomMoves)

        testPuzzle3 = 179849340
        solnPath = search.astar(testPuzzle2, search.manhattanDistance)
        solnPath2 = search.astar(testPuzzle2, search.numWrongTiles)
        self.assertLessEqual(len(solnPath), 16)
        self.assertLessEqual(len(solnPath2), 16)

        testPuzzle = puzzle8.state([1, 2, 0, 8, 6, 3, 7, 5, 4])
        self.assertEqual(search.astar(testPuzzle, search.manhattanDistance),
                         [5, 8, 7, 4])
        self.assertEqual(search.astar(testPuzzle, search.numWrongTiles),
                         [5, 8, 7, 4])
Esempio n. 4
0
class TestMethods(unittest.TestCase):

    offTwoPuzzle = puzzle8.state([3, 4, 5, 2, 0, 6, 1, 8, 7])

    def testNumWrongTiles(self):
        self.assertEqual(search.numWrongTiles(puzzle8.solution()), 0)
        self.assertEqual(search.numWrongTiles(puzzle8.randomState(1)), 1)
        self.assertEqual(search.numWrongTiles(self.offTwoPuzzle), 8)

    def testManhattanDistance(self):
        self.assertEqual(search.manhattanDistance(puzzle8.solution()), 0)
        self.assertEqual(search.manhattanDistance(puzzle8.randomState(1)), 1)
        self.assertEqual(search.manhattanDistance(self.offTwoPuzzle), 16)

    def testItdeep(self):
        self.assertEqual(search.itdeep(puzzle8.solution()), [])

        self.assertEqual(len(search.itdeep(puzzle8.randomState(1))), 1)

        testPuzzle = puzzle8.state([1, 2, 0, 8, 6, 3, 7, 5, 4])
        self.assertEqual(search.itdeep(testPuzzle), [5, 8, 7, 4])

        random.seed(12345)
        randomMoves = 32
        testPuzzle2 = puzzle8.randomState(randomMoves)
        #puzzle8.display(testPuzzle2)
        solnPath = search.itdeep(testPuzzle2)
        self.assertLessEqual(len(solnPath), randomMoves)

    def testAstar(self):
        #random.seed(12345)
        solnPath = []
        solnPath2 = []
        randomMoves = 40
        while True:
            testPuzzle2 = puzzle8.randomState(randomMoves)
            then = time()
            solnPath = search.astar(testPuzzle2, search.manhattanDistance)
            then1 = time()
            #solnPath3 = search.itdeep(testPuzzle2)
            then2 = time()
            solnPath2 = search.astar(testPuzzle2, search.numWrongTiles)
            then3 = time()
            print("took {0} moves, astar manhattan took {1} sec, "
             "astar numWrong took {2} sec"
             .format(len(solnPath), round(then1-then,3), \
                round(then3-then2,3)))
            self.assertEqual(len(solnPath), len(solnPath2))

        solnPath2 = search.astar(testPuzzle2, search.numWrongTiles)
        print(testPuzzle2)
        self.assertEqual(solnPath, solnPath2)
        self.assertLessEqual(len(solnPath), randomMoves)

        testPuzzle3 = 179849340
        solnPath = search.astar(testPuzzle2, search.manhattanDistance)
        solnPath2 = search.astar(testPuzzle2, search.numWrongTiles)
        self.assertLessEqual(len(solnPath), 16)
        self.assertLessEqual(len(solnPath2), 16)

        testPuzzle = puzzle8.state([1, 2, 0, 8, 6, 3, 7, 5, 4])
        self.assertEqual(search.astar(testPuzzle, search.manhattanDistance),
                         [5, 8, 7, 4])
        self.assertEqual(search.astar(testPuzzle, search.numWrongTiles),
                         [5, 8, 7, 4])