feat(spatial): 契约加 conflicts——两份来源打架时,只有两个世界都成立的答案才算答案

This commit is contained in:
liuhuajie committed 2026-12-04 01:15:31 +08:00
1 parent e799e02e22
commit a01ca7eab7
9 files changed
+206 -27

No files matched your search

+57 -13
View File
@@ -26,6 +26,17 @@ FAMILIES = {
"cond.p3": {"domain": "connection", "n_paths": 3, "hidden": 0},
"cond.p1h": {"domain": "connection", "n_paths": 2, "hidden": 1},
"cond.p3h": {"domain": "connection", "n_paths": 4, "hidden": 1},
# source conflicts. The dispute is drawn *within* the family and its
# position is the causal variable: a branch is redundant once a second one
# exists, the trunk never is. So the same scene shape yields true when the
# dispute sits on a branch and unknown when it sits on the trunk -- which is
# the rule the contract has to teach, and no constant answer fits it.
"src.scope": {"domain": "connection", "n_paths_pick": (2, 3),
"conflict_pick": (1, 3), "hidden": 0, "no_edit": 1},
"src.clean": {"domain": "connection", "n_paths_pick": (0, 1, 2),
"conflict": 0, "hidden": 0, "no_edit": 1},
"src.scope4": {"domain": "connection", "n_paths": 4,
"conflict_pick": (1, 3), "hidden": 0, "no_edit": 1},
"area.sup2": {"domain": "stability", "n_blocks": 2, "hidden": 0},
"area.sup3": {"domain": "stability", "n_blocks": 3, "hidden": 0},
"area.sup2h": {"domain": "stability", "n_blocks": 2, "hidden": 1},
@@ -49,7 +60,8 @@ FAMILIES = {
DOMAIN_OF = {f: spec["domain"] for f, spec in FAMILIES.items()}
def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, cameras, units):
def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, cameras,
units, conflicts=()):
vis_e = [e for e in edges if e.eid in visible_edges]
keep = set(visible_ents) | {e.a for e in vis_e} | {e.b for e in vis_e}
# derived, not hand-written: the gap between the full state and this view is
@@ -87,6 +99,7 @@ def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, camera
"rendered_relations": dict(Counter(e.kind for e in vis_e)),
"layers": _layers(entities),
"rendered_layers": _layers([e for e in entities if e.eid in keep]),
"conflicts": dict(Counter(c["kind"] for c in conflicts)),
}
return Observation(
entities=[e for e in entities if e.eid in keep],
@@ -96,6 +109,7 @@ def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, camera
hidden_layers=sorted(hidden_layers),
hidden=hidden,
dangling=dangling,
conflicts=list(conflicts),
scope=scope,
)
@@ -110,7 +124,8 @@ def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, camera
# cannot tell those apart.
def _build_connection(rng, n_paths: int, hidden: int):
def _build_connection(rng, n_paths: int, hidden: int, conflict: int = 0,
no_edit: int = 0):
ents = [Entity("A", "pad", attrs={"x": 0, "y": 0, "z": 0, "layer": 0}),
Entity("B", "pad", attrs={"x": 100, "y": 0, "z": 0, "layer": 0}),
Entity("M", "via", attrs={"x": 50, "y": 0, "z": 0, "layer": 0})]
@@ -134,16 +149,36 @@ def _build_connection(rng, n_paths: int, hidden: int):
visible.add(trunk.eid)
query = Query(kind="connection", a="A", b="B")
r = rng.random()
if r < 0.4 and branch_edges:
query.edit = {"op": "remove_edge", "edge": branch_edges[0]}
elif r < 0.7:
query.edit = {"op": "remove_edge", "edge": "trunk"}
# The source-conflict families stay edit-free on purpose: the query is about
# what two observers disagree on, and an intervention would be a third
# variable changing the answer at the same time.
if not (conflict or no_edit):
r = rng.random()
if r < 0.4 and branch_edges:
query.edit = {"op": "remove_edge", "edge": branch_edges[0]}
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
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}
# A dispute is about one relation, named by its endpoints: the netlist says
# A connects to V0, the board says there is no copper. Both edges of the
# branch can be disputed at once, in which case neither world decides.
conflicts = []
if conflict:
# 1 = one branch edge, 2 = both branch edges, 3 = the trunk. A disputed
# branch is survivable while another branch exists; a disputed trunk is
# not, because every path uses it.
disputed = {1: ["c0a"], 2: ["c0a", "c0b"], 3: ["trunk"]}[int(conflict)]
by_id = {e.eid: e for e in edges}
for eid in disputed:
e = by_id.get(eid)
if e is not None:
conflicts.append({"kind": e.kind, "a": e.a, "b": e.b,
"against": "board", "with": "netlist"})
return (ents, edges, query, visible, vis_ents, layers, ["top"],
{"length": "mm", "per_unit": 1}, conflicts)
# --------------------------------------------------------------------------
@@ -304,7 +339,9 @@ def _build_freedom(rng, n_cons: int, hidden: int, no_edit: int = 0, notch: int =
_BUILDERS = {
"connection": lambda rng, spec: _build_connection(rng, spec["n_paths"], spec["hidden"]),
"connection": lambda rng, spec: _build_connection(rng, spec["n_paths"], spec["hidden"],
spec.get("conflict", 0),
spec.get("no_edit", 0)),
"stability": lambda rng, spec: _build_stability(rng, spec["n_blocks"], spec["hidden"],
spec.get("free", 0), spec.get("notch", 0),
spec.get("clearance", 0),
@@ -315,13 +352,20 @@ _BUILDERS = {
def make(family: str, seed: int, index: int = 0) -> Scenario:
spec = FAMILIES[family]
rng = random.Random(f"{family}:{seed}:{index}")
ents, edges, query, vis_edges, vis_ents, layers, cameras, units = \
_BUILDERS[spec["domain"]](rng, spec)
spec = dict(FAMILIES[family])
# a spec may say "draw this instead of fixing it": the draw happens per
# sample, which is what makes the family two-label rather than one.
for key in ("n_paths", "conflict"):
pick = spec.pop(key + "_pick", None)
if pick:
spec[key] = rng.choice(list(pick))
built = _BUILDERS[spec["domain"]](rng, spec)
ents, edges, query, vis_edges, vis_ents, layers, cameras, units = built[:8]
conflicts = built[8] if len(built) > 8 else []
full = FullState(entities=ents, edges=edges)
obs = _observe(ents, edges, vis_edges, vis_ents, layers, cameras, units)
obs = _observe(ents, edges, vis_edges, vis_ents, layers, cameras, units, conflicts)
# the world the observation alone describes: anything not visible is gone
restricted = FullState(entities=list(obs.entities), edges=list(obs.edges))