-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc_experience_replay.py
More file actions
377 lines (314 loc) · 14.6 KB
/
Copy patharc_experience_replay.py
File metadata and controls
377 lines (314 loc) · 14.6 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
"""
Experience Replay System with Q-Learning
Implements continuous learning from past solving attempts
"""
import json
import numpy as np
from collections import deque
from typing import List, Dict, Any, Tuple, Optional
from datetime import datetime
import pickle
import hashlib
class Experience:
"""Represents a single solving experience"""
def __init__(self, puzzle_id: str, state: np.ndarray, action: str,
reward: float, next_state: np.ndarray, done: bool):
self.puzzle_id = puzzle_id
self.state = state
self.action = action
self.reward = reward
self.next_state = next_state
self.done = done
self.timestamp = datetime.now()
def to_dict(self) -> Dict:
"""Convert to dictionary for storage"""
return {
'puzzle_id': self.puzzle_id,
'state': self.state.tolist() if isinstance(self.state, np.ndarray) else self.state,
'action': self.action,
'reward': self.reward,
'next_state': self.next_state.tolist() if isinstance(self.next_state, np.ndarray) else self.next_state,
'done': self.done,
'timestamp': self.timestamp.isoformat()
}
class ExperienceReplayBuffer:
"""Circular buffer for storing experiences"""
def __init__(self, capacity: int = 10000):
self.buffer = deque(maxlen=capacity)
self.priorities = deque(maxlen=capacity)
self.total_experiences = 0
def add(self, experience: Experience, priority: float = 1.0):
"""Add experience to buffer"""
self.buffer.append(experience)
self.priorities.append(priority)
self.total_experiences += 1
def sample(self, batch_size: int, prioritized: bool = True) -> List[Experience]:
"""Sample batch of experiences"""
if len(self.buffer) < batch_size:
batch_size = len(self.buffer)
if prioritized and len(self.priorities) > 0:
# Prioritized sampling
priorities = np.array(self.priorities)
probabilities = priorities / priorities.sum()
indices = np.random.choice(len(self.buffer), batch_size, p=probabilities)
else:
# Uniform sampling
indices = np.random.choice(len(self.buffer), batch_size)
return [self.buffer[i] for i in indices]
def update_priorities(self, indices: List[int], priorities: List[float]):
"""Update priorities for experiences"""
for idx, priority in zip(indices, priorities):
if 0 <= idx < len(self.priorities):
self.priorities[idx] = priority
def save(self, filepath: str):
"""Save buffer to disk"""
with open(filepath, 'wb') as f:
pickle.dump({
'buffer': list(self.buffer),
'priorities': list(self.priorities),
'total_experiences': self.total_experiences
}, f)
def load(self, filepath: str):
"""Load buffer from disk"""
with open(filepath, 'rb') as f:
data = pickle.load(f)
self.buffer = deque(data['buffer'], maxlen=self.buffer.maxlen)
self.priorities = deque(data['priorities'], maxlen=self.priorities.maxlen)
self.total_experiences = data['total_experiences']
class QLearningAgent:
"""Q-Learning agent for puzzle solving"""
def __init__(self, state_size: int, action_size: int, learning_rate: float = 0.01,
discount_factor: float = 0.95, epsilon: float = 0.1):
self.state_size = state_size
self.action_size = action_size
self.learning_rate = learning_rate
self.discount_factor = discount_factor
self.epsilon = epsilon
# Initialize Q-table
self.q_table = {}
# Action mapping
self.actions = [
'rotate_90', 'rotate_180', 'rotate_270',
'flip_horizontal', 'flip_vertical',
'color_swap', 'extract_objects', 'fill_pattern',
'apply_symmetry', 'crop', 'expand', 'tile'
]
def get_state_hash(self, state: np.ndarray) -> str:
"""Get hash of state for Q-table lookup"""
if isinstance(state, np.ndarray):
state_bytes = state.tobytes()
else:
state_bytes = str(state).encode()
return hashlib.md5(state_bytes).hexdigest()
def get_q_value(self, state: np.ndarray, action: str) -> float:
"""Get Q-value for state-action pair"""
state_hash = self.get_state_hash(state)
if state_hash not in self.q_table:
self.q_table[state_hash] = {a: 0.0 for a in self.actions}
return self.q_table[state_hash].get(action, 0.0)
def set_q_value(self, state: np.ndarray, action: str, value: float):
"""Set Q-value for state-action pair"""
state_hash = self.get_state_hash(state)
if state_hash not in self.q_table:
self.q_table[state_hash] = {a: 0.0 for a in self.actions}
self.q_table[state_hash][action] = value
def choose_action(self, state: np.ndarray, explore: bool = True) -> str:
"""Choose action using epsilon-greedy policy"""
if explore and np.random.random() < self.epsilon:
# Explore: random action
return np.random.choice(self.actions)
else:
# Exploit: best action
state_hash = self.get_state_hash(state)
if state_hash in self.q_table:
q_values = self.q_table[state_hash]
max_q = max(q_values.values())
best_actions = [a for a, q in q_values.items() if q == max_q]
return np.random.choice(best_actions)
else:
return np.random.choice(self.actions)
def update(self, state: np.ndarray, action: str, reward: float,
next_state: np.ndarray, done: bool):
"""Update Q-value using Q-learning update rule"""
current_q = self.get_q_value(state, action)
if done:
target_q = reward
else:
# Get max Q-value for next state
next_state_hash = self.get_state_hash(next_state)
if next_state_hash in self.q_table:
max_next_q = max(self.q_table[next_state_hash].values())
else:
max_next_q = 0.0
target_q = reward + self.discount_factor * max_next_q
# Q-learning update
new_q = current_q + self.learning_rate * (target_q - current_q)
self.set_q_value(state, action, new_q)
def decay_epsilon(self, decay_rate: float = 0.995):
"""Decay exploration rate"""
self.epsilon = max(0.01, self.epsilon * decay_rate)
class ReinforcementLearningSolver:
"""Solver using reinforcement learning with experience replay"""
def __init__(self, buffer_capacity: int = 10000):
self.replay_buffer = ExperienceReplayBuffer(buffer_capacity)
self.q_agent = QLearningAgent(state_size=100, action_size=12)
self.episode_rewards = []
self.success_count = 0
self.total_episodes = 0
def state_from_puzzle(self, puzzle: Dict) -> np.ndarray:
"""Extract state representation from puzzle"""
if 'train' in puzzle and len(puzzle['train']) > 0:
# Use first training example as state
input_grid = np.array(puzzle['train'][0]['input'])
# Create fixed-size state representation
features = []
# Grid statistics
features.append(input_grid.shape[0])
features.append(input_grid.shape[1])
features.append(len(np.unique(input_grid)))
features.append(np.mean(input_grid))
features.append(np.std(input_grid))
# Color histogram (up to 10 colors)
hist = np.bincount(input_grid.flatten(), minlength=10)[:10]
features.extend(hist.tolist())
# Spatial features
features.append(np.sum(input_grid[0, :])) # Top row sum
features.append(np.sum(input_grid[-1, :])) # Bottom row sum
features.append(np.sum(input_grid[:, 0])) # Left column sum
features.append(np.sum(input_grid[:, -1])) # Right column sum
# Pad to fixed size
while len(features) < 100:
features.append(0)
return np.array(features[:100])
else:
return np.zeros(100)
def calculate_reward(self, puzzle: Dict, solution: np.ndarray,
solved: bool) -> float:
"""Calculate reward for a solution attempt"""
if solved:
return 10.0
# Partial rewards based on similarity
if 'train' in puzzle and len(puzzle['train']) > 0:
if 'output' in puzzle['train'][0]:
expected = np.array(puzzle['train'][0]['output'])
# Shape similarity
shape_reward = 0
if solution.shape == expected.shape:
shape_reward = 2.0
# Color similarity
color_reward = 0
expected_colors = set(expected.flatten())
solution_colors = set(solution.flatten())
color_overlap = len(expected_colors & solution_colors) / max(len(expected_colors), 1)
color_reward = color_overlap * 2.0
# Pixel similarity (if same shape)
pixel_reward = 0
if solution.shape == expected.shape:
pixel_accuracy = np.mean(solution == expected)
pixel_reward = pixel_accuracy * 5.0
return shape_reward + color_reward + pixel_reward - 1.0
return -1.0 # Penalty for failure
async def train_on_puzzle(self, puzzle: Dict) -> Dict:
"""Train the agent on a single puzzle"""
state = self.state_from_puzzle(puzzle)
total_reward = 0
steps = 0
max_steps = 20
while steps < max_steps:
# Choose action
action = self.q_agent.choose_action(state)
# Apply action (simulated)
solution = self._apply_action(puzzle, action)
# Check if solved
solved = self._check_solution(puzzle, solution)
# Calculate reward
reward = self.calculate_reward(puzzle, solution, solved)
total_reward += reward
# Get next state
next_state = self._get_next_state(state, action)
# Store experience
experience = Experience(
puzzle.get('id', 'unknown'),
state, action, reward, next_state, solved
)
self.replay_buffer.add(experience, abs(reward))
# Update Q-values
self.q_agent.update(state, action, reward, next_state, solved)
# Experience replay
if len(self.replay_buffer.buffer) >= 32:
batch = self.replay_buffer.sample(32)
for exp in batch:
self.q_agent.update(
exp.state, exp.action, exp.reward,
exp.next_state, exp.done
)
if solved:
self.success_count += 1
break
state = next_state
steps += 1
# Decay exploration
self.q_agent.decay_epsilon()
# Track episode
self.episode_rewards.append(total_reward)
self.total_episodes += 1
return {
'solved': solved,
'steps': steps,
'total_reward': total_reward,
'epsilon': self.q_agent.epsilon,
'q_table_size': len(self.q_agent.q_table)
}
def _apply_action(self, puzzle: Dict, action: str) -> np.ndarray:
"""Apply action to puzzle (simulated)"""
if 'train' in puzzle and len(puzzle['train']) > 0:
grid = np.array(puzzle['train'][0]['input'])
if action == 'rotate_90':
return np.rot90(grid, -1)
elif action == 'rotate_180':
return np.rot90(grid, 2)
elif action == 'rotate_270':
return np.rot90(grid, 1)
elif action == 'flip_horizontal':
return np.fliplr(grid)
elif action == 'flip_vertical':
return np.flipud(grid)
elif action == 'color_swap':
result = grid.copy()
if len(np.unique(grid)) >= 2:
colors = np.unique(grid)[:2]
result[grid == colors[0]] = colors[1]
result[grid == colors[1]] = colors[0]
return result
else:
# Default: return input
return grid
return np.array([[0]])
def _check_solution(self, puzzle: Dict, solution: np.ndarray) -> bool:
"""Check if solution is correct (simulated)"""
# For real puzzles with known output
if 'train' in puzzle and len(puzzle['train']) > 0:
if 'output' in puzzle['train'][0]:
expected = np.array(puzzle['train'][0]['output'])
return np.array_equal(solution, expected)
# Probabilistic check for generated puzzles
return np.random.random() < 0.3
def _get_next_state(self, state: np.ndarray, action: str) -> np.ndarray:
"""Get next state after action (simulated)"""
# Simple state transition
next_state = state.copy()
# Modify state based on action
action_idx = self.q_agent.actions.index(action) if action in self.q_agent.actions else 0
next_state[action_idx] += 1 # Increment action counter
return next_state
def get_statistics(self) -> Dict:
"""Get training statistics"""
return {
'total_episodes': self.total_episodes,
'success_count': self.success_count,
'success_rate': self.success_count / max(self.total_episodes, 1),
'average_reward': np.mean(self.episode_rewards) if self.episode_rewards else 0,
'q_table_size': len(self.q_agent.q_table),
'epsilon': self.q_agent.epsilon,
'buffer_size': len(self.replay_buffer.buffer)
}