/
a_star_tree_misplaced_tiles.py
40 lines (31 loc) · 1.05 KB
/
a_star_tree_misplaced_tiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
pynpuzzle - Solve n-puzzle with Python
A* tree search algorithm using misplaced tiles heuristic
Version : 1.0.0
Author : Hamidreza Mahdavipanah
Repository: http://github.com/mahdavipanah/pynpuzzle
License : MIT License
"""
import heapq
from .util import best_first_seach as bfs
def search(state, goal_state):
"""A* tree search using misplaced tiles heuristic"""
def gn(node):
return node.gn()
tiles_places = []
for i in range(len(goal_state)):
for j in range(len(goal_state)):
heapq.heappush(tiles_places, (goal_state[i][j], (i, j)))
def hn(node):
misplace_count = 0
for i in range(len(node.state)):
for j in range(len(node.state)):
if node.state[i][j] == 0:
continue
tile_i, tile_j = tiles_places[node.state[i][j]][1]
if i != tile_i or j != tile_j:
misplace_count += 1
return misplace_count
def fn(node):
return gn(node) + hn(node)
return bfs.search(state, goal_state, fn)