此前"少了一条接触"只是全局计数,模型只能说"我在猜";它无法区分"目标根本没支撑" 和"目标的那条支撑被挡住了"。现在: - Observation.dangling:一端可见、另一端不可见的关系,锚在可见那一端,不带身份。 它说的是无知挂在哪儿,不是答案是什么。 - 模型多一个未知槽位(位置 1),悬空边的远端指向它;有悬空关系的锚定实体与它 相距一跳。一条链走到它上面,就是一条链走进了问题。 - 悬空边不参与连通计算、不进入实体的入射边求和、边特征里带 dangling 标志。 - 新增 area.free 家族:T 的唯一接触连到一个自身悬空的块,什么也没藏、什么也不悬空, 所以"没有支撑"是事实而非缺口,正确答案是 false,弃权才是错的。 它与 area.sup2h 构成最小对:同样是一条接触,一个通到未知、一个通到死路。
209 lines
6.8 KiB
Python
209 lines
6.8 KiB
Python
"""Data contract for the spatial-structure task.
|
|
|
|
Two records per sample, and the split between them is the whole point:
|
|
|
|
``Observation``
|
|
Everything the model may read. Visible entities and edges only. A hidden
|
|
inner-layer trace, a contact on the back face, or a constraint that lives in
|
|
a mate definition is simply absent here -- not masked, absent.
|
|
|
|
``FullState``
|
|
Everything the checker and the generator know. Never reaches the model.
|
|
|
|
If a generator bug leaks a hidden edge into the observation the fixture
|
|
``hidden_pair`` catches it, because that fixture builds two samples with
|
|
identical observation digests and opposite full-state answers.
|
|
|
|
Everything is integer geometry on a 0.25 mm grid, so a digest is reproducible
|
|
across machines and a "same seed rebuilds the same digest" check means
|
|
something.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from dataclasses import dataclass, field
|
|
from typing import Optional
|
|
|
|
EDGE_KINDS = ("contain", "adjacent", "contact", "constraint", "conduct")
|
|
ENTITY_KINDS = ("pad", "via", "trace", "block", "part", "ground")
|
|
ANSWERS = ("true", "false", "unknown")
|
|
CHANGES = ("changed", "same", "unknown")
|
|
DOF_AXES = ("tx", "ty", "tz", "rx", "ry", "rz")
|
|
|
|
|
|
def canonical(obj) -> str:
|
|
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
|
|
|
|
|
|
def digest(obj) -> str:
|
|
return hashlib.sha256(canonical(obj).encode("utf-8")).hexdigest()
|
|
|
|
|
|
@dataclass
|
|
class Entity:
|
|
eid: str
|
|
kind: str
|
|
frame: str = "world"
|
|
unit: str = "mm"
|
|
source: str = "given"
|
|
attrs: dict = field(default_factory=dict)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"eid": self.eid,
|
|
"kind": self.kind,
|
|
"frame": self.frame,
|
|
"unit": self.unit,
|
|
"source": self.source,
|
|
"attrs": dict(sorted(self.attrs.items())),
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class Edge:
|
|
eid: str
|
|
kind: str
|
|
a: str
|
|
b: str
|
|
layer: int = 0
|
|
source: str = "given"
|
|
attrs: dict = field(default_factory=dict)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"eid": self.eid,
|
|
"kind": self.kind,
|
|
"a": self.a,
|
|
"b": self.b,
|
|
"layer": self.layer,
|
|
"source": self.source,
|
|
"attrs": dict(sorted(self.attrs.items())),
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class Observation:
|
|
"""What the model sees. Only visible items are stored here.
|
|
|
|
``hidden`` and ``dangling`` are the two things here that are not visible, and
|
|
they are deliberate.
|
|
|
|
``hidden`` counts by kind what the full state contains and this view does
|
|
not. Without it "nothing is there" and "it is behind the part" are the same
|
|
input, and the two have different answers -- no reasoning can recover a
|
|
distinction the input never carried.
|
|
|
|
``dangling`` is the sharper form of the same idea: a relation whose one end
|
|
is visible and whose other end is not. It carries no identity -- the anchor
|
|
is the entity you can already see, never the one you cannot -- so it says
|
|
*where* the unknown attaches, not *what* the answer is. A support that
|
|
dangles still does not decide anything, which is why a chain that ends in a
|
|
dangling edge is evidence of ignorance rather than evidence of support.
|
|
"""
|
|
|
|
entities: list = field(default_factory=list)
|
|
edges: list = field(default_factory=list)
|
|
cameras: list = field(default_factory=list)
|
|
units: dict = field(default_factory=lambda: {"length": "mm", "per_unit": 1})
|
|
hidden_layers: list = field(default_factory=list)
|
|
hidden_faces: list = field(default_factory=list)
|
|
hidden: dict = field(default_factory=lambda: {"entity": {}, "edge": {}})
|
|
dangling: list = field(default_factory=list)
|
|
|
|
def entity_ids(self) -> set:
|
|
return {e.eid for e in self.entities}
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"entities": [e.to_dict() for e in sorted(self.entities, key=lambda x: x.eid)],
|
|
"edges": [e.to_dict() for e in sorted(self.edges, key=lambda x: x.eid)],
|
|
"cameras": list(self.cameras),
|
|
"units": dict(self.units),
|
|
"hidden_layers": sorted(self.hidden_layers),
|
|
"hidden_faces": sorted(self.hidden_faces),
|
|
"hidden": {k: dict(sorted(v.items())) for k, v in sorted(self.hidden.items())},
|
|
"dangling": [e.to_dict() for e in sorted(self.dangling, key=lambda x: x.eid)],
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class FullState:
|
|
"""Everything, including what the observation omits."""
|
|
|
|
entities: list = field(default_factory=list)
|
|
edges: list = field(default_factory=list)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"entities": [e.to_dict() for e in sorted(self.entities, key=lambda x: x.eid)],
|
|
"edges": [e.to_dict() for e in sorted(self.edges, key=lambda x: x.eid)],
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class Query:
|
|
kind: str # "connection" | "stability" | "freedom"
|
|
a: str = ""
|
|
b: str = ""
|
|
edit: Optional[dict] = None # {"op": "remove_edge", "edge": "<eid>"}
|
|
|
|
def to_dict(self) -> dict:
|
|
return {"kind": self.kind, "a": self.a, "b": self.b, "edit": self.edit}
|
|
|
|
|
|
@dataclass
|
|
class Label:
|
|
answer: str = "unknown" # one of ANSWERS
|
|
change: str = "unknown" # one of CHANGES
|
|
evidence: list = field(default_factory=list) # edge ids
|
|
sufficient: bool = False # observation alone decides the answer
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"answer": self.answer,
|
|
"change": self.change,
|
|
"evidence": sorted(self.evidence),
|
|
"sufficient": self.sufficient,
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class Scenario:
|
|
key: str
|
|
domain: str
|
|
family: str
|
|
seed: int
|
|
obs: Observation
|
|
query: Query
|
|
full: FullState
|
|
label: Label = field(default_factory=Label)
|
|
tags: dict = field(default_factory=dict)
|
|
|
|
# ---- digests -------------------------------------------------------
|
|
def input_dict(self) -> dict:
|
|
"""Exactly what the model may read. Two samples with the same digest
|
|
must be indistinguishable to the model, whatever their labels are."""
|
|
return {"obs": self.obs.to_dict(), "query": self.query.to_dict()}
|
|
|
|
def input_digest(self) -> str:
|
|
return digest(self.input_dict())
|
|
|
|
def full_digest(self) -> str:
|
|
return digest({"full": self.full.to_dict(), "query": self.query.to_dict(),
|
|
"obs": self.obs.to_dict()})
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"key": self.key,
|
|
"domain": self.domain,
|
|
"family": self.family,
|
|
"seed": self.seed,
|
|
"obs": self.obs.to_dict(),
|
|
"query": self.query.to_dict(),
|
|
"label": self.label.to_dict(),
|
|
"full": self.full.to_dict(),
|
|
"tags": dict(sorted(self.tags.items())),
|
|
"input_digest": self.input_digest(),
|
|
}
|