Spaces:
Paused
Paused
File size: 12,331 Bytes
a52f96d |
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 |
"""Enhanced mock student agent with PPO-like features: transfer learning, exponential learning curves."""
import random
from typing import Dict, List, Set, Optional
import numpy as np
from interfaces import Task, StudentState, StudentAgentInterface
class MockStudentAgent(StudentAgentInterface):
"""
Enhanced mock student with PPO-like features:
- Learning: improves with practice (exponential when guided, linear when random)
- Forgetting: Ebbinghaus curve
- Per-topic skill tracking
- Transfer learning: skills in related topics help each other
- Feature representations: abstract concepts that transfer across topics
- Exponential learning curve when teacher-guided (coherent curriculum)
- Stochastic/erratic learning when random
"""
def __init__(
self,
learning_rate: float = 0.15,
forgetting_rate: float = 0.01, # Reduced for long training
transfer_strength: float = 0.3, # How much skills transfer between topics
seed: int = 42,
curriculum_coherence: Optional[float] = None # Track if teacher-guided
):
"""
Initialize enhanced mock student.
Args:
learning_rate: Base learning rate (0-1)
forgetting_rate: How fast retention decays
transfer_strength: How much skills transfer (0-1)
seed: Random seed
curriculum_coherence: Track if following coherent curriculum (auto-detected)
"""
self.learning_rate = learning_rate
self.forgetting_rate = forgetting_rate
self.transfer_strength = transfer_strength
self.rng = random.Random(seed)
# Track per-topic base skill (0.0 to 1.0)
self.topic_skills: Dict[str, float] = {}
# PPO-like: Feature representations (abstract concepts that transfer)
# Groups of related topics share feature representations
self.feature_representations: Dict[str, Set[str]] = self._build_feature_groups()
# Track history
self.topic_attempts: Dict[str, int] = {}
self.last_practice_time: Dict[str, float] = {}
# Time tracking for forgetting simulation
self.current_time = 0.0
self.total_timesteps = 0
# Track curriculum coherence (exponential learning vs stochastic)
self.curriculum_coherence = curriculum_coherence
self.recent_topics: List[str] = [] # Track recent topic sequence
self.recent_topics_window = 5
# Expanded difficulty learning factors (all 7 levels)
self.difficulty_factors = {
'trivial': 1.2, # Very easy, learn quickly
'easy': 1.0, # Standard easy
'medium': 0.8, # Moderate
'hard': 0.6, # Challenging
'expert': 0.4, # Very hard (multi-step)
'master': 0.25, # Extremely hard
'grandmaster': 0.15 # Maximum difficulty
}
# Multi-step penalty: harder difficulties need more practice
self.multi_step_penalty = {
'trivial': 0.0,
'easy': 0.0,
'medium': 0.1,
'hard': 0.2,
'expert': 0.3,
'master': 0.4,
'grandmaster': 0.5
}
def _build_feature_groups(self) -> Dict[str, Set[str]]:
"""Build groups of related topics for transfer learning."""
# Group related topics that share underlying concepts
return {
'stem_concepts': {'mathematics', 'programming', 'science', 'physics', 'chemistry'},
'humanities_concepts': {'history', 'literature', 'philosophy', 'art'},
'social_concepts': {'current_events', 'economics', 'psychology', 'geography'},
'abstract_reasoning': {'mathematics', 'programming', 'philosophy'},
'memorization': {'history', 'geography', 'biology', 'chemistry'}
}
def _get_transfer_boost(self, topic: str) -> float:
"""
Calculate transfer learning boost from related topics.
Returns:
Multiplier for learning rate based on related topic skills
"""
boost = 0.0
# Find which feature groups this topic belongs to
for feature_name, topics in self.feature_representations.items():
if topic in topics:
# Get average skill from related topics
related_skills = [
self.topic_skills.get(t, 0.0)
for t in topics
if t != topic and t in self.topic_skills
]
if related_skills:
avg_related_skill = np.mean(related_skills)
# Transfer boost proportional to related skills
boost += self.transfer_strength * avg_related_skill * 0.5
return min(boost, 0.5) # Cap at 50% boost
def _get_curriculum_coherence(self) -> float:
"""
Detect if student is following coherent curriculum (teacher-guided).
Returns:
Coherence score (0.0 = random, 1.0 = very coherent)
"""
if len(self.recent_topics) < 3:
return 0.5 # Neutral
# Check if topics are related (same feature groups)
recent_set = set(self.recent_topics[-3:])
coherence_score = 0.0
for feature_name, topics in self.feature_representations.items():
if recent_set.issubset(topics) or len(recent_set.intersection(topics)) >= 2:
coherence_score += 0.3
# Check for progressive difficulty or review patterns
if len(self.recent_topics) >= 2:
# If topics repeat (review) or progress logically
if self.recent_topics[-1] == self.recent_topics[-2]:
coherence_score += 0.2 # Review pattern
return min(coherence_score, 1.0)
def answer(self, task: Task) -> int:
"""
Answer a task based on effective skill (accounting for forgetting and transfer).
Returns:
Index of chosen answer (0-3)
"""
effective_skill = self._get_effective_skill(task.topic)
# Probability of correct = 0.25 (random) + 0.75 * effective_skill
prob_correct = 0.25 + 0.75 * effective_skill
if self.rng.random() < prob_correct:
return task.answer
else:
wrong_answers = [i for i in range(4) if i != task.answer]
return self.rng.choice(wrong_answers)
def learn(self, task: Task) -> bool:
"""
Learn from a task with PPO-like features.
Features:
- Transfer learning: Related topics boost learning
- Exponential learning: Coherent curriculum accelerates learning
- Multi-step penalty: Harder tasks need more practice
- Adaptive learning: Learning rate adjusts based on context
Returns:
Whether answer was correct
"""
was_correct = (self.answer(task) == task.answer)
topic = task.topic
difficulty = task.difficulty
# Initialize if new topic
if topic not in self.topic_skills:
self.topic_skills[topic] = 0.0
self.topic_attempts[topic] = 0
self.last_practice_time[topic] = self.current_time
current_base_skill = self.topic_skills[topic]
difficulty_factor = self.difficulty_factors.get(difficulty, 0.5)
# PPO-like: Transfer learning boost
transfer_boost = self._get_transfer_boost(topic)
# PPO-like: Curriculum coherence (exponential learning when guided)
coherence = self._get_curriculum_coherence()
curriculum_multiplier = 1.0 + (coherence * 0.5) # Up to 1.5x with coherent curriculum
# Update recent topics for coherence tracking
self.recent_topics.append(topic)
if len(self.recent_topics) > self.recent_topics_window:
self.recent_topics.pop(0)
# Learning multiplier based on correctness
if was_correct:
learning_multiplier = 1.0
else:
learning_multiplier = 0.3
# Multi-step penalty for very hard tasks
steps = self._get_steps_for_difficulty(difficulty)
step_penalty = 1.0 - (self.multi_step_penalty.get(difficulty, 0.0) * steps)
# Exponential learning when guided, linear when random
if coherence > 0.6: # Teacher-guided (coherent)
# Exponential: faster learning as skills accumulate
skill_gap = 1.0 - current_base_skill
exponential_factor = 1.0 + (current_base_skill * 0.5) # Accelerates with skill
else: # Random/progressive (incoherent)
# Linear: constant learning rate
skill_gap = 1.0 - current_base_skill
exponential_factor = 1.0 # No acceleration
skill_increase = (
self.learning_rate *
difficulty_factor *
learning_multiplier *
skill_gap *
(1.0 + transfer_boost) * # Transfer learning
curriculum_multiplier * # Curriculum coherence
step_penalty * # Multi-step penalty
exponential_factor # Exponential vs linear
)
self.topic_skills[topic] = min(1.0, current_base_skill + skill_increase)
self.topic_attempts[topic] = self.topic_attempts.get(topic, 0) + 1
self.last_practice_time[topic] = self.current_time
self.total_timesteps += 1
return was_correct
def _get_steps_for_difficulty(self, difficulty: str) -> int:
"""Determine number of reasoning steps for a difficulty level."""
step_map = {
'trivial': 1,
'easy': 1,
'medium': 2,
'hard': 3,
'expert': 4,
'master': 5,
'grandmaster': 6
}
return step_map.get(difficulty, 1)
def _get_effective_skill(self, topic: str) -> float:
"""
Get effective skill accounting for forgetting (Ebbinghaus curve).
Formula: effective_skill = base_skill * retention
retention = exp(-forgetting_rate * time_since_practice)
"""
if topic not in self.topic_skills:
return 0.0
base_skill = self.topic_skills[topic]
time_since = self.current_time - self.last_practice_time.get(topic, self.current_time)
# Ebbinghaus forgetting curve
retention = np.exp(-self.forgetting_rate * time_since)
# Effective skill = base skill reduced by forgetting
effective_skill = base_skill * retention
return max(0.0, min(1.0, effective_skill))
def evaluate(self, eval_tasks: List[Task]) -> float:
"""
Evaluate student on a list of tasks.
Returns:
Accuracy (0.0 to 1.0)
"""
if not eval_tasks:
return 0.0
correct = 0
for task in eval_tasks:
answer = self.answer(task)
if answer == task.answer:
correct += 1
return correct / len(eval_tasks)
def get_state(self) -> StudentState:
"""Get current student state."""
topic_accuracies = {}
for topic in self.topic_skills.keys():
effective_skill = self._get_effective_skill(topic)
topic_accuracies[topic] = 0.25 + 0.75 * effective_skill
time_since_practice = {}
for topic in self.last_practice_time:
time_since_practice[topic] = self.current_time - self.last_practice_time[topic]
return StudentState(
topic_accuracies=topic_accuracies,
topic_attempts=self.topic_attempts.copy(),
time_since_practice=time_since_practice,
total_timesteps=self.total_timesteps,
current_time=self.current_time
)
def advance_time(self, delta: float = 1.0):
"""Advance time for forgetting simulation."""
self.current_time += delta
|