-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
115 lines (86 loc) · 3.28 KB
/
Copy pathevaluate.py
File metadata and controls
115 lines (86 loc) · 3.28 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
#!/usr/bin/env pybricks-micropython
"""
Evaluation / Final Exam Script for EV3 Q-Learning Line Follower Agent.
"""
import sys
import os
# Ensure project root is in sys.path (MicroPython os.path fallback)
try:
import os.path
current_dir = os.path.dirname(os.path.abspath(__file__))
except (ImportError, AttributeError, NameError):
current_dir = "."
if current_dir and current_dir not in sys.path:
sys.path.append(current_dir)
try:
from pybricks.tools import wait
except ImportError:
import time
def wait(ms):
time.sleep(ms / 1000.0)
from config import settings
from hardware.robot import RobotInterface
from hardware.reflexes import hardcoded_obstacle_avoidance
from core.agent import QLearningAgent
from core.environment import Environment
def file_exists(filename):
"""MicroPython safe file existence check."""
try:
os.stat(filename)
return True
except Exception:
return False
def evaluate_agent(max_iterations=None, use_simulator=False):
"""
Evaluation loop executing pure Q-table exploitation with track direction detection.
"""
robot = RobotInterface(use_simulator=use_simulator)
agent = QLearningAgent(n_states=settings.NUM_STATES, n_actions=settings.NUM_ACTIONS)
env = Environment()
print("==================================================")
print("Starting EV3 Robot Evaluation...")
print("==================================================")
model_filename = "models/cw_q_table_8state.pkl"
fallback_filename = "models/cw_q_table.pkl"
if file_exists(model_filename):
load_path = model_filename
elif file_exists(fallback_filename):
load_path = fallback_filename
else:
load_path = model_filename
try:
agent.load(load_path)
except Exception as e:
print("[Evaluate] Warning: Failed to load {}: {}. Agent will evaluate with zeroed Q-table.".format(load_path, e))
# Set epsilon = 0.0 for pure exploitation
epsilon = 0.0
print("[Evaluate] Epsilon set to 0.0 (Pure Exploitation Mode).")
iteration = 0
try:
while True:
if max_iterations is not None and iteration >= max_iterations:
print("[Evaluate] Reached maximum evaluation iterations ({}).".format(max_iterations))
break
# RULE D: Non-RL Reflex for Obstacle Avoidance
if robot.read_ir() < settings.OBSTACLE_DISTANCE_THRESHOLD:
print("[Evaluate] Obstacle detected by IR sensor! Executing reflex.")
hardcoded_obstacle_avoidance(robot)
continue
# 1. Read current intensity gradient
intensity = robot.read_intensity()
# 2. Get state
state = env.get_state(intensity)
# 3. Select best action (pure exploitation)
action = agent.choose_action(state, epsilon=0.0)
# 4. Execute action
robot.execute_action(action)
wait(settings.DEFAULT_STEP_TIME_MS)
iteration += 1
except KeyboardInterrupt:
print("\n[Evaluate] Program interrupted by user.")
finally:
robot.stop()
print("[Evaluate] Robot safely stopped.")
if __name__ == "__main__":
# If run as main, execute evaluation loop
evaluate_agent(use_simulator=False)