Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement best first search algorithm with heuristic #5

Open
wants to merge 1 commit into
base: file_separation
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement best first search algorithm with sum of permutation of inve…
…rsions as a heuristic.
MewtR committed Oct 1, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ec749e35d40ad2dd1516b1b15bd4d723e766c58a
29 changes: 29 additions & 0 deletions best_first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import heapq
import heuristics
import utilities

def search(p):
unvisited = []
heapq.heappush(unvisited, (heuristics.sum_of_permutation_inversion(p), p))
visited = []

while unvisited:
current = heapq.heappop(unvisited)[1]
#with open('puzzleBFS.txt', 'a') as f:
# f.write("{} {} SPI= {}\n".format(current.move, current.config, heuristics.sum_of_permutation_inversion(current)))

if utilities.goal(current):
return current
if utilities.wasVisited(current, visited):
continue
else:
children = utilities.generateChildren(current)
#Remove visited children
children = utilities.unvisitedChildren(children, visited)
for c in children:
heapq.heappush(unvisited, (heuristics.sum_of_permutation_inversion(c), c))
visited.append(current)
#input("Press Enter to continue")
return None


14 changes: 14 additions & 0 deletions heuristics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from node import Node

def sum_of_permutation_inversion(p):
value = 0
for x in range(11):
for i in range(x+1, 12):
if ((p.config[x] > p.config[i]) and p.config[i] != 0):
value+=1
return value





4 changes: 4 additions & 0 deletions node.py
Original file line number Diff line number Diff line change
@@ -13,4 +13,8 @@ def __eq__(self, other):
def __ne__(self, other):
#Override the default Unequal behavior
return self.config != other.config

#In case there is a tie during heapq operations
def __lt__(self, other):
return ord(self.move) < ord(other.move)

41 changes: 13 additions & 28 deletions solver.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
from node import Node
import utilities
import depth_first
import time
import heuristics
import best_first
# Take puzzle in as input from user
# Split input by spaces and convert all elements to int
puzzle = [int(x) for x in input().split()]

root_node = Node(puzzle, None, '0')

#Example puzzles:
#DFS
# 0 2 3 4 1 6 7 8 5 9 10 11 <- solved instantaneously
# 0 2 3 4 1 5 7 8 6 9 10 11 <- takes about 1 min to solve
# 0 2 3 4 1 5 7 8 6 9 10 11 <- takes about 30 secs to solve

#BFS with sum of permutation of inversions
# 0 2 3 4 1 6 7 8 5 9 10 11 <- solved instantaneously
# 0 2 3 4 1 5 7 8 6 9 10 11 <- solved instantaneously

print ("Is puzzle valid? "+str(utilities.valid(root_node)))
print ("Is puzzle in goal state? "+str(utilities.goal(root_node)))
goal_node = depth_first.search(root_node)
print ("------ Reached Goal State -------")
print ("Config: "+str(goal_node.config))
print ("Parent config: "+str(goal_node.parent.config))
print ("Parent: "+str(goal_node.parent))
print ("Move: "+goal_node.move)
start_time = time.time()
goal_node = best_first.search(root_node)

solution_path = utilities.find_solution_path(goal_node)
#print("Solution path is: "+str(solution_path))
#for x in solution_path:
# print (x.move+" "+str(x.config))
utilities.write_to_file(solution_path, 'puzzleDFS.txt')

utilities.write_to_file(solution_path, 'puzzleBFS.txt')

#########Code commented out used for testing various functions ##################
#testVisited = generateChildren(root_node)
#testChildren = [Node([1, 2, 3, 4, 5, 6, 7, 0, 8, 9, 10, 11], root_node, 'x'),Node([1, 2, 3, 4, 5, 6, 0, 7, 8, 9, 10, 11], root_node, 'x')]
#for child in testChildren:
# print ("Config: "+str(child.config))
#testChildren = unvisitedChildren(testChildren, testVisited)
#print ("---------------------------------")
#for child in testChildren:
# print ("Config: "+str(child.config))
print ("Program execution time: {}".format(time.time()-start_time))

#testChildren = generateChildren(root_node)
#for child in testChildren:
# print ("Parent config: "+str(child.parent.config))
# print ("Config: "+str(child.config))
# print ("Parent: "+str(child.parent))
# print ("Move: "+child.move)
# print ("\n")