Fix fill_color=0 bug in ITT rule learning + add mixed→recolor fallback
Browse files- itt_solver/itt_engine.py +0 -498
itt_solver/itt_engine.py
CHANGED
|
@@ -1,498 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
ITT Physics Engine for ARC-AGI
|
| 3 |
-
==============================
|
| 4 |
-
|
| 5 |
-
Pure implementation of the Intent Tensor Theory solver, ported from
|
| 6 |
-
Sensei-Intent-Tensor/0.0_ARC_AGI (ITT_PURE_SOLVER.py v4).
|
| 7 |
-
|
| 8 |
-
Phases 1-7 of the ITT integration:
|
| 9 |
-
1. PhiField dual-field (Phi_q + Phi_tilde)
|
| 10 |
-
2. rho_q boundary charge with physics-derived threshold
|
| 11 |
-
3. SigmaResidue change typing
|
| 12 |
-
4. Fan Signature 6-bit classifier
|
| 13 |
-
5. TransformationRule.learn()
|
| 14 |
-
6. FieldInvariants (spectral, harmonic, eigenspectrum, Fourier, frames)
|
| 15 |
-
7. Rule apply methods (tile, self_tile, fill, multi_fill, period, shape, recolor)
|
| 16 |
-
|
| 17 |
-
References:
|
| 18 |
-
- https://github.com/Sensei-Intent-Tensor/0.0_ARC_AGI
|
| 19 |
-
- https://zenodo.org/records/18077258
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
import numpy as np
|
| 23 |
-
from typing import Dict, List, Tuple, Optional, Set, Any
|
| 24 |
-
from dataclasses import dataclass, field
|
| 25 |
-
from collections import deque, Counter
|
| 26 |
-
from math import gcd
|
| 27 |
-
from functools import reduce
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
class PhiField:
|
| 31 |
-
"""Dual-Field: Phi_q (int semantics) + Phi_tilde (smooth float operators)."""
|
| 32 |
-
|
| 33 |
-
def __init__(self, data):
|
| 34 |
-
arr = np.array(data, dtype=np.float64)
|
| 35 |
-
self._q = np.rint(arr).astype(np.int32)
|
| 36 |
-
self._tilde = self._compute_smooth(self._q)
|
| 37 |
-
|
| 38 |
-
@staticmethod
|
| 39 |
-
def _compute_smooth(q, iters=2):
|
| 40 |
-
x = q.astype(np.float64)
|
| 41 |
-
h, w = x.shape
|
| 42 |
-
for _ in range(iters):
|
| 43 |
-
new_x = x.copy()
|
| 44 |
-
for i in range(h):
|
| 45 |
-
for j in range(w):
|
| 46 |
-
total = x[i, j]; count = 1
|
| 47 |
-
if i > 0: total += x[i-1, j]; count += 1
|
| 48 |
-
if i < h-1: total += x[i+1, j]; count += 1
|
| 49 |
-
if j > 0: total += x[i, j-1]; count += 1
|
| 50 |
-
if j < w-1: total += x[i, j+1]; count += 1
|
| 51 |
-
new_x[i, j] = total / count
|
| 52 |
-
x = new_x
|
| 53 |
-
return x
|
| 54 |
-
|
| 55 |
-
@property
|
| 56 |
-
def q(self): return self._q
|
| 57 |
-
@property
|
| 58 |
-
def tilde(self): return self._tilde
|
| 59 |
-
@property
|
| 60 |
-
def shape(self): return self._q.shape
|
| 61 |
-
@property
|
| 62 |
-
def h(self): return self._q.shape[0]
|
| 63 |
-
@property
|
| 64 |
-
def w(self): return self._q.shape[1]
|
| 65 |
-
@property
|
| 66 |
-
def colors(self): return set(int(x) for x in self._q.flatten() if x != 0)
|
| 67 |
-
|
| 68 |
-
def gradient(self):
|
| 69 |
-
gx = np.zeros_like(self._tilde); gy = np.zeros_like(self._tilde)
|
| 70 |
-
gy[:-1, :] = self._tilde[1:, :] - self._tilde[:-1, :]
|
| 71 |
-
gx[:, :-1] = self._tilde[:, 1:] - self._tilde[:, :-1]
|
| 72 |
-
return gx, gy
|
| 73 |
-
|
| 74 |
-
def gradient_magnitude(self):
|
| 75 |
-
gx, gy = self.gradient()
|
| 76 |
-
return np.sqrt(gx**2 + gy**2)
|
| 77 |
-
|
| 78 |
-
def laplacian(self):
|
| 79 |
-
x = self._tilde; lap = np.zeros_like(x); h, w = self.shape
|
| 80 |
-
for i in range(h):
|
| 81 |
-
for j in range(w):
|
| 82 |
-
total = 0.0; count = 0
|
| 83 |
-
if i > 0: total += x[i-1, j]; count += 1
|
| 84 |
-
if i < h-1: total += x[i+1, j]; count += 1
|
| 85 |
-
if j > 0: total += x[i, j-1]; count += 1
|
| 86 |
-
if j < w-1: total += x[i, j+1]; count += 1
|
| 87 |
-
lap[i, j] = total - count * x[i, j]
|
| 88 |
-
return lap
|
| 89 |
-
|
| 90 |
-
def boundary_charge(self):
|
| 91 |
-
"""rho_q := |grad(laplacian(Phi_tilde))|"""
|
| 92 |
-
lap = self.laplacian()
|
| 93 |
-
gx = np.zeros_like(lap); gy = np.zeros_like(lap)
|
| 94 |
-
gy[:-1, :] = lap[1:, :] - lap[:-1, :]
|
| 95 |
-
gx[:, :-1] = lap[:, 1:] - lap[:, :-1]
|
| 96 |
-
return np.sqrt(gx**2 + gy**2)
|
| 97 |
-
|
| 98 |
-
def boundary_mask(self):
|
| 99 |
-
"""Boolean boundary mask, threshold = mu + 1.5*sigma."""
|
| 100 |
-
rho = self.boundary_charge()
|
| 101 |
-
nonzero = rho[rho > 0]
|
| 102 |
-
if len(nonzero) == 0: return np.zeros_like(rho, dtype=bool)
|
| 103 |
-
mu = np.mean(nonzero); sigma = np.std(nonzero)
|
| 104 |
-
return rho > (mu + 1.5 * sigma)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
class FieldInvariants:
|
| 108 |
-
|
| 109 |
-
@staticmethod
|
| 110 |
-
def enclosed_mask(phi):
|
| 111 |
-
h, w = phi.shape; boundary = phi.boundary_mask()
|
| 112 |
-
if not np.any(boundary): boundary = (phi.q != 0)
|
| 113 |
-
exterior = np.zeros((h, w), dtype=bool); queue = deque()
|
| 114 |
-
for i in range(h):
|
| 115 |
-
for j in range(w):
|
| 116 |
-
if (i == 0 or i == h-1 or j == 0 or j == w-1):
|
| 117 |
-
if not boundary[i, j] and phi.q[i, j] == 0:
|
| 118 |
-
exterior[i, j] = True; queue.append((i, j))
|
| 119 |
-
while queue:
|
| 120 |
-
r, c = queue.popleft()
|
| 121 |
-
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
|
| 122 |
-
nr, nc = r + dr, c + dc
|
| 123 |
-
if 0 <= nr < h and 0 <= nc < w and not exterior[nr, nc] and not boundary[nr, nc]:
|
| 124 |
-
if phi.q[nr, nc] == 0: exterior[nr, nc] = True; queue.append((nr, nc))
|
| 125 |
-
return (phi.q == 0) & ~exterior & ~boundary
|
| 126 |
-
|
| 127 |
-
@staticmethod
|
| 128 |
-
def get_enclosed_regions(phi):
|
| 129 |
-
mask = FieldInvariants.enclosed_mask(phi)
|
| 130 |
-
if not np.any(mask): return []
|
| 131 |
-
h, w = phi.shape; visited = np.zeros((h, w), dtype=bool); regions = []
|
| 132 |
-
for r in range(h):
|
| 133 |
-
for c in range(w):
|
| 134 |
-
if mask[r, c] and not visited[r, c]:
|
| 135 |
-
cells = set(); queue = deque([(r, c)]); visited[r, c] = True
|
| 136 |
-
while queue:
|
| 137 |
-
cr, cc = queue.popleft(); cells.add((cr, cc))
|
| 138 |
-
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
|
| 139 |
-
nr, nc = cr+dr, cc+dc
|
| 140 |
-
if 0 <= nr < h and 0 <= nc < w and mask[nr, nc] and not visited[nr, nc]:
|
| 141 |
-
visited[nr, nc] = True; queue.append((nr, nc))
|
| 142 |
-
rm = np.zeros((h, w), dtype=bool)
|
| 143 |
-
for rr, rc in cells: rm[rr, rc] = True
|
| 144 |
-
regions.append({'mask': rm, 'cells': cells, 'size': len(cells)})
|
| 145 |
-
return regions
|
| 146 |
-
|
| 147 |
-
@staticmethod
|
| 148 |
-
def frame_size(phi, interior_mask):
|
| 149 |
-
rows = np.any(interior_mask, axis=1); cols = np.any(interior_mask, axis=0)
|
| 150 |
-
if not rows.any() or not cols.any(): return (0, 0)
|
| 151 |
-
rmin, rmax = np.where(rows)[0][[0, -1]]; cmin, cmax = np.where(cols)[0][[0, -1]]
|
| 152 |
-
return (rmax - rmin + 1, cmax - cmin + 1)
|
| 153 |
-
|
| 154 |
-
@staticmethod
|
| 155 |
-
def get_frame_components(phi):
|
| 156 |
-
h, w = phi.shape; bg = _most_common(phi.q); frames = []
|
| 157 |
-
for color in sorted(phi.colors):
|
| 158 |
-
color_mask = (phi.q == color); visited = np.zeros((h, w), dtype=bool)
|
| 159 |
-
for r in range(h):
|
| 160 |
-
for c in range(w):
|
| 161 |
-
if color_mask[r, c] and not visited[r, c]:
|
| 162 |
-
comp = set(); queue = deque([(r, c)]); visited[r, c] = True
|
| 163 |
-
while queue:
|
| 164 |
-
cr, cc = queue.popleft(); comp.add((cr, cc))
|
| 165 |
-
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
|
| 166 |
-
nr, nc = cr+dr, cc+dc
|
| 167 |
-
if 0 <= nr < h and 0 <= nc < w and color_mask[nr, nc] and not visited[nr, nc]:
|
| 168 |
-
visited[nr, nc] = True; queue.append((nr, nc))
|
| 169 |
-
if len(comp) < 4: continue
|
| 170 |
-
rows_c = [rr for rr, _ in comp]; cols_c = [cc for _, cc in comp]
|
| 171 |
-
rmin, rmax = min(rows_c), max(rows_c); cmin, cmax = min(cols_c), max(cols_c)
|
| 172 |
-
bbox_area = (rmax - rmin + 1) * (cmax - cmin + 1)
|
| 173 |
-
if bbox_area > len(comp) and len(comp) >= 4:
|
| 174 |
-
interior_mask = np.zeros((h, w), dtype=bool)
|
| 175 |
-
for ir in range(rmin + 1, rmax):
|
| 176 |
-
for ic in range(cmin + 1, cmax):
|
| 177 |
-
if (ir, ic) not in comp: interior_mask[ir, ic] = True
|
| 178 |
-
if np.any(interior_mask):
|
| 179 |
-
frames.append({'frame_color': color, 'interior_mask': interior_mask,
|
| 180 |
-
'frame_size': (rmax-rmin+1, cmax-cmin+1), 'bbox': (rmin,cmin,rmax,cmax)})
|
| 181 |
-
return frames
|
| 182 |
-
|
| 183 |
-
@staticmethod
|
| 184 |
-
def shape_eigenspectrum(phi, positions, k=4):
|
| 185 |
-
n = len(positions)
|
| 186 |
-
if n < 2: return None
|
| 187 |
-
pos_to_idx = {p: i for i, p in enumerate(positions)}
|
| 188 |
-
L = np.zeros((n, n), dtype=np.float64)
|
| 189 |
-
for i, (r, c) in enumerate(positions):
|
| 190 |
-
degree = 0
|
| 191 |
-
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
|
| 192 |
-
nb = (r+dr, c+dc)
|
| 193 |
-
if nb in pos_to_idx: j = pos_to_idx[nb]; L[i, j] = -1; degree += 1
|
| 194 |
-
L[i, i] = degree
|
| 195 |
-
try:
|
| 196 |
-
eigs = np.linalg.eigvalsh(L)
|
| 197 |
-
nonzero = eigs[eigs > 1e-8]
|
| 198 |
-
if len(nonzero) == 0: return (0.0,)
|
| 199 |
-
return tuple(round(float(e), 4) for e in sorted(nonzero)[:k])
|
| 200 |
-
except Exception: return None
|
| 201 |
-
|
| 202 |
-
@staticmethod
|
| 203 |
-
def detect_period_fourier(phi, axis=0):
|
| 204 |
-
data = phi.q.astype(np.float64)
|
| 205 |
-
signal = data.mean(axis=1) if axis == 0 else data.mean(axis=0)
|
| 206 |
-
n = len(signal)
|
| 207 |
-
if n < 2: return 0
|
| 208 |
-
fft = np.fft.rfft(signal); mags = np.abs(fft)
|
| 209 |
-
if len(mags) < 2: return 0
|
| 210 |
-
m = mags[1:]
|
| 211 |
-
if len(m) == 0 or np.max(m) < 1e-10: return 0
|
| 212 |
-
threshold = np.max(m) * 0.3
|
| 213 |
-
sig_freqs = np.where(m > threshold)[0] + 1
|
| 214 |
-
if len(sig_freqs) == 0: return 0
|
| 215 |
-
periods = [n // f for f in sig_freqs if 0 < n // f < n]
|
| 216 |
-
for p in sorted(set(periods)):
|
| 217 |
-
if p > 0 and p < n:
|
| 218 |
-
base = signal[:p]; ok = True
|
| 219 |
-
for start in range(p, n - p + 1, p):
|
| 220 |
-
chunk = signal[start:start+p]
|
| 221 |
-
if len(chunk) == p and not np.allclose(chunk, base, atol=0.5): ok = False; break
|
| 222 |
-
if ok: return p
|
| 223 |
-
return 0
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
@dataclass
|
| 227 |
-
class SigmaResidue:
|
| 228 |
-
residue: float; total_cells: int; change_type: str; structural_condition: str
|
| 229 |
-
|
| 230 |
-
@classmethod
|
| 231 |
-
def from_transformation(cls, phi_in, phi_out):
|
| 232 |
-
h_in, w_in = phi_in.shape; h_out, w_out = phi_out.shape; total = h_out * w_out
|
| 233 |
-
if h_out > h_in or w_out > w_in:
|
| 234 |
-
return cls(float(np.sum(np.abs(phi_out.q))), total, "expansion", "size_increase")
|
| 235 |
-
if h_out < h_in or w_out < w_in:
|
| 236 |
-
return cls(float(np.sum(np.abs(phi_in.q))), total, "compression", "size_decrease")
|
| 237 |
-
diff = (phi_in.q != phi_out.q)
|
| 238 |
-
residue = float(np.sum(np.abs(phi_out.q.astype(float) - phi_in.q.astype(float))))
|
| 239 |
-
if not np.any(diff): return cls(0.0, total, "identity", "none")
|
| 240 |
-
in_v = phi_in.q[diff]; out_v = phi_out.q[diff]
|
| 241 |
-
z2n = np.sum((in_v == 0) & (out_v != 0)); n2z = np.sum((in_v != 0) & (out_v == 0))
|
| 242 |
-
cc = np.sum((in_v != 0) & (out_v != 0) & (in_v != out_v))
|
| 243 |
-
if z2n > 0 and n2z == 0 and cc == 0: return cls(residue, total, "fill", "enclosed")
|
| 244 |
-
if n2z > 0 and z2n == 0: return cls(residue, total, "erase", "removal")
|
| 245 |
-
if cc > 0 and z2n == 0 and n2z == 0: return cls(residue, total, "recolor", "substitution")
|
| 246 |
-
return cls(residue, total, "mixed", "complex")
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
@dataclass
|
| 250 |
-
class FanSignature:
|
| 251 |
-
delta_1: bool; delta_2: bool; delta_3: bool; delta_4: bool; delta_5: bool; delta_6: bool
|
| 252 |
-
def to_tuple(self): return tuple(int(x) for x in [self.delta_1,self.delta_2,self.delta_3,self.delta_4,self.delta_5,self.delta_6])
|
| 253 |
-
def __repr__(self):
|
| 254 |
-
fans = []
|
| 255 |
-
if self.delta_1: fans.append("D1")
|
| 256 |
-
if self.delta_2: fans.append("D2")
|
| 257 |
-
if self.delta_3: fans.append("D3")
|
| 258 |
-
if self.delta_4: fans.append("D4")
|
| 259 |
-
if self.delta_5: fans.append("D5")
|
| 260 |
-
if self.delta_6: fans.append("D6")
|
| 261 |
-
return f"FanSig[{','.join(fans) or 'none'}]"
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
def compute_fan_signature(train_pairs):
|
| 265 |
-
inputs = [np.array(p['input']) for p in train_pairs]
|
| 266 |
-
outputs = [np.array(p['output']) for p in train_pairs]
|
| 267 |
-
same_shape = all(i.shape == o.shape for i, o in zip(inputs, outputs))
|
| 268 |
-
is_expansion = all(o.shape[0] >= i.shape[0] and o.shape[1] >= i.shape[1] and o.shape != i.shape for i, o in zip(inputs, outputs))
|
| 269 |
-
has_sym = False
|
| 270 |
-
for inp in inputs:
|
| 271 |
-
if np.array_equal(inp, np.fliplr(inp)) or np.array_equal(inp, np.flipud(inp)): has_sym = True
|
| 272 |
-
if inp.shape[0] == inp.shape[1] and np.array_equal(inp, np.rot90(inp)): has_sym = True
|
| 273 |
-
for inp, out in zip(inputs, outputs):
|
| 274 |
-
if inp.shape == out.shape:
|
| 275 |
-
if np.array_equal(out, np.fliplr(inp)) or np.array_equal(out, np.flipud(inp)) or np.array_equal(out, np.rot90(inp, 2)): has_sym = True
|
| 276 |
-
has_enc = False
|
| 277 |
-
for inp in inputs:
|
| 278 |
-
if np.any(FieldInvariants.enclosed_mask(PhiField(inp))): has_enc = True; break
|
| 279 |
-
has_per = False
|
| 280 |
-
for inp in inputs:
|
| 281 |
-
phi = PhiField(inp)
|
| 282 |
-
if FieldInvariants.detect_period_fourier(phi, 0) > 0 or FieldInvariants.detect_period_fourier(phi, 1) > 0: has_per = True; break
|
| 283 |
-
ic = set(); oc = set()
|
| 284 |
-
for i, o in zip(inputs, outputs): ic |= set(np.unique(i)); oc |= set(np.unique(o))
|
| 285 |
-
return FanSignature(same_shape and has_enc, has_sym, is_expansion, has_enc or same_shape, has_per, bool(oc - ic) or bool(ic - oc))
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
@dataclass
|
| 289 |
-
class TransformationRule:
|
| 290 |
-
rule_type: str = "unknown"
|
| 291 |
-
size_ratio: Tuple[float, float] = (1.0, 1.0)
|
| 292 |
-
fill_color: int = 0
|
| 293 |
-
size_to_color: Dict = field(default_factory=dict)
|
| 294 |
-
frame_to_fill: Dict = field(default_factory=dict)
|
| 295 |
-
color_map: Dict = field(default_factory=dict)
|
| 296 |
-
tile_pattern: List = field(default_factory=list)
|
| 297 |
-
detected_period: int = 0
|
| 298 |
-
indicator_color: int = 0
|
| 299 |
-
target_color: int = 0
|
| 300 |
-
shape_to_color: Dict = field(default_factory=dict)
|
| 301 |
-
|
| 302 |
-
@classmethod
|
| 303 |
-
def learn(cls, train_pairs):
|
| 304 |
-
rule = cls(); sigmas = []
|
| 305 |
-
for pair in train_pairs:
|
| 306 |
-
pi = PhiField(pair['input']); po = PhiField(pair['output'])
|
| 307 |
-
s = SigmaResidue.from_transformation(pi, po); sigmas.append(s)
|
| 308 |
-
rule.size_ratio = (po.h / pi.h, po.w / pi.w)
|
| 309 |
-
rule._learn_from_pair(pi, po, s)
|
| 310 |
-
ct = [s.change_type for s in sigmas]; sc = [s.structural_condition for s in sigmas]
|
| 311 |
-
if all(t == "fill" and s == "enclosed" for t, s in zip(ct, sc)):
|
| 312 |
-
rule.rule_type = "multi_region_fill" if len(rule.size_to_color) > 1 and len(set(rule.size_to_color.values())) > 1 else "fill_enclosed"
|
| 313 |
-
elif all(t == "fill" for t in ct): rule.rule_type = "fill"
|
| 314 |
-
elif all(t == "recolor" for t in ct): rule.rule_type = "recolor"
|
| 315 |
-
elif all(t == "expansion" for t in ct):
|
| 316 |
-
if rule._check_tiling(train_pairs): rule.rule_type = "tile"
|
| 317 |
-
elif rule._check_self_tile(train_pairs): rule.rule_type = "self_tile"
|
| 318 |
-
elif rule.detected_period > 0: rule.rule_type = "periodic_extension"
|
| 319 |
-
else: rule.rule_type = "expansion"
|
| 320 |
-
elif rule.indicator_color != 0: rule.rule_type = "shape_indicator"
|
| 321 |
-
return rule
|
| 322 |
-
|
| 323 |
-
def _learn_from_pair(self, phi_in, phi_out, sigma):
|
| 324 |
-
if sigma.change_type == "fill" and sigma.structural_condition == "enclosed":
|
| 325 |
-
for frame in FieldInvariants.get_frame_components(phi_in):
|
| 326 |
-
fv = phi_out.q[frame['interior_mask']]
|
| 327 |
-
if len(fv) > 0:
|
| 328 |
-
u, c = np.unique(fv, return_counts=True); fc = int(u[np.argmax(c)])
|
| 329 |
-
if fc != 0:
|
| 330 |
-
self.size_to_color[frame['frame_size']] = fc; self.fill_color = fc
|
| 331 |
-
if frame['frame_color'] != 0: self.frame_to_fill[frame['frame_color']] = fc
|
| 332 |
-
for region in FieldInvariants.get_enclosed_regions(phi_in):
|
| 333 |
-
fs = FieldInvariants.frame_size(phi_in, region['mask']); fv = phi_out.q[region['mask']]
|
| 334 |
-
if len(fv) > 0:
|
| 335 |
-
u, c = np.unique(fv, return_counts=True); fc = int(u[np.argmax(c)])
|
| 336 |
-
if fc != 0 and fs not in self.size_to_color: self.size_to_color[fs] = fc; self.fill_color = fc
|
| 337 |
-
if phi_in.shape == phi_out.shape:
|
| 338 |
-
for col in phi_in.colors:
|
| 339 |
-
ov = phi_out.q[phi_in.q == col]; u = np.unique(ov)
|
| 340 |
-
if len(u) == 1 and u[0] != col: self.color_map[int(col)] = int(u[0])
|
| 341 |
-
if phi_in.shape != phi_out.shape and phi_in.w == phi_out.w:
|
| 342 |
-
p = FieldInvariants.detect_period_fourier(phi_in, axis=0)
|
| 343 |
-
if p > 0:
|
| 344 |
-
self.detected_period = p
|
| 345 |
-
ib = phi_in.q[:p, :]; ob = phi_out.q[:p, :]
|
| 346 |
-
for ci in set(ib.flatten()) - {0}:
|
| 347 |
-
ov = ob[ib == ci]; u = np.unique(ov)
|
| 348 |
-
if len(u) == 1 and u[0] != ci: self.color_map[int(ci)] = int(u[0])
|
| 349 |
-
if len(phi_in.colors) == 2: self._learn_shape_indicator(phi_in, phi_out)
|
| 350 |
-
self._learn_tile_pattern(phi_in, phi_out)
|
| 351 |
-
|
| 352 |
-
def _learn_shape_indicator(self, phi_in, phi_out):
|
| 353 |
-
if phi_in.shape != phi_out.shape: return
|
| 354 |
-
c1, c2 = sorted(phi_in.colors)
|
| 355 |
-
o1 = set(phi_out.q[phi_in.q == c1].flatten()) - {0}; o2 = set(phi_out.q[phi_in.q == c2].flatten()) - {0}
|
| 356 |
-
if len(o1) == 0 and len(o2) == 1: ind, tgt, oc = c1, c2, int(list(o2)[0])
|
| 357 |
-
elif len(o2) == 0 and len(o1) == 1: ind, tgt, oc = c2, c1, int(list(o1)[0])
|
| 358 |
-
else: return
|
| 359 |
-
self.indicator_color = ind; self.target_color = tgt
|
| 360 |
-
pos = list(zip(*np.where(phi_in.q == ind)))
|
| 361 |
-
if pos:
|
| 362 |
-
ss = FieldInvariants.shape_eigenspectrum(phi_in, pos)
|
| 363 |
-
if ss: self.shape_to_color[ss] = oc
|
| 364 |
-
|
| 365 |
-
def _learn_tile_pattern(self, phi_in, phi_out):
|
| 366 |
-
ih, iw = phi_in.shape; oh, ow = phi_out.shape
|
| 367 |
-
if oh < ih or ow < iw or oh % ih != 0 or ow % iw != 0: return
|
| 368 |
-
th, tw = oh // ih, ow // iw
|
| 369 |
-
if th == 1 and tw == 1: return
|
| 370 |
-
pattern = []
|
| 371 |
-
for ti in range(th):
|
| 372 |
-
row = []
|
| 373 |
-
for tj in range(tw):
|
| 374 |
-
t = phi_out.q[ti*ih:(ti+1)*ih, tj*iw:(tj+1)*iw]
|
| 375 |
-
if np.array_equal(t, phi_in.q): row.append(0)
|
| 376 |
-
elif np.array_equal(t, np.fliplr(phi_in.q)): row.append(1)
|
| 377 |
-
elif np.array_equal(t, np.flipud(phi_in.q)): row.append(2)
|
| 378 |
-
elif np.array_equal(t, np.rot90(phi_in.q, 2)): row.append(3)
|
| 379 |
-
else: row.append(-1)
|
| 380 |
-
pattern.append(row)
|
| 381 |
-
self.tile_pattern = pattern
|
| 382 |
-
|
| 383 |
-
def _check_tiling(self, pairs):
|
| 384 |
-
for p in pairs:
|
| 385 |
-
pi, po = PhiField(p['input']), PhiField(p['output'])
|
| 386 |
-
if po.h % pi.h != 0 or po.w % pi.w != 0: return False
|
| 387 |
-
th, tw = po.h // pi.h, po.w // pi.w
|
| 388 |
-
if th <= 1 and tw <= 1: return False
|
| 389 |
-
for ti in range(th):
|
| 390 |
-
for tj in range(tw):
|
| 391 |
-
t = po.q[ti*pi.h:(ti+1)*pi.h, tj*pi.w:(tj+1)*pi.w]
|
| 392 |
-
if not any(np.array_equal(t, x) for x in [pi.q, np.fliplr(pi.q), np.flipud(pi.q), np.rot90(pi.q, 2)]): return False
|
| 393 |
-
return True
|
| 394 |
-
|
| 395 |
-
def _check_self_tile(self, pairs):
|
| 396 |
-
for p in pairs:
|
| 397 |
-
pi, po = PhiField(p['input']), PhiField(p['output'])
|
| 398 |
-
ih, iw = pi.shape
|
| 399 |
-
if po.h != ih*ih or po.w != iw*iw: continue
|
| 400 |
-
ok = True
|
| 401 |
-
for ti in range(ih):
|
| 402 |
-
for tj in range(iw):
|
| 403 |
-
t = po.q[ti*ih:(ti+1)*ih, tj*iw:(tj+1)*iw]
|
| 404 |
-
if pi.q[ti, tj] != 0:
|
| 405 |
-
if not np.array_equal(t, pi.q): ok = False; break
|
| 406 |
-
elif np.any(t != 0): ok = False; break
|
| 407 |
-
if not ok: break
|
| 408 |
-
if ok: return True
|
| 409 |
-
return False
|
| 410 |
-
|
| 411 |
-
def apply(self, phi_in):
|
| 412 |
-
m = {'tile': self._apply_tile, 'self_tile': self._apply_self_tile, 'fill_enclosed': self._apply_fill,
|
| 413 |
-
'fill': self._apply_fill, 'multi_region_fill': self._apply_multi_fill,
|
| 414 |
-
'periodic_extension': self._apply_period, 'shape_indicator': self._apply_shape,
|
| 415 |
-
'recolor': self._apply_recolor}
|
| 416 |
-
return m.get(self.rule_type, lambda p: p.q.copy())(phi_in)
|
| 417 |
-
|
| 418 |
-
def _apply_tile(self, phi_in):
|
| 419 |
-
ih, iw = phi_in.shape; th, tw = int(self.size_ratio[0]), int(self.size_ratio[1])
|
| 420 |
-
r = np.zeros((ih*th, iw*tw), dtype=int)
|
| 421 |
-
xforms = [phi_in.q, np.fliplr(phi_in.q), np.flipud(phi_in.q), np.rot90(phi_in.q, 2)]
|
| 422 |
-
for ti in range(th):
|
| 423 |
-
for tj in range(tw):
|
| 424 |
-
code = self.tile_pattern[ti][tj] if self.tile_pattern and ti < len(self.tile_pattern) and tj < len(self.tile_pattern[ti]) else 0
|
| 425 |
-
r[ti*ih:(ti+1)*ih, tj*iw:(tj+1)*iw] = xforms[code] if 0 <= code <= 3 else phi_in.q
|
| 426 |
-
return r
|
| 427 |
-
|
| 428 |
-
def _apply_self_tile(self, phi_in):
|
| 429 |
-
ih, iw = phi_in.shape; r = np.zeros((ih*ih, iw*iw), dtype=int)
|
| 430 |
-
for ti in range(ih):
|
| 431 |
-
for tj in range(iw):
|
| 432 |
-
if phi_in.q[ti, tj] != 0: r[ti*ih:(ti+1)*ih, tj*iw:(tj+1)*iw] = phi_in.q
|
| 433 |
-
return r
|
| 434 |
-
|
| 435 |
-
def _apply_fill(self, phi_in):
|
| 436 |
-
r = phi_in.q.copy(); m = FieldInvariants.enclosed_mask(phi_in)
|
| 437 |
-
if np.any(m): r[m] = self.fill_color
|
| 438 |
-
return r
|
| 439 |
-
|
| 440 |
-
def _apply_multi_fill(self, phi_in):
|
| 441 |
-
r = phi_in.q.copy()
|
| 442 |
-
for frame in FieldInvariants.get_frame_components(phi_in):
|
| 443 |
-
fs = frame['frame_size']; fc = self.size_to_color.get(fs)
|
| 444 |
-
if fc is None and self.size_to_color:
|
| 445 |
-
fa = fs[0]*fs[1]; fc = self.size_to_color[min(self.size_to_color, key=lambda s: abs(s[0]*s[1]-fa))]
|
| 446 |
-
if fc is None: fc = self.frame_to_fill.get(frame.get('frame_color', 0))
|
| 447 |
-
if fc is None: fc = self.fill_color
|
| 448 |
-
if fc and fc != 0: r[frame['interior_mask']] = fc
|
| 449 |
-
return r
|
| 450 |
-
|
| 451 |
-
def _apply_period(self, phi_in):
|
| 452 |
-
if self.detected_period == 0: return phi_in.q.copy()
|
| 453 |
-
oh = int(phi_in.h * self.size_ratio[0]); base = phi_in.q[:self.detected_period, :].copy()
|
| 454 |
-
for oc, nc in self.color_map.items(): base[base == oc] = nc
|
| 455 |
-
reps = max(1, oh // self.detected_period)
|
| 456 |
-
return np.tile(base, (reps, 1))[:oh, :]
|
| 457 |
-
|
| 458 |
-
def _apply_shape(self, phi_in):
|
| 459 |
-
r = np.zeros_like(phi_in.q); pos = list(zip(*np.where(phi_in.q == self.indicator_color)))
|
| 460 |
-
if pos:
|
| 461 |
-
ss = FieldInvariants.shape_eigenspectrum(phi_in, pos); oc = self.shape_to_color.get(ss, 0)
|
| 462 |
-
if oc == 0:
|
| 463 |
-
best_d = float('inf')
|
| 464 |
-
for ks, kc in self.shape_to_color.items():
|
| 465 |
-
if ss and ks:
|
| 466 |
-
ml = min(len(ss), len(ks)); d = sum((a-b)**2 for a, b in zip(ss[:ml], ks[:ml]))
|
| 467 |
-
if d < best_d: best_d = d; oc = kc
|
| 468 |
-
r[phi_in.q == self.target_color] = oc
|
| 469 |
-
return r
|
| 470 |
-
|
| 471 |
-
def _apply_recolor(self, phi_in):
|
| 472 |
-
r = phi_in.q.copy()
|
| 473 |
-
for oc, nc in self.color_map.items(): r[phi_in.q == oc] = nc
|
| 474 |
-
return r
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
class ITTSolver:
|
| 478 |
-
def try_solve(self, task):
|
| 479 |
-
train = task.get('train', []); tests = task.get('test', [])
|
| 480 |
-
if not train: return None
|
| 481 |
-
rule = TransformationRule.learn(train)
|
| 482 |
-
if rule.rule_type == "unknown": return None
|
| 483 |
-
for pair in train:
|
| 484 |
-
pred = rule.apply(PhiField(pair['input'])); exp = np.array(pair['output'], dtype=int)
|
| 485 |
-
if pred.shape != exp.shape or not np.array_equal(pred, exp): return None
|
| 486 |
-
return [rule.apply(PhiField(t['input'])).tolist() for t in tests]
|
| 487 |
-
|
| 488 |
-
def try_solve_pair(self, inp, target, train_pairs):
|
| 489 |
-
rule = TransformationRule.learn(train_pairs)
|
| 490 |
-
if rule.rule_type == "unknown": return None
|
| 491 |
-
for pair in train_pairs:
|
| 492 |
-
pred = rule.apply(PhiField(pair['input'])); exp = np.array(pair['output'], dtype=int)
|
| 493 |
-
if pred.shape != exp.shape or not np.array_equal(pred, exp): return None
|
| 494 |
-
return rule.apply(PhiField(inp))
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
def _most_common(arr):
|
| 498 |
-
return Counter(arr.flatten().tolist()).most_common(1)[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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