Esempio n. 1
0
 def test_h_manhattan(self):
     a = E.State([[3, 1, 2], [4, 5, 0], [6, 7, 8]])
     self.assertTrue(a.h_manhattan() == 3)
     b = E.State([[8, 7, 6], [5, 4, 3], [2, 1, 0]])
     self.assertTrue(b.h_manhattan() == 20)
     c = E.State([[7, 2, 4], [5, 0, 6], [8, 3, 1]])
     self.assertTrue(c.h_manhattan() == 18)
     print("Manhattan passes!")
Esempio n. 2
0
 def test_custom(self):
     a = E.State([[0, 2, 1], [3, 7, 8], [4, 6, 5]])
     self.assertTrue(a.h_manhattan() == 8)
     self.assertTrue(a.h_custom() == 10)
     b = E.State([[8, 7, 6], [5, 4, 3], [2, 1, 0]])
     self.assertTrue(b.h_manhattan() == 20)
     self.assertTrue(b.h_custom() == 22)
     print("custom tests passed")
Esempio n. 3
0
    def test_h_euclidian(self):
        a = E.State([[8, 1, 2], [3, 4, 5], [6, 7, 0]])
        self.assertTrue(a.h_euclidian() == m.sqrt(8))
        b = E.State([[3, 1, 2], [4, 5, 0], [6, 7, 8]])
        self.assertTrue(b.h_euclidian() == 3)
        c = E.State(([[8, 7, 6], [5, 4, 3], [2, 1, 0]]))
        result = c.h_euclidian()
        #print(result)
        #self.assertTrue(c.h_euclidian() == 3*m.sqrt(8) + 8)

        print("Euclidian passes!")
Esempio n. 4
0
 def test_isSolvable(self):
     a = E.State([[3, 1, 2], [4, 5, 0], [6, 7, 8]])
     self.assertTrue(isSolvable(a))
     b = E.State([[8, 7, 6], [5, 4, 3], [2, 1, 0]])
     self.assertTrue(isSolvable(b))
     c = E.State([[0, 1, 2], [3, 4, 5], [6, 8, 7]])
     self.assertFalse(isSolvable(c))
     d = E.State([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
     self.assertTrue(isSolvable(d))
     e = E.State([[8, 7, 6], [5, 0, 3], [4, 2, 1]])
     self.assertFalse(isSolvable(e))
     print("tests passed")
Esempio n. 5
0
 def test_h_hamming(self):
     a = E.State([[0, 3, 2], [1, 7, 4], [8, 5, 6]])
     self.assertTrue(a.h_hamming() == 7)
     b = E.State([[0, 1, 3], [4, 6, 7], [5, 8, 2]])
     self.assertTrue(b.h_hamming() == 7)
     c = E.State([[-2, -5, -6], [99, 82, 45], [99, 17, 56]])
     self.assertTrue(c.h_hamming() == 8)
     d = E.State([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
     self.assertTrue(d.h_hamming() == 0)
     e = E.State(([[0, 1, 2], [4, 3, 5], [7, 6, 8]]))
     self.assertTrue(e.h_hamming() == 4)
     f = E.State([[3, 1, 2], [4, 5, 0], [6, 7, 8]])
     self.assertTrue(f.h_hamming() == 3)
     print("Hamming tests Passed!")
Esempio n. 6
0
import EightPuzzleWithHeuristics as Problem

# puzzle1a:
CREATE_INITIAL_STATE = lambda: Problem.State([1, 0, 2, 3, 4, 5, 6, 7, 8])
Esempio n. 7
0
import EightPuzzleWithHeuristics as p

CREATE_INITIAL_STATE = lambda: p.State([4, 5, 0, 1, 2, 8, 3, 7, 6])
Esempio n. 8
0
import EightPuzzleWithHeuristics as p

CREATE_INITIAL_STATE = lambda: p.State([3, 1, 2, 6, 8, 7, 5, 4, 0])
Esempio n. 9
0
import EightPuzzleWithHeuristics as Problem

#puzzle10a.py:
CREATE_INITIAL_STATE = lambda: Problem.State([4, 5, 0, 1, 2, 3, 6, 7, 8])

Esempio n. 10
0
# python3 AStar.py EightPuzzleWithHeuristics h_manhattan puzzle10a

import sys
from queue import PriorityQueue

# DO NOT CHANGE THIS SECTION
if sys.argv==[''] or len(sys.argv)<3:
    import EightPuzzleWithHeuristics as Problem
    heuristics = lambda s: Problem.HEURISTICS['h_manhattan'](s)
    initial_state = Problem.CREATE_INITIAL_STATE()

else:
    import importlib
    Problem = importlib.import_module(sys.argv[1])
    heuristics = lambda s: Problem.HEURISTICS[sys.argv[2]](s)
    initial_state = Problem.State(importlib.import_module(sys.argv[3]).CREATE_INITIAL_STATE())



print("\nWelcome to AStar")
COUNT = None
BACKLINKS = {}

# DO NOT CHANGE THIS SECTION
def runAStar():
    #initial_state = Problem.CREATE_INITIAL_STATE(keyVal)
    # initial_state = Problem.CREATE_INITIAL_STATE()
    print("Initial State:")
    print(initial_state)
    global COUNT, BACKLINKS
    COUNT = 0
Esempio n. 11
0
import EightPuzzleWithHeuristics as Problem

# puzzle4a:
CREATE_INITIAL_STATE = lambda: Problem.State([1, 4, 2, 3, 7, 0, 6, 8, 5])
Esempio n. 12
0
import EightPuzzleWithHeuristics as Problem

#puzzle16a.py:
CREATE_INITIAL_STATE = lambda: Problem.State([0, 8, 2, 1, 7, 4, 3, 6, 5])

Esempio n. 13
0
import EightPuzzleWithHeuristics as p
CREATE_INITIAL_STATE = lambda: p.State([0, 1, 2, 3, 4, 5, 6, 7, 8])
Esempio n. 14
0
import EightPuzzleWithHeuristics as Problem

# puzzle2a:
CREATE_INITIAL_STATE = lambda: Problem.State([3, 1, 2, 4, 0, 5, 6, 7, 8])