exp(spatial): 训练集加入拓扑不同的稳定性隐藏家族 area.sup3h

area.sup2h 的弃权三种子恒为 0:模型从未见过"稳定性家族里出现悬空接触"。加入
area.sup3h(三层堆叠、底座被藏、U 坐在 T 上),与测试用的 area.sup2h 拓扑不同、
机制相同,用来区分"机制没学会"和"这一族没见过"。
This commit is contained in:
x committed 2026-12-03 19:24:01 +08:00
1 parent 90be753718
commit 8ab284e146
2 files changed
+268 -267

No files matched your search

+2 -2
View File
@@ -17,14 +17,14 @@
"cond.p1h", "cond.p1h",
"area.sup2", "area.sup2",
"area.sup3", "area.sup3",
"area.sup3h",
"area.free", "area.free",
"dof.c6", "dof.c6",
"dof.c6r", "dof.c6r",
"dof.c6h" "dof.c6h"
], ],
"val_families": [ "val_families": [
"area.sup2h", "area.sup2h"
"area.sup2"
], ],
"test_families": [ "test_families": [
"area.sup2h", "area.sup2h",
+266 -265
View File
@@ -1,265 +1,266 @@
"""Procedural scenes. """Procedural scenes.
Three domains, each built so that a *structure family* -- not a single sample -- Three domains, each built so that a *structure family* -- not a single sample --
is the unit of split. Families differ in topology depth and in what the is the unit of split. Families differ in topology depth and in what the
observation withholds, never in rendering noise, so a test family is genuinely observation withholds, never in rendering noise, so a test family is genuinely
unseen structure. unseen structure.
Builders only construct geometry and visible subsets. They never write a label: Builders only construct geometry and visible subsets. They never write a label:
``checker.label`` does that, using the full state plus the world obtained by ``checker.label`` does that, using the full state plus the world obtained by
restricting the full state to what the observation shows. restricting the full state to what the observation shows.
""" """
from __future__ import annotations from __future__ import annotations
import random import random
from collections import Counter from collections import Counter
from dataclasses import replace from dataclasses import replace
from . import checker from . import checker
from .schema import (DOF_AXES, Edge, Entity, FullState, Observation, Query, from .schema import (DOF_AXES, Edge, Entity, FullState, Observation, Query,
Scenario) Scenario)
# family -> builder parameters. Splits are assigned in configs/*.json and must # family -> builder parameters. Splits are assigned in configs/*.json and must
# be disjoint over families. # be disjoint over families.
FAMILIES = { FAMILIES = {
"cond.p1": {"domain": "connection", "n_paths": 1, "hidden": 0}, "cond.p1": {"domain": "connection", "n_paths": 1, "hidden": 0},
"cond.p3": {"domain": "connection", "n_paths": 3, "hidden": 0}, "cond.p3": {"domain": "connection", "n_paths": 3, "hidden": 0},
"cond.p1h": {"domain": "connection", "n_paths": 2, "hidden": 1}, "cond.p1h": {"domain": "connection", "n_paths": 2, "hidden": 1},
"cond.p3h": {"domain": "connection", "n_paths": 4, "hidden": 1}, "cond.p3h": {"domain": "connection", "n_paths": 4, "hidden": 1},
"area.sup2": {"domain": "stability", "n_blocks": 2, "hidden": 0}, "area.sup2": {"domain": "stability", "n_blocks": 2, "hidden": 0},
"area.sup3": {"domain": "stability", "n_blocks": 3, "hidden": 0}, "area.sup3": {"domain": "stability", "n_blocks": 3, "hidden": 0},
"area.sup2h": {"domain": "stability", "n_blocks": 2, "hidden": 1}, "area.sup2h": {"domain": "stability", "n_blocks": 2, "hidden": 1},
"area.free": {"domain": "stability", "n_blocks": 2, "hidden": 0, "free": 1}, "area.free": {"domain": "stability", "n_blocks": 2, "hidden": 0, "free": 1},
"dof.c6": {"domain": "freedom", "n_cons": 6, "hidden": 0}, "area.sup3h": {"domain": "stability", "n_blocks": 3, "hidden": 1},
"dof.c6r": {"domain": "freedom", "n_cons": 7, "hidden": 0}, "dof.c6": {"domain": "freedom", "n_cons": 6, "hidden": 0},
"dof.c6h": {"domain": "freedom", "n_cons": 6, "hidden": 1}, "dof.c6r": {"domain": "freedom", "n_cons": 7, "hidden": 0},
} "dof.c6h": {"domain": "freedom", "n_cons": 6, "hidden": 1},
}
DOMAIN_OF = {f: spec["domain"] for f, spec in FAMILIES.items()}
DOMAIN_OF = {f: spec["domain"] for f, spec in FAMILIES.items()}
def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, cameras, units):
vis_e = [e for e in edges if e.eid in visible_edges] def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, cameras, units):
keep = set(visible_ents) | {e.a for e in vis_e} | {e.b for e in vis_e} vis_e = [e for e in edges if e.eid in visible_edges]
# derived, not hand-written: the gap between the full state and this view is keep = set(visible_ents) | {e.a for e in vis_e} | {e.b for e in vis_e}
# exactly what an agent has to know in order to decide it cannot decide. # derived, not hand-written: the gap between the full state and this view is
# Computing it here means a new occluder cannot silently go undeclared. # exactly what an agent has to know in order to decide it cannot decide.
hidden = { # Computing it here means a new occluder cannot silently go undeclared.
"entity": dict(Counter(e.kind for e in entities if e.eid not in keep)), hidden = {
"edge": dict(Counter(e.kind for e in edges if e.eid not in visible_edges)), "entity": dict(Counter(e.kind for e in entities if e.eid not in keep)),
} "edge": dict(Counter(e.kind for e in edges if e.eid not in visible_edges)),
# a hidden relation with one visible end becomes a dangling edge anchored on }
# that visible end. Both ends hidden leaves no mouth to report, so it stays # a hidden relation with one visible end becomes a dangling edge anchored on
# a bare count. The anchor is always something already in view: this says # that visible end. Both ends hidden leaves no mouth to report, so it stays
# where the ignorance attaches, never where it leads. # a bare count. The anchor is always something already in view: this says
dangling = [] # where the ignorance attaches, never where it leads.
for e in edges: dangling = []
if e.eid in visible_edges: for e in edges:
continue if e.eid in visible_edges:
anchor = e.a if e.a in keep else (e.b if e.b in keep else None) continue
if anchor is None: anchor = e.a if e.a in keep else (e.b if e.b in keep else None)
continue if anchor is None:
dangling.append(Edge(f"d{len(dangling)}", e.kind, anchor, "", continue
layer=e.layer, source="inferred")) dangling.append(Edge(f"d{len(dangling)}", e.kind, anchor, "",
return Observation( layer=e.layer, source="inferred"))
entities=[e for e in entities if e.eid in keep], return Observation(
edges=vis_e, entities=[e for e in entities if e.eid in keep],
cameras=list(cameras), edges=vis_e,
units=dict(units), cameras=list(cameras),
hidden_layers=sorted(hidden_layers), units=dict(units),
hidden=hidden, hidden_layers=sorted(hidden_layers),
dangling=dangling, hidden=hidden,
) dangling=dangling,
)
# --------------------------------------------------------------------------
# connection: parallel branches into one trunk, optional inner-layer branch # --------------------------------------------------------------------------
# # connection: parallel branches into one trunk, optional inner-layer branch
# A --branch i--> M --trunk--> B #
# # A --branch i--> M --trunk--> B
# removing one branch is harmless while another exists; removing the trunk is #
# never harmless. With one visible branch and one hidden branch the observation # removing one branch is harmless while another exists; removing the trunk is
# cannot tell those apart. # never harmless. With one visible branch and one hidden branch the observation
# cannot tell those apart.
def _build_connection(rng, n_paths: int, hidden: int):
ents = [Entity("A", "pad", attrs={"x": 0, "y": 0, "z": 0, "layer": 0}), def _build_connection(rng, n_paths: int, hidden: int):
Entity("B", "pad", attrs={"x": 100, "y": 0, "z": 0, "layer": 0}), ents = [Entity("A", "pad", attrs={"x": 0, "y": 0, "z": 0, "layer": 0}),
Entity("M", "via", attrs={"x": 50, "y": 0, "z": 0, "layer": 0})] Entity("B", "pad", attrs={"x": 100, "y": 0, "z": 0, "layer": 0}),
edges, visible, layers = [], set(), set() Entity("M", "via", attrs={"x": 50, "y": 0, "z": 0, "layer": 0})]
branch_edges = [] edges, visible, layers = [], set(), set()
for i in range(n_paths): branch_edges = []
is_hidden = i >= (n_paths - hidden) for i in range(n_paths):
layer = (1 + i % 2) if is_hidden else 0 is_hidden = i >= (n_paths - hidden)
vid = f"V{i}" layer = (1 + i % 2) if is_hidden else 0
ents.append(Entity(vid, "via", attrs={"x": 50, "y": 10 * (i + 1), "z": layer, "layer": layer})) vid = f"V{i}"
e1 = Edge(f"c{i}a", "conduct", "A", vid, layer=layer) ents.append(Entity(vid, "via", attrs={"x": 50, "y": 10 * (i + 1), "z": layer, "layer": layer}))
e2 = Edge(f"c{i}b", "conduct", vid, "M", layer=layer) e1 = Edge(f"c{i}a", "conduct", "A", vid, layer=layer)
edges += [e1, e2] e2 = Edge(f"c{i}b", "conduct", vid, "M", layer=layer)
if layer == 0: edges += [e1, e2]
visible.update({e1.eid, e2.eid}) if layer == 0:
branch_edges += [e1.eid, e2.eid] visible.update({e1.eid, e2.eid})
else: branch_edges += [e1.eid, e2.eid]
layers.add(layer) else:
trunk = Edge("trunk", "conduct", "M", "B", layer=0) layers.add(layer)
edges.append(trunk) trunk = Edge("trunk", "conduct", "M", "B", layer=0)
visible.add(trunk.eid) edges.append(trunk)
visible.add(trunk.eid)
query = Query(kind="connection", a="A", b="B")
r = rng.random() query = Query(kind="connection", a="A", b="B")
if r < 0.4 and branch_edges: r = rng.random()
query.edit = {"op": "remove_edge", "edge": branch_edges[0]} if r < 0.4 and branch_edges:
elif r < 0.7: query.edit = {"op": "remove_edge", "edge": branch_edges[0]}
query.edit = {"op": "remove_edge", "edge": "trunk"} elif r < 0.7:
query.edit = {"op": "remove_edge", "edge": "trunk"}
# a hidden branch must not appear as an isolated node either: the mere
# presence of a via on layer 1 is already a leak of the hidden detour # a hidden branch must not appear as an isolated node either: the mere
vis_ents = {"A", "B", "M"} | {f"V{i}" for i in range(n_paths - hidden)} # presence of a via on layer 1 is already a leak of the hidden detour
return ents, edges, query, visible, vis_ents, layers, ["top"], {"length": "mm", "per_unit": 1} vis_ents = {"A", "B", "M"} | {f"V{i}" for i in range(n_paths - hidden)}
return ents, edges, query, visible, vis_ents, layers, ["top"], {"length": "mm", "per_unit": 1}
# --------------------------------------------------------------------------
# stability: stacked blocks, optional hidden pedestal # --------------------------------------------------------------------------
# stability: stacked blocks, optional hidden pedestal
def _build_stability(rng, n_blocks: int, hidden: int, free: int = 0):
if free: def _build_stability(rng, n_blocks: int, hidden: int, free: int = 0):
# the minimal pair against area.sup2h: here T has exactly one contact and if free:
# that contact leads to a block which itself hangs in the air. Nothing # the minimal pair against area.sup2h: here T has exactly one contact and
# dangles and nothing is hidden, so "no support" is a fact rather than a # that contact leads to a block which itself hangs in the air. Nothing
# gap -- the answer is legitimately false and abstaining would be wrong. # dangles and nothing is hidden, so "no support" is a fact rather than a
ents = [Entity("G", "ground", attrs={"plane": 0}), # gap -- the answer is legitimately false and abstaining would be wrong.
Entity("T", "block", attrs={"box": [42, 42, 20, 58, 58, 36]}), ents = [Entity("G", "ground", attrs={"plane": 0}),
Entity("Q", "block", attrs={"box": [44, 44, 8, 56, 56, 20]}), Entity("T", "block", attrs={"box": [42, 42, 20, 58, 58, 36]}),
Entity("D", "block", attrs={"box": [80, 40, 0, 100, 60, 16]})] Entity("Q", "block", attrs={"box": [44, 44, 8, 56, 56, 20]}),
edges = [Edge("kTQ", "contact", "Q", "T"), Edge("kDG", "contact", "D", "G")] Entity("D", "block", attrs={"box": [80, 40, 0, 100, 60, 16]})]
query = Query(kind="stability", a="T") edges = [Edge("kTQ", "contact", "Q", "T"), Edge("kDG", "contact", "D", "G")]
if rng.random() < 0.6: query = Query(kind="stability", a="T")
query.edit = {"op": "remove_entity", "entity": "Q"} if rng.random() < 0.6:
return (ents, edges, query, {e.eid for e in edges}, {e.eid for e in ents}, query.edit = {"op": "remove_entity", "entity": "Q"}
set(), ["front", "iso"], {"length": "mm", "per_unit": 1}) return (ents, edges, query, {e.eid for e in edges}, {e.eid for e in ents},
set(), ["front", "iso"], {"length": "mm", "per_unit": 1})
# D is a decoy: it rests on the ground next to the stack and carries
# nothing. Without it "something was removed" and "the target lost support" # D is a decoy: it rests on the ground next to the stack and carries
# are the same event on every sample, and a one-bit rule fits the whole # nothing. Without it "something was removed" and "the target lost support"
# family -- measured: that rule scores 1.000 here while knowing no physics. # are the same event on every sample, and a one-bit rule fits the whole
ents = [Entity("G", "ground", attrs={"plane": 0}), # family -- measured: that rule scores 1.000 here while knowing no physics.
Entity("P", "block", attrs={"box": [40, 40, 0, 60, 60, 20]}), ents = [Entity("G", "ground", attrs={"plane": 0}),
Entity("T", "block", attrs={"box": [42, 42, 20, 58, 58, 36]}), Entity("P", "block", attrs={"box": [40, 40, 0, 60, 60, 20]}),
Entity("D", "block", attrs={"box": [80, 40, 0, 100, 60, 16]})] Entity("T", "block", attrs={"box": [42, 42, 20, 58, 58, 36]}),
edges = [Edge("kPG", "contact", "P", "G"), Edge("kPT", "contact", "T", "P"), Entity("D", "block", attrs={"box": [80, 40, 0, 100, 60, 16]})]
Edge("kDG", "contact", "D", "G")] edges = [Edge("kPG", "contact", "P", "G"), Edge("kPT", "contact", "T", "P"),
top = None Edge("kDG", "contact", "D", "G")]
if n_blocks >= 3: top = None
ents.append(Entity("U", "block", attrs={"box": [44, 44, 36, 56, 56, 48]})) if n_blocks >= 3:
edges.append(Edge("kTU", "contact", "U", "T")) ents.append(Entity("U", "block", attrs={"box": [44, 44, 36, 56, 56, 48]}))
top = "U" edges.append(Edge("kTU", "contact", "U", "T"))
vis_edges = {e.eid for e in edges} top = "U"
vis_ents = {e.eid for e in ents} vis_edges = {e.eid for e in edges}
layers = set() vis_ents = {e.eid for e in ents}
layers = set()
if hidden:
# the pedestal sits behind the front face: its contacts are unseen, and if hidden:
# the pedestal itself is not in view # the pedestal sits behind the front face: its contacts are unseen, and
for e in list(edges): # the pedestal itself is not in view
if "P" in (e.a, e.b): for e in list(edges):
vis_edges.discard(e.eid) if "P" in (e.a, e.b):
vis_ents.discard("P") vis_edges.discard(e.eid)
vis_ents.discard("P")
# the target is T on purpose: it survives every legal edit except the one
# that removes P, so "removed something" no longer implies "answer false" # the target is T on purpose: it survives every legal edit except the one
query = Query(kind="stability", a="T") # that removes P, so "removed something" no longer implies "answer false"
r = rng.random() query = Query(kind="stability", a="T")
if not hidden: r = rng.random()
harmless = top or "D" if not hidden:
if r < 0.4: harmless = top or "D"
query.edit = {"op": "remove_entity", "entity": "P"} # collapses if r < 0.4:
elif r < 0.75: query.edit = {"op": "remove_entity", "entity": "P"} # collapses
query.edit = {"op": "remove_entity", "entity": harmless} # no effect elif r < 0.75:
return ents, edges, query, vis_edges, vis_ents, layers, ["front", "iso"], {"length": "mm", "per_unit": 1} query.edit = {"op": "remove_entity", "entity": harmless} # no effect
return ents, edges, query, vis_edges, vis_ents, layers, ["front", "iso"], {"length": "mm", "per_unit": 1}
# --------------------------------------------------------------------------
# freedom: constraint sets, optionally redundant on one axis # --------------------------------------------------------------------------
# freedom: constraint sets, optionally redundant on one axis
def _build_freedom(rng, n_cons: int, hidden: int):
axes = list(DOF_AXES) + ["tz"] # a 7th constraint repeats tz: redundant def _build_freedom(rng, n_cons: int, hidden: int):
ents = [Entity("P", "part", attrs={"x": 0, "y": 0, "z": 0})] axes = list(DOF_AXES) + ["tz"] # a 7th constraint repeats tz: redundant
edges, visible = [], set() ents = [Entity("P", "part", attrs={"x": 0, "y": 0, "z": 0})]
for i in range(n_cons): edges, visible = [], set()
fid = f"F{i}" for i in range(n_cons):
ents.append(Entity(fid, "ground", attrs={"x": 0, "y": 0, "z": 0})) fid = f"F{i}"
e = Edge(f"k{i}", "constraint", "P", fid, attrs={"axis": axes[i]}) ents.append(Entity(fid, "ground", attrs={"x": 0, "y": 0, "z": 0}))
edges.append(e) e = Edge(f"k{i}", "constraint", "P", fid, attrs={"axis": axes[i]})
visible.add(e.eid) edges.append(e)
visible.add(e.eid)
vis_ents = {e.eid for e in ents}
layers = set() vis_ents = {e.eid for e in ents}
if hidden: layers = set()
visible.discard("k5") # one mate lives in the part definition if hidden:
vis_ents.discard("F5") visible.discard("k5") # one mate lives in the part definition
vis_ents.discard("F5")
query = Query(kind="freedom", a="P")
if n_cons == 7: query = Query(kind="freedom", a="P")
# removing the duplicate must not change the verdict; that is the point if n_cons == 7:
query.edit = {"op": "remove_edge", "edge": "k6"} # removing the duplicate must not change the verdict; that is the point
elif rng.random() < 0.6: query.edit = {"op": "remove_edge", "edge": "k6"}
query.edit = {"op": "remove_edge", "edge": "k0"} elif rng.random() < 0.6:
return ents, edges, query, visible, vis_ents, layers, ["iso"], {"length": "mm", "per_unit": 1} query.edit = {"op": "remove_edge", "edge": "k0"}
return ents, edges, query, visible, vis_ents, layers, ["iso"], {"length": "mm", "per_unit": 1}
_BUILDERS = {
"connection": lambda rng, spec: _build_connection(rng, spec["n_paths"], spec["hidden"]), _BUILDERS = {
"stability": lambda rng, spec: _build_stability(rng, spec["n_blocks"], spec["hidden"], "connection": lambda rng, spec: _build_connection(rng, spec["n_paths"], spec["hidden"]),
spec.get("free", 0)), "stability": lambda rng, spec: _build_stability(rng, spec["n_blocks"], spec["hidden"],
"freedom": lambda rng, spec: _build_freedom(rng, spec["n_cons"], spec["hidden"]), spec.get("free", 0)),
} "freedom": lambda rng, spec: _build_freedom(rng, spec["n_cons"], spec["hidden"]),
}
def make(family: str, seed: int, index: int = 0) -> Scenario:
spec = FAMILIES[family] def make(family: str, seed: int, index: int = 0) -> Scenario:
rng = random.Random(f"{family}:{seed}:{index}") spec = FAMILIES[family]
ents, edges, query, vis_edges, vis_ents, layers, cameras, units = \ rng = random.Random(f"{family}:{seed}:{index}")
_BUILDERS[spec["domain"]](rng, spec) ents, edges, query, vis_edges, vis_ents, layers, cameras, units = \
_BUILDERS[spec["domain"]](rng, spec)
full = FullState(entities=ents, edges=edges)
obs = _observe(ents, edges, vis_edges, vis_ents, layers, cameras, units) full = FullState(entities=ents, edges=edges)
# the world the observation alone describes: anything not visible is gone obs = _observe(ents, edges, vis_edges, vis_ents, layers, cameras, units)
restricted = FullState(entities=list(obs.entities), edges=list(obs.edges)) # the world the observation alone describes: anything not visible is gone
restricted = FullState(entities=list(obs.entities), edges=list(obs.edges))
sc = Scenario(key=f"{family}-{seed}-{index:04d}", domain=spec["domain"],
family=family, seed=seed, obs=obs, query=query, full=full, sc = Scenario(key=f"{family}-{seed}-{index:04d}", domain=spec["domain"],
tags={"n_hidden": spec["hidden"], "edit": query.edit is not None}) family=family, seed=seed, obs=obs, query=query, full=full,
sc.label = checker.label(obs, full, query, alternatives=[restricted]) tags={"n_hidden": spec["hidden"], "edit": query.edit is not None})
return sc sc.label = checker.label(obs, full, query, alternatives=[restricted])
return sc
# --------------------------------------------------------------------------
# invariances: camera and units must not change the answer # --------------------------------------------------------------------------
# invariances: camera and units must not change the answer
def with_camera(sc: Scenario, camera: str) -> Scenario:
out = replace(sc, obs=replace(sc.obs, cameras=[camera])) def with_camera(sc: Scenario, camera: str) -> Scenario:
out.tags = dict(sc.tags, camera=camera) out = replace(sc, obs=replace(sc.obs, cameras=[camera]))
return out out.tags = dict(sc.tags, camera=camera)
return out
def rescale(sc: Scenario, factor: int, unit: str) -> Scenario:
"""Multiply integer geometry by ``factor`` and relabel the unit. The answer def rescale(sc: Scenario, factor: int, unit: str) -> Scenario:
must be invariant; the fixture checks that it actually is.""" """Multiply integer geometry by ``factor`` and relabel the unit. The answer
def scale_ent(e: Entity) -> Entity: must be invariant; the fixture checks that it actually is."""
attrs = dict(e.attrs) def scale_ent(e: Entity) -> Entity:
for k in ("x", "y", "z"): attrs = dict(e.attrs)
if k in attrs: for k in ("x", "y", "z"):
attrs[k] = attrs[k] * factor if k in attrs:
if "box" in attrs: attrs[k] = attrs[k] * factor
attrs["box"] = [v * factor for v in attrs["box"]] if "box" in attrs:
return replace(e, attrs=attrs, unit=unit) attrs["box"] = [v * factor for v in attrs["box"]]
return replace(e, attrs=attrs, unit=unit)
full = FullState(entities=[scale_ent(e) for e in sc.full.entities], edges=list(sc.full.edges))
obs = replace(sc.obs, entities=[scale_ent(e) for e in sc.obs.entities], full = FullState(entities=[scale_ent(e) for e in sc.full.entities], edges=list(sc.full.edges))
edges=list(sc.obs.edges), obs = replace(sc.obs, entities=[scale_ent(e) for e in sc.obs.entities],
units={"length": unit, "per_unit": factor}) edges=list(sc.obs.edges),
out = replace(sc, obs=obs, full=full, tags=dict(sc.tags, unit=unit, scale=factor)) units={"length": unit, "per_unit": factor})
return out out = replace(sc, obs=obs, full=full, tags=dict(sc.tags, unit=unit, scale=factor))
return out