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
Next Next commit
Added Breadth-First Tree Search
  • Loading branch information
apb7 committed Dec 18, 2017
commit cd027d5498174b8b9ac251b5f6cdd057c0247514
201 changes: 168 additions & 33 deletions gui/romania_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
import math
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from search import *

from search import breadth_first_tree_search as bfts
from utils import Stack, FIFOQueue, PriorityQueue
from copy import deepcopy
root = None
city_coord = {}
romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)
algo=None
start=None
goal=None
romania_problem = None
algo = None
start = None
goal = None
counter = -1
city_map = None
frontier = None
front = None
node = None
next_button = None


def create_map(root):
'''
This function draws out the required map.
'''
global romania_problem
global city_map, start, goal
romania_locations = romania_map.locations
width = 750
height = 670
Expand Down Expand Up @@ -237,6 +245,7 @@ def create_map(root):

make_legend(city_map)


def make_line(map, x0, y0, x1, y1, distance):
'''
This function draws out the lines joining various points.
Expand All @@ -263,47 +272,173 @@ def make_rectangle(map, x0, y0, margin, city_name):
anchor=SE)
city_coord.update({city_name: rect})


def make_legend(map):
rect1=map.create_rectangle(600,100,610,110,fill="white")
text1 = map.create_text(615, 105,anchor=W,text="Un-explored")
rect2=map.create_rectangle(600,115,610,125,fill="orange")
text2 = map.create_text(615, 120,anchor=W,text="Frontier")
rect3=map.create_rectangle(600,130,610,140,fill="red")
text3 = map.create_text(615, 135,anchor=W,text="Currently Exploring")
rect4=map.create_rectangle(600,145,610,155,fill="grey")
text4 = map.create_text(615, 150,anchor=W,text="Explored")
rect5=map.create_rectangle(600,160,610,170,fill="dark green")

rect1 = map.create_rectangle(600, 100, 610, 110, fill="white")
text1 = map.create_text(615, 105, anchor=W, text="Un-explored")

rect2 = map.create_rectangle(600, 115, 610, 125, fill="orange")
text2 = map.create_text(615, 120, anchor=W, text="Frontier")

rect3 = map.create_rectangle(600, 130, 610, 140, fill="red")
text3 = map.create_text(615, 135, anchor=W, text="Currently Exploring")

rect4 = map.create_rectangle(600, 145, 610, 155, fill="grey")
text4 = map.create_text(615, 150, anchor=W, text="Explored")

rect5 = map.create_rectangle(600, 160, 610, 170, fill="dark green")
text5 = map.create_text(615, 165, anchor=W, text="Final Solution")



def tree_search(problem):
'''
This function has been changed to make it suitable for the Tkinter GUI.
'''
global counter, frontier, node
# print(counter)
if counter == -1:
frontier.append(Node(problem.initial))
# print(frontier)
display_frontier(frontier)
if counter % 3 == 0 and counter >= 0:
node = frontier.pop()
# print(node)
display_current(node)
if counter % 3 == 1 and counter >= 0:
if problem.goal_test(node.state):
# print(node)
return node
frontier.extend(node.expand(problem))
# print(frontier)
display_frontier(frontier)
if counter % 3 == 2 and counter >= 0:
# print(node)
display_explored(node)
return None


def display_frontier(queue):
'''
This function marks the frontier nodes (orange) on the map.
'''
global city_map, city_coord
qu = deepcopy(queue)
while qu:
node = qu.pop()
for city in city_coord.keys():
if node.state == city:
city_map.itemconfig(city_coord[city], fill="orange")
return


def display_current(node):
'''
This function marks the currently exploring node (red) on the map.
'''
global city_map, city_coord
city = node.state
city_map.itemconfig(city_coord[city], fill="red")
return


def display_explored(node):
'''
This function marks the already explored node (gray) on the map.
'''
global city_map, city_coord
city = node.state
city_map.itemconfig(city_coord[city], fill="gray")
return


def display_final(cities):
'''
This function marks the final solution nodes (green) on the map.
'''
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."""
global frontier, counter
if counter == -1:
frontier = FIFOQueue()
return tree_search(problem)


def on_click():
'''
This function defines the action of the 'Next' button.
'''
global algo, counter, next_button, romania_problem, start, goal
romania_problem = GraphProblem(start.get(), goal.get(), romania_map)
if "Breadth-First Tree Search" == algo.get():
node = breadth_first_tree_search(romania_problem)
if node is not None:
final_path = bfts(romania_problem).solution()
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
counter = -1
for city in city_coord.keys():
city_map.itemconfig(city_coord[city], fill="white")
next_button.config(state="normal")

# TODO: Add more search algorithms in the OptionMenu


def main():
global algo,start,goal
global algo, start, goal, next_button
root = Tk()
root.title("Road Map of Romania")
root.geometry("950x1150")
algo=StringVar(root)
algo = StringVar(root)
start = StringVar(root)
goal = StringVar(root)
algo.set("Breadth First Tree Search")
algo.set("Breadth-First Tree Search")
start.set('Arad')
goal.set('Bucharest')
cities=list(romania_map.locations.keys())
cities.sort()
algorithm_menu=OptionMenu(root,algo,"Breadth-First Tree Search","Depth-First Tree Search")
Label(root,text="\n Search Algorithm").pack()
cities = sorted(romania_map.locations.keys())
algorithm_menu = OptionMenu(root, algo, "Breadth-First Tree Search")
Label(root, text="\n Search Algorithm").pack()
algorithm_menu.pack()
Label(root,text="\n Start City").pack()
start_menu = OptionMenu(root,start,*cities)
Label(root, text="\n Start City").pack()
start_menu = OptionMenu(root, start, *cities)
start_menu.pack()
Label(root, text="\n Goal City").pack()
goal_menu = OptionMenu(root,goal,*cities)
goal_menu = OptionMenu(root, goal, *cities)
goal_menu.pack()
next_button = Button(root, width=6, height=2, text="Next", command=None,padx=2,pady=2,relief=GROOVE)
next_button.pack(side=BOTTOM)
frame1 = Frame(root)
next_button = Button(
frame1,
width=6,
height=2,
text="Next",
command=on_click,
padx=2,
pady=2,
relief=GROOVE)
next_button.pack(side=RIGHT)
reset_button = Button(
frame1,
width=6,
height=2,
text="Reset",
command=reset_map,
padx=2,
pady=2,
relief=GROOVE)
reset_button.pack(side=RIGHT)
frame1.pack(side=BOTTOM)
create_map(root)
root.mainloop()

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