Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import hashlib
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import os
|
| 5 |
+
from copy import deepcopy
|
| 6 |
+
|
| 7 |
+
class SymbolicMemory:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.memory = {}
|
| 10 |
+
self.history = []
|
| 11 |
+
|
| 12 |
+
def encode(self, grid):
|
| 13 |
+
flat = grid.flatten()
|
| 14 |
+
key = hashlib.sha256(str((grid.shape, tuple(np.bincount(flat, minlength=10)))).encode()).hexdigest()
|
| 15 |
+
return key
|
| 16 |
+
|
| 17 |
+
def store(self, key, value):
|
| 18 |
+
self.memory[key] = value
|
| 19 |
+
|
| 20 |
+
def entropy(self, grid):
|
| 21 |
+
flat = grid.flatten()
|
| 22 |
+
_, counts = np.unique(flat, return_counts=True)
|
| 23 |
+
probs = counts / counts.sum()
|
| 24 |
+
entropy = -np.sum(probs * np.log2(probs))
|
| 25 |
+
self.history.append(entropy)
|
| 26 |
+
return entropy
|
| 27 |
+
|
| 28 |
+
def plot(self, task_id):
|
| 29 |
+
os.makedirs("entropy_graphs", exist_ok=True)
|
| 30 |
+
plt.plot(self.history)
|
| 31 |
+
plt.title(f"ZEVE Entropy: {task_id}")
|
| 32 |
+
plt.xlabel("Step")
|
| 33 |
+
plt.ylabel("Entropy")
|
| 34 |
+
plt.savefig(f"entropy_graphs/entropy_{task_id}.png")
|
| 35 |
+
plt.clf()
|
| 36 |
+
|
| 37 |
+
class IRACOETSolver:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self.memory = SymbolicMemory()
|
| 40 |
+
|
| 41 |
+
def solve_task(self, task):
|
| 42 |
+
for pair in task['train']:
|
| 43 |
+
key = self.memory.encode(np.array(pair['input']))
|
| 44 |
+
self.memory.store(key, pair['output'])
|
| 45 |
+
|
| 46 |
+
results = []
|
| 47 |
+
for pair in task['test']:
|
| 48 |
+
result = self.solve(np.array(pair['input']), task.get('id', 'unknown'))
|
| 49 |
+
results.append(result)
|
| 50 |
+
return results
|
| 51 |
+
|
| 52 |
+
def solve(self, grid, task_id):
|
| 53 |
+
current = deepcopy(grid)
|
| 54 |
+
best = self.memory.entropy(current)
|
| 55 |
+
for _ in range(9):
|
| 56 |
+
options = [
|
| 57 |
+
np.fliplr(current), np.flipud(current),
|
| 58 |
+
np.rot90(current), np.roll(current, 1, 0), np.roll(current, 1, 1)
|
| 59 |
+
]
|
| 60 |
+
scored = [(g, self.memory.entropy(g)) for g in options]
|
| 61 |
+
candidate, score = min(scored, key=lambda x: x[1])
|
| 62 |
+
if score < best:
|
| 63 |
+
current, best = candidate, score
|
| 64 |
+
else:
|
| 65 |
+
break
|
| 66 |
+
self.memory.plot(task_id)
|
| 67 |
+
return current.tolist()
|