-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
263 lines (215 loc) · 7.74 KB
/
validate.py
File metadata and controls
263 lines (215 loc) · 7.74 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
"""
Pre-submission validation script for AdaptiveTutor-Env.
Checks all items on the Pre-Submission Checklist:
1. openenv.yaml is valid and parseable
2. All typed models are importable
3. reset() returns a valid Observation
4. step() returns valid (Observation, Reward, done, info)
5. state() returns a dict with required keys
6. 3+ tasks exist
7. Each grader produces a score in [0.0, 1.0]
8. Graders are deterministic
Run with: python validate.py
"""
from __future__ import annotations
import sys
import yaml
import traceback
from pathlib import Path
from typing import List, Tuple
PASS = "✅"
FAIL = "❌"
results: List[Tuple[str, bool, str]] = []
def check(name: str, fn) -> bool:
try:
msg = fn()
results.append((name, True, msg or "OK"))
return True
except Exception as e:
results.append((name, False, str(e)))
return False
# ─── Checks ───────────────────────────────────────────────────────────────────
def check_yaml():
with open("openenv.yaml") as f:
data = yaml.safe_load(f)
required = [
"name",
"version",
"description",
"tasks",
"action_space",
"observation_space",
]
missing = [k for k in required if k not in data]
if missing:
raise ValueError(f"Missing keys in openenv.yaml: {missing}")
n_tasks = len(data["tasks"])
return f"YAML valid. {n_tasks} tasks defined."
def check_imports():
from server.models import Action, Observation, Reward, StepResponse
from server.environment import AdaptiveTutorEnv
from server.tasks import TASK_CATALOGUE
from server.reward import compute_reward
return "All modules imported successfully."
def check_reset():
from server.environment import AdaptiveTutorEnv
env = AdaptiveTutorEnv()
obs = env.reset(task_id="single_subject_mastery", seed=42)
assert obs.step == 0
assert obs.task_id == "single_subject_mastery"
assert 0.0 <= obs.overall_mastery <= 1.0
assert obs.last_action is None
return f"reset() OK. Initial mastery={obs.overall_mastery:.3f}"
def check_step():
from server.environment import AdaptiveTutorEnv
from server.models import Action
env = AdaptiveTutorEnv()
env.reset(seed=0)
result = env.step(
Action(
subject="mathematics",
topic="algebra",
activity_type=1,
difficulty=0,
strategy=0,
)
)
assert hasattr(result, "observation")
assert hasattr(result, "reward")
assert isinstance(result.done, bool)
assert -1.0 <= result.reward.total <= 1.0
return f"step() OK. reward={result.reward.total:.4f} done={result.done}"
def check_state():
from server.environment import AdaptiveTutorEnv
env = AdaptiveTutorEnv()
env.reset(seed=0)
s = env.state()
required = ["initialized", "task_id", "step", "masteries", "engagement", "fatigue"]
missing = [k for k in required if k not in s]
if missing:
raise ValueError(f"state() missing keys: {missing}")
return f"state() OK. Keys present: {list(s.keys())[:6]}..."
def check_tasks():
from server.tasks import TASK_CATALOGUE
assert len(TASK_CATALOGUE) >= 3, f"Only {len(TASK_CATALOGUE)} tasks found"
diffs = [t.difficulty for t in TASK_CATALOGUE.values()]
assert "easy" in diffs, "No easy task"
assert "medium" in diffs, "No medium task"
assert "hard" in diffs, "No hard task"
return f"{len(TASK_CATALOGUE)} tasks: {list(TASK_CATALOGUE.keys())}"
def check_graders():
from server.environment import AdaptiveTutorEnv
from server.models import Action, SUBJECTS, TOPICS_PER_SUBJECT
from server.tasks import TASK_CATALOGUE
scores = {}
for task_id in TASK_CATALOGUE:
env = AdaptiveTutorEnv()
env.reset(task_id=task_id, seed=3)
done = False
step = 0
while not done and step < TASK_CATALOGUE[task_id].max_steps + 5:
subj = SUBJECTS[step % len(SUBJECTS)]
topic = TOPICS_PER_SUBJECT[subj][step % 5]
result = env.step(
Action(
subject=subj,
topic=topic,
activity_type=1,
difficulty=1,
strategy=1,
)
)
done = result.done
step += 1
grade_result = env.grade_episode()
assert 0.002 <= grade_result.score <= 0.998, (
f"Score {grade_result.score} out of range [0.002, 0.998] for task {task_id}"
)
scores[task_id] = round(grade_result.score, 4)
return f"Grader scores: {scores}"
def check_grader_determinism():
from server.environment import AdaptiveTutorEnv
from server.models import Action
env = AdaptiveTutorEnv()
env.reset(task_id="multi_subject_balancing", seed=7)
done = False
while not done:
result = env.step(
Action(
subject="mathematics",
topic="algebra",
activity_type=0,
difficulty=1,
strategy=1,
)
)
done = result.done
r1 = env.grade_episode()
r2 = env.grade_episode()
assert r1.score == r2.score, f"Non-deterministic grader: {r1.score} vs {r2.score}"
return f"Grader deterministic (score={r1.score:.4f})."
def check_reward_density():
from server.environment import AdaptiveTutorEnv
from server.models import Action, SUBJECTS, TOPICS_PER_SUBJECT
env = AdaptiveTutorEnv()
env.reset(task_id="multi_subject_balancing", seed=1)
rewards = []
for i in range(10):
subj = SUBJECTS[i % len(SUBJECTS)]
topic = TOPICS_PER_SUBJECT[subj][i % 5]
result = env.step(
Action(
subject=subj,
topic=topic,
activity_type=1,
difficulty=1,
strategy=1,
)
)
rewards.append(result.reward.total)
if result.done:
break
all_zero = all(r == 0.0 for r in rewards)
assert not all_zero, "All rewards are zero — reward function not working"
unique_vals = len(set(round(r, 4) for r in rewards))
return f"Dense rewards over {len(rewards)} steps. Unique values: {unique_vals}."
# ─── Run All Checks ───────────────────────────────────────────────────────────
CHECKS = [
("openenv.yaml valid", check_yaml),
("Module imports", check_imports),
("reset() API", check_reset),
("step() API", check_step),
("state() API", check_state),
("3+ tasks with difficulty range", check_tasks),
("Graders score in (0.002, 0.998)", check_graders),
("Graders deterministic", check_grader_determinism),
("Dense reward signal", check_reward_density),
]
def main():
print("=" * 60)
print("AdaptiveTutor-Env — Pre-Submission Validation")
print("=" * 60)
passed = 0
for name, fn in CHECKS:
ok = check(name, fn)
if ok:
passed += 1
print()
for name, ok, msg in results:
icon = PASS if ok else FAIL
print(f"{icon} {name}")
if not ok:
print(f" ↳ {msg}")
else:
print(f" ↳ {msg}")
print()
print(f"{'=' * 60}")
print(f"Result: {passed}/{len(CHECKS)} checks passed.")
if passed == len(CHECKS):
print("✅ All checks passed. Ready to submit!")
else:
print("❌ Some checks failed. Please fix before submitting.")
print("=" * 60)
sys.exit(0 if passed == len(CHECKS) else 1)
if __name__ == "__main__":
main()