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

@@ -4,7 +4,7 @@ from __future__ import annotations
from flask import Blueprint, flash, redirect, render_template, request, url_for
from physcom.models.domain import Domain, MetricBound
from physcom.models.domain import Domain, FreeVariable, MetricBound, MetricFormula
from physcom_web.app import get_repo
bp = Blueprint("domains", __name__, url_prefix="/domains")
@@ -117,3 +117,96 @@ def metric_delete(domain_id: int, metric_id: int):
flash("Metric removed.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_metrics_table.html", domain=domain)
# ── Free variable CRUD (HTMX partials) ───────────────────────
@bp.route("/<int:domain_id>/free-vars/add", methods=["POST"])
def free_var_add(domain_id: int):
repo = get_repo()
name = request.form["name"].strip()
floor_formula = request.form.get("floor_formula", "").strip()
ceiling_formula = request.form.get("ceiling_formula", "").strip()
try:
sort_order = int(request.form.get("sort_order", "0"))
except ValueError:
sort_order = 0
if not name or not floor_formula or not ceiling_formula:
flash("Name, floor formula, and ceiling formula are required.", "error")
else:
fv = FreeVariable(
name=name, floor_formula=floor_formula, ceiling_formula=ceiling_formula,
sort_order=sort_order,
)
repo.add_free_variable(domain_id, fv)
flash(f"Free variable '{name}' added.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_free_vars_table.html", domain=domain)
@bp.route("/<int:domain_id>/free-vars/<int:fv_id>/edit", methods=["POST"])
def free_var_edit(domain_id: int, fv_id: int):
repo = get_repo()
try:
sort_order = int(request.form.get("sort_order", "0"))
except ValueError:
sort_order = 0
fv = FreeVariable(
name=request.form["name"].strip(),
floor_formula=request.form.get("floor_formula", "").strip(),
ceiling_formula=request.form.get("ceiling_formula", "").strip(),
sort_order=sort_order,
)
repo.update_free_variable(fv_id, fv)
flash("Free variable updated.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_free_vars_table.html", domain=domain)
@bp.route("/<int:domain_id>/free-vars/<int:fv_id>/delete", methods=["POST"])
def free_var_delete(domain_id: int, fv_id: int):
repo = get_repo()
repo.delete_free_variable(fv_id)
flash("Free variable removed.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_free_vars_table.html", domain=domain)
# ── Metric formula CRUD (HTMX partials) ──────────────────────
@bp.route("/<int:domain_id>/formulas/add", methods=["POST"])
def formula_add(domain_id: int):
repo = get_repo()
metric_name = request.form["metric_name"].strip()
formula = request.form.get("formula", "").strip()
if not metric_name or not formula:
flash("Metric name and formula are required.", "error")
else:
repo.add_metric_formula(domain_id, MetricFormula(metric_name=metric_name, formula=formula))
flash(f"Formula for '{metric_name}' added.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_formulas_table.html", domain=domain)
@bp.route("/<int:domain_id>/formulas/<int:formula_id>/edit", methods=["POST"])
def formula_edit(domain_id: int, formula_id: int):
repo = get_repo()
mf = MetricFormula(
metric_name=request.form["metric_name"].strip(),
formula=request.form.get("formula", "").strip(),
)
repo.update_metric_formula(formula_id, mf)
flash("Formula updated.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_formulas_table.html", domain=domain)
@bp.route("/<int:domain_id>/formulas/<int:formula_id>/delete", methods=["POST"])
def formula_delete(domain_id: int, formula_id: int):
repo = get_repo()
repo.delete_metric_formula(formula_id)
flash("Formula removed.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_formulas_table.html", domain=domain)

View File

@@ -0,0 +1,56 @@
<table id="formulas-table">
<thead>
<tr>
<th>Metric</th>
<th>Formula</th>
<th></th>
</tr>
</thead>
<tbody>
{% for mf in domain.metric_formulas %}
<tr>
<td>{{ mf.metric_name }}</td>
<td><code>{{ mf.formula }}</code></td>
<td class="actions">
<button class="btn btn-sm"
onclick="this.closest('tr').nextElementSibling.style.display='table-row'; this.closest('tr').style.display='none'">
Edit
</button>
<form method="post"
hx-post="{{ url_for('domains.formula_delete', domain_id=domain.id, formula_id=mf.id) }}"
hx-target="#formulas-section" hx-swap="innerHTML"
class="inline-form">
<button type="submit" class="btn btn-sm btn-danger">Del</button>
</form>
</td>
</tr>
<tr class="edit-row" style="display:none">
<form method="post"
hx-post="{{ url_for('domains.formula_edit', domain_id=domain.id, formula_id=mf.id) }}"
hx-target="#formulas-section" hx-swap="innerHTML">
<td><input name="metric_name" value="{{ mf.metric_name }}" required></td>
<td><input name="formula" value="{{ mf.formula }}" required></td>
<td>
<button type="submit" class="btn btn-sm btn-primary">Save</button>
<button type="button" class="btn btn-sm"
onclick="this.closest('tr').style.display='none'; this.closest('tr').previousElementSibling.style.display=''">
Cancel
</button>
</td>
</form>
</tr>
{% endfor %}
</tbody>
</table>
<h3>Add Formula</h3>
<form method="post"
hx-post="{{ url_for('domains.formula_add', domain_id=domain.id) }}"
hx-target="#formulas-section" hx-swap="innerHTML"
class="dep-add-form">
<div class="form-row">
<input name="metric_name" placeholder="metric name" required>
<input name="formula" placeholder='formula, e.g. draw_weight_chosen * 2' required>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>

View File

@@ -0,0 +1,64 @@
<table id="free-vars-table">
<thead>
<tr>
<th>Name</th>
<th>Order</th>
<th>Floor formula</th>
<th>Ceiling formula</th>
<th></th>
</tr>
</thead>
<tbody>
{% for fv in domain.free_variables %}
<tr>
<td>{{ fv.name }}</td>
<td>{{ fv.sort_order }}</td>
<td><code>{{ fv.floor_formula }}</code></td>
<td><code>{{ fv.ceiling_formula }}</code></td>
<td class="actions">
<button class="btn btn-sm"
onclick="this.closest('tr').nextElementSibling.style.display='table-row'; this.closest('tr').style.display='none'">
Edit
</button>
<form method="post"
hx-post="{{ url_for('domains.free_var_delete', domain_id=domain.id, fv_id=fv.id) }}"
hx-target="#free-vars-section" hx-swap="innerHTML"
class="inline-form">
<button type="submit" class="btn btn-sm btn-danger">Del</button>
</form>
</td>
</tr>
<tr class="edit-row" style="display:none">
<form method="post"
hx-post="{{ url_for('domains.free_var_edit', domain_id=domain.id, fv_id=fv.id) }}"
hx-target="#free-vars-section" hx-swap="innerHTML">
<td><input name="name" value="{{ fv.name }}" required></td>
<td><input name="sort_order" type="number" step="1" value="{{ fv.sort_order }}"></td>
<td><input name="floor_formula" value="{{ fv.floor_formula }}" required></td>
<td><input name="ceiling_formula" value="{{ fv.ceiling_formula }}" required></td>
<td>
<button type="submit" class="btn btn-sm btn-primary">Save</button>
<button type="button" class="btn btn-sm"
onclick="this.closest('tr').style.display='none'; this.closest('tr').previousElementSibling.style.display=''">
Cancel
</button>
</td>
</form>
</tr>
{% endfor %}
</tbody>
</table>
<h3>Add Free Variable</h3>
<form method="post"
hx-post="{{ url_for('domains.free_var_add', domain_id=domain.id) }}"
hx-target="#free-vars-section" hx-swap="innerHTML"
class="dep-add-form">
<div class="form-row">
<input name="name" placeholder="name, e.g. draw_weight_chosen" required>
<input name="sort_order" type="number" step="1" placeholder="order" value="0">
<input name="floor_formula" placeholder='floor formula, e.g. dep("draw_weight", "range_min")' required>
<input name="ceiling_formula" placeholder='ceiling formula, e.g. dep("draw_weight", "range_max")' required>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>

View File

@@ -33,4 +33,18 @@
<div id="metrics-section">
{% include "domains/_metrics_table.html" %}
</div>
<h2>Free Variables</h2>
<p class="hint">Quantities pass 2's estimator searches to maximize this domain's composite score. Leave empty for domains where nothing needs sizing.</p>
<div id="free-vars-section">
{% include "domains/_free_vars_table.html" %}
</div>
<h2>Metric Formulas</h2>
<p class="hint">How each metric's raw value is computed from declared entity properties (via <code>dep(key, constraint_type="provides")</code>) and any free variable above. If this domain declares any formulas, pass 2 uses them instead of the built-in vehicle physics model.</p>
<div id="formulas-section">
{% include "domains/_formulas_table.html" %}
</div>
{% endblock %}