Kalletlamadhav/sql-optimization-env
0
1# curriculum/curriculum_engine.py2 3import json4from pathlib import Path5from .level_config import LEVEL_CONFIG6 7STATE_FILE = Path('/tmp/curriculum_state.json')8 9 10class CurriculumEngine:11 12 GRADUATE_THRESHOLD = 0.7013 GRADUATE_CONSECUTIVE = 314 REMEDIATE_THRESHOLD = 0.4015 REMEDIATE_CONSECUTIVE = 216 MAX_LEVEL = 517 MIN_LEVEL = 118 19 def __init__(self):20 self._load_state()21 22 def _load_state(self):23 if STATE_FILE.exists():24 with open(STATE_FILE) as f:25 s = json.load(f)26 27 self.current_level = s.get('level', 1)28 self.recent_scores = s.get('recent_scores', [])29 self.total_episodes = s.get('total_episodes', 0)30 31 else:32 self.current_level = 133 self.recent_scores = []34 self.total_episodes = 035 36 def _save_state(self):37 with open(STATE_FILE, 'w') as f:38 json.dump({39 'level': self.current_level,40 'recent_scores': self.recent_scores[-10:],41 'total_episodes': self.total_episodes42 }, f)43 44 def record_episode(self, score: float):45 self.recent_scores.append(round(score, 4))46 self.total_episodes += 147 48 self._evaluate_transition()49 self._save_state()50 51 def _evaluate_transition(self):52 53 if len(self.recent_scores) < self.GRADUATE_CONSECUTIVE:54 return55 56 last_n = self.recent_scores[-self.GRADUATE_CONSECUTIVE:]57 58 # Graduation59 if all(s >= self.GRADUATE_THRESHOLD for s in last_n):60 if self.current_level < self.MAX_LEVEL:61 self.current_level += 162 self.recent_scores = []63 print(f'CURRICULUM: Graduated to Level {self.current_level}')64 return65 66 # Remediation67 last_n_fail = self.recent_scores[-self.REMEDIATE_CONSECUTIVE:]68 69 if all(s < self.REMEDIATE_THRESHOLD for s in last_n_fail):70 if self.current_level > self.MIN_LEVEL:71 self.current_level -= 172 self.recent_scores = []73 print(f'CURRICULUM: Stepped down to Level {self.current_level}')