Skip to content

Adding Tkinter GUI #661

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

Merged
merged 8 commits into from
Dec 20, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
Added Depth-First Graph Search
  • Loading branch information
apb7 committed Dec 19, 2017
commit 7e3dc209d587b59003b48ebceabbe1e8c0a5662f
74 changes: 63 additions & 11 deletions gui/romania_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from search import *
from search import breadth_first_tree_search as bfts, depth_first_tree_search as dfts
from search import breadth_first_tree_search as bfts, depth_first_tree_search as dfts,depth_first_graph_search as dfgs
from utils import Stack, FIFOQueue, PriorityQueue
from copy import deepcopy
root = None
Expand All @@ -19,7 +19,7 @@
front = None
node = None
next_button = None

explored=None

def create_map(root):
'''
Expand Down Expand Up @@ -152,6 +152,15 @@ def create_map(root):
height -
romania_locations['Pitesti'][1],
romania_map.get('Rimnicu', 'Pitesti'))
make_line(
city_map,
romania_locations['Bucharest'][0],
height -
romania_locations['Bucharest'][1],
romania_locations['Pitesti'][0],
height -
romania_locations['Pitesti'][1],
romania_map.get('Bucharest', 'Pitesti'))
make_line(
city_map,
romania_locations['Fagaras'][0],
Expand Down Expand Up @@ -293,6 +302,9 @@ def make_legend(map):

def tree_search(problem):
'''
earch through the successors of a problem to find a goal.
The argument frontier should be an empty queue.
Don't worry about repeated paths to a state. [Figure 3.7]
This function has been changed to make it suitable for the Tkinter GUI.
'''
global counter, frontier, node
Expand All @@ -317,6 +329,34 @@ def tree_search(problem):
display_explored(node)
return None

def graph_search(problem):
'''
Search through the successors of a problem to find a goal.
The argument frontier should be an empty queue.
If two paths reach a state, only use the first one. [Figure 3.7]
This function has been changed to make it suitable for the Tkinter GUI.
'''
global counter,frontier,node,explored
if counter == -1:
frontier.append(Node(problem.initial))
explored=set()
display_frontier(frontier)
if counter % 3 ==0 and counter >=0:
node = frontier.pop()
display_current(node)
if counter % 3 == 1 and counter >= 0:
if problem.goal_test(node.state):
return node
explored.add(node.state)
frontier.extend(child for child in node.expand(problem)
if child.state not in explored and
child not in frontier)
display_frontier(frontier)
if counter % 3 == 2 and counter >= 0:
display_explored(node)
return None



def display_frontier(queue):
'''
Expand All @@ -329,8 +369,6 @@ def display_frontier(queue):
for city in city_coord.keys():
if node.state == city:
city_map.itemconfig(city_coord[city], fill="orange")
return


def display_current(node):
'''
Expand All @@ -339,8 +377,6 @@ def display_current(node):
global city_map, city_coord
city = node.state
city_map.itemconfig(city_coord[city], fill="red")
return


def display_explored(node):
'''
Expand All @@ -349,8 +385,6 @@ def display_explored(node):
global city_map, city_coord
city = node.state
city_map.itemconfig(city_coord[city], fill="gray")
return


def display_final(cities):
'''
Expand All @@ -359,8 +393,6 @@ def display_final(cities):
global city_map, city_coord
for city in cities:
city_map.itemconfig(city_coord[city], fill="green")
return


def breadth_first_tree_search(problem):
"""Search the shallowest nodes in the search tree first."""
Expand All @@ -378,6 +410,14 @@ def depth_first_tree_search(problem):
frontier=Stack()
return tree_search(problem)

# TODO: Check if the solution given by this function is consistent with the original function.
def depth_first_graph_search(problem):
"""Search the deepest nodes in the search tree first."""
global frontier, counter
if counter == -1:
frontier = Stack()
return graph_search(problem)

# TODO:
# Remove redundant code.
# Make the interchangbility work between various algorithms at each step.
Expand All @@ -403,6 +443,18 @@ def on_click():
display_final(final_path)
next_button.config(state="disabled")
counter += 1
elif "Depth-First Graph Search" == algo.get():
node = depth_first_graph_search(romania_problem)
if node is not None:
print(node)
final_path = dfgs(romania_problem).solution()
print(final_path)
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
counter += 1



def reset_map():
global counter, city_coord, city_map, next_button
Expand All @@ -427,7 +479,7 @@ def main():
goal.set('Bucharest')
cities = sorted(romania_map.locations.keys())
algorithm_menu = OptionMenu(
root, algo, "Breadth-First Tree Search", "Depth-First Tree Search")
root, algo, "Breadth-First Tree Search", "Depth-First Tree Search","Depth-First Graph Search")
Label(root, text="\n Search Algorithm").pack()
algorithm_menu.pack()
Label(root, text="\n Start City").pack()
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy