-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvi_gui.py
More file actions
391 lines (304 loc) · 14.2 KB
/
vi_gui.py
File metadata and controls
391 lines (304 loc) · 14.2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python3
"""
Value Iteration GUI - Python conversion of VIgui.java
This application demonstrates value iteration for a grid world problem.
Converted from Java Swing to Python tkinter.
Original Java code by David Poole, UBC
Python conversion maintains the same functionality and layout.
"""
import tkinter as tk
from tkinter import ttk, scrolledtext
# import numpy as np # Temporarily disabled due to installation issues
import math
from typing import List, Tuple, Optional
class VICore:
"""
Core value iteration logic - implements the algorithm referenced by the GUI.
Based on usage patterns observed in the original Java code.
"""
def __init__(self):
self.discount = 0.9
self.absorbing = False
# Initialize 10x10 grid
self.grid_size = 10
self.values = [[0.0 for _ in range(self.grid_size)] for _ in range(self.grid_size)]
# Q-values for 4 actions: up, right, down, left
self.qvalues = [[[0.0 for _ in range(4)] for _ in range(self.grid_size)] for _ in range(self.grid_size)]
# Rewards (can be customized)
self.rewards = [[0.0 for _ in range(self.grid_size)] for _ in range(self.grid_size)]
# Set up some sample rewards for demonstration
self.rewards[8][8] = 10.0 # Goal state
self.rewards[2][2] = -5.0 # Obstacle
self.rewards[7][3] = -5.0 # Obstacle
def doreset(self, initial_value: float = 0.0):
"""Reset all values to initial value"""
for x in range(self.grid_size):
for y in range(self.grid_size):
self.values[x][y] = initial_value
for a in range(4):
self.qvalues[x][y][a] = initial_value
def dostep(self, discount: float):
"""Perform one step of value iteration"""
self.discount = discount
# Store old values for convergence check
old_values = [[self.values[x][y] for y in range(self.grid_size)] for x in range(self.grid_size)]
# Update Q-values for each state-action pair
for x in range(self.grid_size):
for y in range(self.grid_size):
if self.absorbing and (self.rewards[x][y] != 0):
# Absorbing states keep their reward value
self.values[x][y] = self.rewards[x][y]
continue
# Calculate Q-values for each action
actions = [(0, -1), (1, 0), (0, 1), (-1, 0)] # up, right, down, left
for action_idx, (dx, dy) in enumerate(actions):
next_x, next_y = x + dx, y + dy
# Check bounds
if 0 <= next_x < self.grid_size and 0 <= next_y < self.grid_size:
next_value = old_values[next_x][next_y]
else:
next_value = old_values[x][y] # Stay in same state if hitting boundary
# Q-value = reward + discount * value of next state
self.qvalues[x][y][action_idx] = self.rewards[x][y] + self.discount * next_value
# Value is max over all Q-values
self.values[x][y] = max(self.qvalues[x][y])
class GridPanel(tk.Canvas):
"""Custom canvas for drawing the grid visualization"""
def __init__(self, parent, vi_core: VICore, **kwargs):
super().__init__(parent, **kwargs)
self.vi_core = vi_core
self.sqsize = 50
self.twid = 5
self.brightness = 1.0
self.font_size = 14
# Configure canvas
self.configure(width=self.sqsize * 10, height=self.sqsize * 10, bg='white')
def update_display(self):
"""Redraw the entire grid"""
self.delete("all")
# Draw grid cells with values
for x in range(10):
for y in range(10):
value = self.vi_core.values[x][y]
# Color based on value (green for positive, red for negative)
if value >= 0.0:
intensity = min(int(255 * (value / 10.0) ** self.brightness), 255)
color = f"#{0:02x}{intensity:02x}{0:02x}"
else:
intensity = min(int(255 * ((-value) / 10.0) ** self.brightness), 255)
color = f"#{intensity:02x}{0:02x}{0:02x}"
# Draw cell
x1, y1 = x * self.sqsize, y * self.sqsize
x2, y2 = (x + 1) * self.sqsize, (y + 1) * self.sqsize
self.create_rectangle(x1, y1, x2, y2, fill=color, outline='white')
# Draw optimal action arrow
self.draw_optimal_action(x, y)
# Draw value text
value_text = f"{value:.2f}"
self.create_text(x1 + self.sqsize//2, y1 + self.sqsize//2,
text=value_text, fill='white', font=('Arial', self.font_size))
# Draw grid lines
self.draw_grid_lines()
def draw_optimal_action(self, x: int, y: int):
"""Draw arrow indicating optimal action"""
center_x = x * self.sqsize + self.sqsize // 2
center_y = y * self.sqsize + self.sqsize // 2
# Find optimal action (action with highest Q-value)
qvals = self.vi_core.qvalues[x][y]
optimal_action = qvals.index(max(qvals))
# Draw arrow based on optimal action
if optimal_action == 0: # Up
points = [center_x - self.twid, center_y,
center_x + self.twid, center_y,
center_x, y * self.sqsize + 5]
elif optimal_action == 1: # Right
points = [center_x, center_y - self.twid,
center_x, center_y + self.twid,
(x + 1) * self.sqsize - 5, center_y]
elif optimal_action == 2: # Down
points = [center_x - self.twid, center_y,
center_x + self.twid, center_y,
center_x, (y + 1) * self.sqsize - 5]
else: # Left
points = [center_x, center_y - self.twid,
center_x, center_y + self.twid,
x * self.sqsize + 5, center_y]
self.create_polygon(points, fill='blue')
def draw_grid_lines(self):
"""Draw the grid lines"""
# Outer boundary
self.create_rectangle(0, 0, 10 * self.sqsize, 10 * self.sqsize,
outline='blue', width=2, fill='')
# Internal grid lines
for i in range(1, 10):
# Vertical lines
self.create_line(i * self.sqsize, 0, i * self.sqsize, 10 * self.sqsize,
fill='white', width=1)
# Horizontal lines
self.create_line(0, i * self.sqsize, 10 * self.sqsize, i * self.sqsize,
fill='white', width=1)
class VIGui:
"""Main application class - Python equivalent of VIgui.java"""
def __init__(self):
self.root = tk.Tk()
self.root.title("Value Iteration Visualization")
self.root.geometry("800x600")
# Initialize core
self.core = VICore()
# GUI variables
self.discount_var = tk.StringVar(value=str(self.core.discount))
self.initial_value_var = tk.StringVar(value="0.0")
self.absorbing_var = tk.BooleanVar(value=False)
self.setup_gui()
# Initial display
self.grid_panel.update_display()
def setup_gui(self):
"""Set up the GUI layout"""
# Main frame
main_frame = ttk.Frame(self.root)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Left panel for grid
left_frame = ttk.Frame(main_frame)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Grid with scrollbars
self.grid_panel = GridPanel(left_frame, self.core)
# Scrollable canvas container
canvas_frame = ttk.Frame(left_frame)
canvas_frame.pack(fill=tk.BOTH, expand=True)
h_scroll = ttk.Scrollbar(canvas_frame, orient=tk.HORIZONTAL)
v_scroll = ttk.Scrollbar(canvas_frame, orient=tk.VERTICAL)
self.grid_panel.configure(scrollregion=self.grid_panel.bbox("all"),
xscrollcommand=h_scroll.set,
yscrollcommand=v_scroll.set)
h_scroll.configure(command=self.grid_panel.xview)
v_scroll.configure(command=self.grid_panel.yview)
self.grid_panel.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
v_scroll.pack(side=tk.RIGHT, fill=tk.Y)
h_scroll.pack(side=tk.BOTTOM, fill=tk.X)
# Right panel for controls
right_frame = ttk.Frame(main_frame)
right_frame.pack(side=tk.RIGHT, fill=tk.Y, padx=(10, 0))
self.setup_controls(right_frame)
def setup_controls(self, parent):
"""Set up control buttons and inputs"""
# Step button
step_frame = ttk.Frame(parent)
step_frame.pack(pady=10)
step_btn = ttk.Button(step_frame, text="Step", command=self.do_step)
step_btn.pack()
# Discount controls
discount_frame = ttk.LabelFrame(parent, text="Discount")
discount_frame.pack(pady=10, fill=tk.X)
discount_control_frame = ttk.Frame(discount_frame)
discount_control_frame.pack(pady=5)
ttk.Button(discount_control_frame, text="-", width=3,
command=lambda: self.adjust_discount(-0.1)).pack(side=tk.LEFT)
discount_entry = ttk.Entry(discount_control_frame, textvariable=self.discount_var, width=8)
discount_entry.pack(side=tk.LEFT, padx=5)
ttk.Button(discount_control_frame, text="+", width=3,
command=lambda: self.adjust_discount(0.1)).pack(side=tk.LEFT)
# Reset controls
reset_frame = ttk.Frame(parent)
reset_frame.pack(pady=10)
reset_btn = ttk.Button(reset_frame, text="Reset", command=self.do_reset)
reset_btn.pack()
# Initial value
initial_frame = ttk.LabelFrame(parent, text="Initial Value")
initial_frame.pack(pady=10, fill=tk.X)
initial_entry = ttk.Entry(initial_frame, textvariable=self.initial_value_var, width=10)
initial_entry.pack(pady=5)
# Display controls
display_frame = ttk.LabelFrame(parent, text="Display")
display_frame.pack(pady=10, fill=tk.X)
# Brightness
brightness_frame = ttk.Frame(display_frame)
brightness_frame.pack(pady=5)
ttk.Label(brightness_frame, text="Brightness").pack()
brightness_control = ttk.Frame(brightness_frame)
brightness_control.pack()
ttk.Button(brightness_control, text="-", width=3,
command=lambda: self.adjust_brightness(1.1)).pack(side=tk.LEFT)
ttk.Button(brightness_control, text="0", width=3,
command=lambda: self.reset_brightness()).pack(side=tk.LEFT)
ttk.Button(brightness_control, text="+", width=3,
command=lambda: self.adjust_brightness(1/1.1)).pack(side=tk.LEFT)
# Font size
font_frame = ttk.Frame(display_frame)
font_frame.pack(pady=5)
ttk.Label(font_frame, text="Font Size").pack()
font_control = ttk.Frame(font_frame)
font_control.pack()
ttk.Button(font_control, text="-", width=3,
command=lambda: self.adjust_font(-1)).pack(side=tk.LEFT)
ttk.Button(font_control, text="+", width=3,
command=lambda: self.adjust_font(1)).pack(side=tk.LEFT)
# Grid size
grid_frame = ttk.Frame(display_frame)
grid_frame.pack(pady=5)
ttk.Label(grid_frame, text="Grid Size").pack()
grid_control = ttk.Frame(grid_frame)
grid_control.pack()
ttk.Button(grid_control, text="-", width=3,
command=lambda: self.adjust_grid_size(-5)).pack(side=tk.LEFT)
ttk.Button(grid_control, text="+", width=3,
command=lambda: self.adjust_grid_size(5)).pack(side=tk.LEFT)
# Absorbing states checkbox
absorbing_frame = ttk.Frame(parent)
absorbing_frame.pack(pady=10)
absorbing_check = ttk.Checkbutton(absorbing_frame, text="Absorbing States",
variable=self.absorbing_var,
command=self.toggle_absorbing)
absorbing_check.pack()
def do_step(self):
"""Perform one step of value iteration"""
try:
discount = float(self.discount_var.get())
self.core.dostep(discount)
self.grid_panel.update_display()
except ValueError:
pass # Invalid discount value, ignore
def do_reset(self):
"""Reset the value iteration"""
try:
initial_value = float(self.initial_value_var.get())
self.core.doreset(initial_value)
self.grid_panel.update_display()
except ValueError:
pass # Invalid initial value, ignore
def adjust_discount(self, delta: float):
"""Adjust discount factor"""
try:
current = float(self.discount_var.get())
new_value = max(0.0, min(1.0, current + delta)) # Clamp between 0 and 1
self.discount_var.set(f"{new_value:.2f}")
self.grid_panel.update_display()
except ValueError:
pass
def adjust_brightness(self, factor: float):
"""Adjust brightness"""
self.grid_panel.brightness *= factor
self.grid_panel.update_display()
def reset_brightness(self):
"""Reset brightness to default"""
self.grid_panel.brightness = 1.0
self.grid_panel.update_display()
def adjust_font(self, delta: int):
"""Adjust font size"""
self.grid_panel.font_size = max(8, self.grid_panel.font_size + delta)
self.grid_panel.update_display()
def adjust_grid_size(self, delta: int):
"""Adjust grid cell size"""
self.grid_panel.sqsize = max(20, self.grid_panel.sqsize + delta)
self.grid_panel.configure(width=self.grid_panel.sqsize * 10,
height=self.grid_panel.sqsize * 10)
self.grid_panel.update_display()
def toggle_absorbing(self):
"""Toggle absorbing states"""
self.core.absorbing = self.absorbing_var.get()
self.grid_panel.update_display()
def run(self):
"""Start the application"""
self.root.mainloop()
if __name__ == "__main__":
app = VIGui()
app.run()