File size: 5,489 Bytes
2052f89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TriClock: synthetic fixed-camera streams; task = temporal LOCALIZATION.

Three objects (light color, agent position, furniture presence) each may undergo
one persistent change at an age drawn LOG-UNIFORM over [1, T]. The label per
object is WHEN it changed, bucketed by timescale:

    0 = no change   1 = fast (age 1-8)   2 = med (9-128)   3 = slow (129-T)

Why localization, not detection: any single old reference detects a persistent
change; only a schedule that BRACKETS the change age (a post-change slot below,
a pre-change slot above, within one bucket) can localize it. Log-backoff gives
uniform relative bracketing precision at every timescale — that's the claim
under test.

Anti-cheat properties: the current frame alone is uninformative (light colors
and agent positions are random per episode; 25% of episodes never had
furniture, so absence now does not imply removal).

Frames render functionally: frame(t) is deterministic given the episode seed,
so only the K+1 frames a policy looks at are ever rendered.
"""

import numpy as np

IMG = 32
T_HORIZON = 1024
FAST_MAX, MED_MAX = 8, 128  # bucket boundaries (octaves 0-3, 3-7, 7-10)

_COLORS = np.array([
    [0.9, 0.2, 0.2], [0.2, 0.9, 0.2], [0.2, 0.4, 0.9], [0.9, 0.9, 0.2],
    [0.9, 0.2, 0.9], [0.2, 0.9, 0.9], [0.95, 0.6, 0.1], [0.7, 0.7, 0.7],
])


def bucket(age, T=T_HORIZON):
    if age is None:
        return 0
    return 1 if age <= FAST_MAX else (2 if age <= MED_MAX else 3)


def _rect(img, x, y, w, h, color):
    img[max(0, y):y + h, max(0, x):x + w] = color


class Episode:
    def __init__(self, seed, horizon=T_HORIZON):
        rng = np.random.default_rng(seed)
        self.horizon = horizon

        self.bg = [(rng.integers(0, IMG - 6), rng.integers(0, IMG - 6),
                    rng.integers(4, 10), rng.integers(4, 10),
                    _COLORS[rng.integers(0, 8)] * 0.5) for _ in range(4)]

        def change_age():
            if rng.random() < 0.5:
                return None
            return int(round(np.exp(rng.uniform(0, np.log(horizon)))))

        self.age_light = change_age()
        self.age_agent = change_age()
        self.age_furn = change_age()

        # light: color c1 -> c2 (random ordered pair; current color uninformative)
        i, j = rng.choice(8, size=2, replace=False)
        self.light_c1, self.light_c2 = _COLORS[i], _COLORS[j]
        self.light_pos = (int(rng.integers(0, IMG - 5)), int(rng.integers(0, IMG - 5)))

        # agent: position A -> B (both random; current position uninformative)
        self.agent_a = (int(rng.integers(0, IMG - 4)), int(rng.integers(0, IMG - 4)))
        self.agent_b = (int(rng.integers(0, IMG - 4)), int(rng.integers(0, IMG - 4)))

        # furniture: present -> removed; 25% of episodes never had furniture
        self.furn_exists = bool(rng.random() < 0.75)
        if not self.furn_exists:
            self.age_furn = None
        self.furn_pos = (int(rng.integers(0, IMG - 7)), int(rng.integers(0, IMG - 7)))
        self.furn_color = _COLORS[rng.integers(0, 8)]

        self.ages = (self.age_light, self.age_agent, self.age_furn)
        self.labels = np.array([bucket(a, horizon) for a in self.ages], dtype=np.int64)

    @staticmethod
    def _after(age_of_change, query_age):
        return age_of_change is not None and query_age < age_of_change

    def frame(self, age):
        img = np.full((IMG, IMG, 3), 0.08, dtype=np.float32)
        for x, y, w, h, c in self.bg:
            _rect(img, int(x), int(y), int(w), int(h), c)
        if self.furn_exists and not self._after(self.age_furn, age):
            _rect(img, *self.furn_pos, 7, 7, self.furn_color)
        ax, ay = self.agent_b if self._after(self.age_agent, age) else self.agent_a
        _rect(img, ax, ay, 4, 4, np.array([1.0, 1.0, 1.0]))
        lc = self.light_c2 if self._after(self.age_light, age) else self.light_c1
        _rect(img, *self.light_pos, 5, 5, lc)
        return img

    def oracle_identifiable(self, slot_ages):
        """Bucket identifiable iff slots bracket the change age within one bucket.

        Consistent age interval given slots: (lo, hi] with lo = newest slot that
        looks pre-change-free (age < a), hi = oldest slot showing pre-change
        state (age >= a). Identifiable iff the whole interval maps to one bucket.
        For no-change objects: identifiable iff coverage reaches the horizon.
        """
        slot_ages = sorted(int(a) for a in slot_ages)
        out = []
        for a in self.ages:
            if a is None:
                out.append(slot_ages and slot_ages[-1] >= self.horizon)
                continue
            lo = max([s for s in slot_ages if s < a], default=0)
            hi = min([s for s in slot_ages if s >= a], default=None)
            if hi is None:
                out.append(False)  # no pre-change reference: confusable with none
            else:
                out.append(bucket(lo + 1) == bucket(hi))
        return np.array(out, dtype=bool)


def make_batch(seeds, ages_fn, rng):
    """(frames [B,K+1,H,W,3], ages [B,K+1], labels [B,3] int64). Slot 0 = now."""
    frames, ages_all, labels = [], [], []
    for s in seeds:
        ep = Episode(int(s))
        ages = ages_fn(rng)
        fs = [ep.frame(0)] + [ep.frame(int(a)) for a in ages]
        frames.append(np.stack(fs))
        ages_all.append(np.array([0] + list(ages), dtype=np.float32))
        labels.append(ep.labels)
    return np.stack(frames), np.stack(ages_all), np.stack(labels)