Advertisement
shoroujjahan

Untitled

Sep 3rd, 2021
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. class puzzle:
  2.     def init (self, starting, parent):
  3.         self.board = starting
  4.         self.parent = parent
  5.         self.f = 0
  6.         self.g = 0
  7.         self.h = 0
  8.  
  9.     def manhattan(self):
  10.         h = 0
  11.         for i in range(3):
  12.             for j in range(3):
  13.                 x, y = divmod(self.board[i][j], 3)
  14.                 h += abs(x-i) + abs(y-j)
  15.         return h
  16.  
  17.  
  18.     def goal(self):
  19.         inc = 0
  20.         for i in range(3):
  21.             for j in range(3):
  22.                 if self.board[i][j] != inc:
  23.                     return False
  24.                 inc += 1
  25.         return True
  26.  
  27.     def __eq__(self, other):
  28.         return self.board == other.board
  29.  
  30. def move_function(curr):
  31.     curr = curr.board
  32.     for i in range(3):
  33.         for j in range(3):
  34.             if curr[i][j] == 0:
  35.                 x, y = i, j
  36.                 break
  37.     q = []
  38.     if x-1 >= 0:
  39.         b = deepcopy(curr)
  40.         b[x][y]=b[x-1][y]
  41.         b[x-1][y]=0
  42.         succ = puzzle(b, curr)
  43.         q.append(succ)
  44.     if x+1 < 3:
  45.         b = deepcopy(curr)
  46.         b[x][y]=b[x+1][y]
  47.         b[x+1][y]=0
  48.         succ = puzzle(b, curr)
  49.         q.append(succ)
  50.     if y-1 >= 0:
  51.         b = deepcopy(curr)
  52.         b[x][y]=b[x][y-1]
  53.         b[x][y-1]=0
  54.         succ = puzzle(b, curr)
  55.         q.append(succ)
  56.     if y+1 < 3:
  57.         b = deepcopy(curr)
  58.         b[x][y]=b[x][y+1]
  59.         b[x][y+1]=0
  60.         succ = puzzle(b, curr)
  61.         q.append(succ)
  62.  
  63.     return q
  64.  
  65. def best_fvalue(openList):
  66.     f = openList[0].f
  67.     index = 0
  68.     for i, item in enumerate(openList):
  69.         if i == 0:
  70.             continue
  71.         if(item.f < f):
  72.             f = item.f
  73.             index  = i
  74.  
  75.     return openList[index], index
  76.  
  77. def AStar(start):
  78.     openList = []
  79.     closedList = []
  80.     openList.append(start)
  81.  
  82.     while openList:
  83.         current, index = best_fvalue(openList)
  84.         if current.goal():
  85.             return current
  86.         openList.pop(index)
  87.         closedList.append(current)
  88.  
  89.         X = move_function(current)
  90.         for move in X:
  91.             ok = False   #checking in closedList
  92.             for i, item in enumerate(closedList):
  93.                 if item == move:
  94.                     ok = True
  95.                     break
  96.             if not ok:              #not in closed list
  97.                 newG = current.g + 1
  98.                 present = False
  99.  
  100.                 #openList includes move
  101.                 for j, item in enumerate(openList):
  102.                     if item == move:
  103.                         present = True
  104.                         if newG < openList[j].g:
  105.                             openList[j].g = newG
  106.                             openList[j].f = openList[j].g + openList[j].h
  107.                             openList[j].parent = current
  108.                 if not present:
  109.                     move.g = newG
  110.                     move.h = move.manhattan()
  111.                     move.f = move.g + move.h
  112.                     move.parent = current
  113.                     openList.append(move)
  114.  
  115.     return None
  116.  
  117.  
  118. #start = puzzle([[2,3,6],[0,1,8],[4,5,7]], None)
  119. start = puzzle([[5,2,8],[4,1,7],[0,3,6]], None)
  120. # start = puzzle([[0,1,2],[3,4,5],[6,7,8]], None)
  121. #start = puzzle([[1,2,0],[3,4,5],[6,7,8]], None)
  122. result = AStar(start)
  123. noofMoves = 0
  124.  
  125. if(not result):
  126.     print ("No solution")
  127. else:
  128.     print(result.board)
  129.     t=result.parent
  130.     while t:
  131.         noofMoves += 1
  132.         print(t.board)
  133.         t=t.parent
  134. print ("Length: " + str(noofMoves))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement