let domains author their own pass-2 estimator as formulas, not Python

Pass 2 previously only knew one estimator: a hardcoded physics model that
matches dimensions literally named platform/actuator/energy_storage. Any
domain outside that shape (e.g. archery) got all-zero estimates and failed
every combo. Domains can now declare free variables and per-metric formulas
as data instead; a safe AST-based evaluator (engine/formula.py, no eval())
resolves declared entity properties via dep(key, constraint_type) and
generalizes the existing hand-nested mass-budget search into an N-variable
recursive optimizer. Fully additive -- the legacy platform/actuator/
energy_storage path is untouched and still runs unchanged for every domain
that declares no formulas.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-16 21:28:15 -05:00
parent 3795a7e826
commit 3429bce8d0
15 changed files with 1064 additions and 9 deletions

View File

@@ -6,7 +6,7 @@ from datetime import datetime, timezone
from physcom.db.repository import Repository
from physcom.models.entity import Entity, Dependency
from physcom.models.domain import Domain, DomainConstraint, MetricBound
from physcom.models.domain import Domain, DomainConstraint, FreeVariable, MetricBound, MetricFormula
from physcom.models.combination import Combination
@@ -70,11 +70,27 @@ def export_snapshot(repo: Repository) -> dict:
"key": dc.key,
"allowed_values": dc.allowed_values,
})
fvs = []
for fv in d.free_variables:
fvs.append({
"name": fv.name,
"sort_order": fv.sort_order,
"floor_formula": fv.floor_formula,
"ceiling_formula": fv.ceiling_formula,
})
mfs = []
for mf in d.metric_formulas:
mfs.append({
"metric_name": mf.metric_name,
"formula": mf.formula,
})
domain_list.append({
"name": d.name,
"description": d.description,
"metric_bounds": mbs,
"constraints": dcs,
"free_variables": fvs,
"metric_formulas": mfs,
})
# Export combinations
@@ -207,11 +223,26 @@ def import_snapshot(repo: Repository, data: dict, *, clear: bool = False) -> dic
)
for dc in d_data.get("constraints", [])
]
fvs = [
FreeVariable(
name=fv["name"],
floor_formula=fv["floor_formula"],
ceiling_formula=fv["ceiling_formula"],
sort_order=fv.get("sort_order", 0),
)
for fv in d_data.get("free_variables", [])
]
mfs = [
MetricFormula(metric_name=mf["metric_name"], formula=mf["formula"])
for mf in d_data.get("metric_formulas", [])
]
domain = Domain(
name=d_data["name"],
description=d_data.get("description", ""),
metric_bounds=mbs,
constraints=dcs,
free_variables=fvs,
metric_formulas=mfs,
)
repo.add_domain(domain)
counts["domains"] += 1