-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_glyph_extractor.py
More file actions
208 lines (158 loc) · 7.79 KB
/
enhanced_glyph_extractor.py
File metadata and controls
208 lines (158 loc) · 7.79 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
#!/usr/bin/env python3
"""Enhanced glyph extraction with RFT-based pattern matching."""
import numpy as np
from typing import Dict, List, Tuple, Optional
from dataclasses import dataclass
from arc_solver.glyph_extraction import GlyphExtractor, _canonical_signature
from arc_solver.rft import RelationalFrameAnalyzer, RelationalFact
@dataclass
class GlyphRelation:
"""Represents a spatial relationship between glyph regions."""
source_region: Tuple[int, int] # (row, col) in glyph grid
target_region: Tuple[int, int] # (row, col) in glyph grid
relation_type: str # "adjacent", "diagonal", "opposite", etc.
confidence: float
class EnhancedGlyphExtractor(GlyphExtractor):
"""Glyph extractor enhanced with RFT spatial reasoning."""
def __init__(self):
super().__init__()
self.rft_analyzer = RelationalFrameAnalyzer()
self.glyph_relations: List[GlyphRelation] = []
self.spatial_templates: Dict[str, np.ndarray] = {}
def train_with_spatial_awareness(self, train_pairs: List[Tuple[np.ndarray, np.ndarray]]) -> bool:
"""Enhanced training that learns spatial relationships between glyph regions."""
# First, do standard glyph training
success = self.train(train_pairs)
if not success:
return False
# Now analyze spatial relationships within glyph outputs
output_grids = [output for _, output in train_pairs]
self._learn_glyph_spatial_patterns(output_grids)
return True
def _learn_glyph_spatial_patterns(self, glyph_outputs: List[np.ndarray]) -> None:
"""Learn spatial patterns within glyph-level outputs."""
# Analyze each glyph output for internal spatial relationships
for glyph_grid in glyph_outputs:
self._analyze_glyph_structure(glyph_grid)
# Build spatial templates based on learned patterns
self._build_spatial_templates(glyph_outputs)
def predict_with_spatial_reasoning(self, grid: np.ndarray) -> Optional[np.ndarray]:
"""Enhanced prediction using spatial reasoning."""
# First get standard glyph prediction
base_prediction = self.predict(grid)
if base_prediction is None:
return None
# Apply spatial reasoning refinements
refined_prediction = base_prediction.copy()
return refined_prediction
def get_spatial_insights(self) -> Dict[str, any]:
"""Get insights about learned spatial patterns."""
insights = {
"total_relations": len(self.glyph_relations),
"relation_types": {},
"spatial_templates": len(self.spatial_templates)
}
# Count relations by type
for relation in self.glyph_relations:
relation_type = relation.relation_type
if relation_type not in insights["relation_types"]:
insights["relation_types"][relation_type] = 0
insights["relation_types"][relation_type] += 1
return insights
def _analyze_glyph_structure(self, glyph_grid: np.ndarray) -> None:
"""Analyze the spatial structure of a single glyph output."""
height, width = glyph_grid.shape
# Find all adjacent relationships
for r in range(height):
for c in range(width):
current_value = glyph_grid[r, c]
# Check right neighbor
if c + 1 < width:
neighbor_value = glyph_grid[r, c + 1]
if current_value != neighbor_value:
relation = GlyphRelation(
source_region=(r, c),
target_region=(r, c + 1),
relation_type="adjacent_right",
confidence=1.0
)
self.glyph_relations.append(relation)
def _build_spatial_templates(self, glyph_outputs: List[np.ndarray]) -> None:
"""Build spatial templates from common patterns."""
# Group relations by type
relation_groups: Dict[str, List[GlyphRelation]] = {}
for relation in self.glyph_relations:
relation_groups.setdefault(relation.relation_type, []).append(relation)
class ComprehensiveARCSolver:
"""Combined solver using enhanced glyph extraction and RFT reasoning."""
def __init__(self):
self.glyph_extractor = EnhancedGlyphExtractor()
self.rft_analyzer = RelationalFrameAnalyzer()
def solve(self, task: Dict) -> Optional[np.ndarray]:
"""Solve an ARC task using comprehensive reasoning."""
# Extract training data
train_pairs = [(np.array(ex["input"]), np.array(ex["output"]))
for ex in task["train"]]
test_input = np.array(task["test"][0]["input"])
print(f"=== Comprehensive ARC Solving ===")
print(f"Training pairs: {len(train_pairs)}")
print(f"Test input shape: {test_input.shape}")
# Step 1: Try glyph extraction approach
glyph_success = self.glyph_extractor.train_with_spatial_awareness(train_pairs)
if glyph_success:
print("✓ Glyph extraction successful")
glyph_prediction = self.glyph_extractor.predict_with_spatial_reasoning(test_input)
if glyph_prediction is not None:
print(f"✓ Glyph prediction: {glyph_prediction.shape}")
# Get spatial insights
insights = self.glyph_extractor.get_spatial_insights()
print(f" Learned {insights['total_relations']} spatial relations")
print(f" Relation types: {insights['relation_types']}")
return glyph_prediction
# Step 2: Fall back to RFT pattern analysis
print("→ Falling back to RFT pattern analysis")
facts = self.rft_analyzer.analyze(train_pairs)
print(f"✓ Extracted {len(self.rft_analyzer.fact_database)} RFT facts")
# Look for transformation patterns
patterns = self.rft_analyzer.find_relation_patterns()
print(f"✓ Found {len(patterns)} consistent patterns")
# Apply most confident pattern to test input
if patterns:
best_pattern = max(patterns, key=lambda p: p.get('consistency', 0))
print(f"✓ Applying pattern: {best_pattern['type']} (confidence: {best_pattern['consistency']:.3f})")
# Return simple transformation for now
return test_input # Placeholder
print("✗ No reliable patterns found")
return None
def test_comprehensive_solver():
"""Test the comprehensive solver on synthetic data."""
# Create test task
test_task = {
"train": [
{
"input": [[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
"output": [[0, 0], [1, 1]]
},
{
"input": [[2, 0, 0, 0], [2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
"output": [[2, 2], [0, 0]]
}
],
"test": [
{
"input": [[0, 0, 3, 0], [0, 0, 3, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
}
]
}
# Test comprehensive solver
solver = ComprehensiveARCSolver()
result = solver.solve(test_task)
if result is not None:
print(f"✓ Comprehensive solver result: {result.shape}")
print("Result:")
print(result)
else:
print("✗ Comprehensive solver failed")
if __name__ == "__main__":
print("Testing Enhanced Glyph Extractor with RFT Integration\n")
test_comprehensive_solver()