Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aimacode
GitHub Repository: aimacode/aima-python
Path: blob/master/gui/vacuum_agent.py
621 views
1
import os.path
2
from tkinter import *
3
4
from agents import *
5
6
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
7
8
loc_A, loc_B = (0, 0), (1, 0) # The two locations for the Vacuum world
9
10
11
class Gui(Environment):
12
"""This GUI environment has two locations, A and B. Each can be Dirty
13
or Clean. The agent perceives its location and the location's
14
status."""
15
16
def __init__(self, root, height=300, width=380):
17
super().__init__()
18
self.status = {loc_A: 'Clean',
19
loc_B: 'Clean'}
20
self.root = root
21
self.height = height
22
self.width = width
23
self.canvas = None
24
self.buttons = []
25
self.create_canvas()
26
self.create_buttons()
27
28
def thing_classes(self):
29
"""The list of things which can be used in the environment."""
30
return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,
31
TableDrivenVacuumAgent, ModelBasedVacuumAgent]
32
33
def percept(self, agent):
34
"""Returns the agent's location, and the location status (Dirty/Clean)."""
35
return agent.location, self.status[agent.location]
36
37
def execute_action(self, agent, action):
38
"""Change the location status (Dirty/Clean); track performance.
39
Score 10 for each dirt cleaned; -1 for each move."""
40
if action == 'Right':
41
agent.location = loc_B
42
agent.performance -= 1
43
elif action == 'Left':
44
agent.location = loc_A
45
agent.performance -= 1
46
elif action == 'Suck':
47
if self.status[agent.location] == 'Dirty':
48
if agent.location == loc_A:
49
self.buttons[0].config(bg='white', activebackground='light grey')
50
else:
51
self.buttons[1].config(bg='white', activebackground='light grey')
52
agent.performance += 10
53
self.status[agent.location] = 'Clean'
54
55
def default_location(self, thing):
56
"""Agents start in either location at random."""
57
return random.choice([loc_A, loc_B])
58
59
def create_canvas(self):
60
"""Creates Canvas element in the GUI."""
61
self.canvas = Canvas(
62
self.root,
63
width=self.width,
64
height=self.height,
65
background='powder blue')
66
self.canvas.pack(side='bottom')
67
68
def create_buttons(self):
69
"""Creates the buttons required in the GUI."""
70
button_left = Button(self.root, height=4, width=12, padx=2, pady=2, bg='white')
71
button_left.config(command=lambda btn=button_left: self.dirt_switch(btn))
72
self.buttons.append(button_left)
73
button_left_window = self.canvas.create_window(130, 200, anchor=N, window=button_left)
74
button_right = Button(self.root, height=4, width=12, padx=2, pady=2, bg='white')
75
button_right.config(command=lambda btn=button_right: self.dirt_switch(btn))
76
self.buttons.append(button_right)
77
button_right_window = self.canvas.create_window(250, 200, anchor=N, window=button_right)
78
79
def dirt_switch(self, button):
80
"""Gives user the option to put dirt in any tile."""
81
bg_color = button['bg']
82
if bg_color == 'saddle brown':
83
button.config(bg='white', activebackground='light grey')
84
elif bg_color == 'white':
85
button.config(bg='saddle brown', activebackground='light goldenrod')
86
87
def read_env(self):
88
"""Reads the current state of the GUI."""
89
for i, btn in enumerate(self.buttons):
90
if i == 0:
91
if btn['bg'] == 'white':
92
self.status[loc_A] = 'Clean'
93
else:
94
self.status[loc_A] = 'Dirty'
95
else:
96
if btn['bg'] == 'white':
97
self.status[loc_B] = 'Clean'
98
else:
99
self.status[loc_B] = 'Dirty'
100
101
def update_env(self, agent):
102
"""Updates the GUI according to the agent's action."""
103
self.read_env()
104
# print(self.status)
105
before_step = agent.location
106
self.step()
107
# print(self.status)
108
# print(agent.location)
109
move_agent(self, agent, before_step)
110
111
112
def create_agent(env, agent):
113
"""Creates the agent in the GUI and is kept independent of the environment."""
114
env.add_thing(agent)
115
# print(agent.location)
116
if agent.location == (0, 0):
117
env.agent_rect = env.canvas.create_rectangle(80, 100, 175, 180, fill='lime green')
118
env.text = env.canvas.create_text(128, 140, font="Helvetica 10 bold italic", text="Agent")
119
else:
120
env.agent_rect = env.canvas.create_rectangle(200, 100, 295, 180, fill='lime green')
121
env.text = env.canvas.create_text(248, 140, font="Helvetica 10 bold italic", text="Agent")
122
123
124
def move_agent(env, agent, before_step):
125
"""Moves the agent in the GUI when 'next' button is pressed."""
126
if agent.location == before_step:
127
pass
128
else:
129
if agent.location == (1, 0):
130
env.canvas.move(env.text, 120, 0)
131
env.canvas.move(env.agent_rect, 120, 0)
132
elif agent.location == (0, 0):
133
env.canvas.move(env.text, -120, 0)
134
env.canvas.move(env.agent_rect, -120, 0)
135
136
137
# TODO: Add more agents to the environment.
138
# TODO: Expand the environment to XYEnvironment.
139
if __name__ == "__main__":
140
root = Tk()
141
root.title("Vacuum Environment")
142
root.geometry("420x380")
143
root.resizable(0, 0)
144
frame = Frame(root, bg='black')
145
# reset_button = Button(frame, text='Reset', height=2, width=6, padx=2, pady=2, command=None)
146
# reset_button.pack(side='left')
147
next_button = Button(frame, text='Next', height=2, width=6, padx=2, pady=2)
148
next_button.pack(side='left')
149
frame.pack(side='bottom')
150
env = Gui(root)
151
agent = ReflexVacuumAgent()
152
create_agent(env, agent)
153
next_button.config(command=lambda: env.update_env(agent))
154
root.mainloop()
155
156