Skip to content

solvi.core.plan.cost

The code strategist CostStrategist: dead ends dropped, the cheapest verified plan by declared costs — no model. See the strategist page.

The code strategist: dead ends dropped, the cheapest verified plan by declared (or measured) costs — no model.

The deterministic strategist (solvi.core.plan.strategist) walks back from each question's targets by exact names and, for a fact with alternative producers (provides=), needs the inputs of ALL of them (a fallback chain). This module plans differently:

  1. Points. A search (an exact 0/1 program; branch and bound as a fallback) picks ONE producer per needed fact so that the plan is valid and cheapest (declared cost=; a part without a declared cost counts as unit). A producer whose inputs cannot be computed from the given facts (a dead end) is never chosen. Hard checks that govern a question (its then names the question, it has no then, or the question requires it) and that the deterministic flow over the usable producers contains are mandatory milestones: every plan must keep them.
  2. Verification. The whole plan is checked (inputs bound, acyclic, types, every mandatory hard check kept); a plan that fails the check is used with the failure recorded (on_failure="code"), or falls back to the deterministic strategist ("deterministic"), or every question abstains ("abstain").

The plan is recorded in the trace as one hashed record (kind "plan"); trace.replay re-verifies it against the catalog.

from solvi.core.plan.cost import CostStrategist
system = System(cat, questions, strategist=CostStrategist())                          # dead ends dropped
system = System(cat, questions, strategist=CostStrategist(producers="equivalent"))    # the cheapest verified plan

(The model strategist, ModelStrategist, that proposed producers where declared costs did not settle the choice, was removed in 1.0: it gained nothing over declaring cost=.)

Selection dataclass

Selection(choice: dict, needed: set, cost: float, proven: bool = True, feasible: bool = True, why: str = '')

One producer per needed fact (names), the facts the plan needs, its cost and whether the search proved it cheapest.

CostStrategist

CostStrategist(producers='declared', on_failure='code', costs=None, keep_alternatives=True, record=True)

The code strategist for System(..., strategist=...): dead ends dropped, the cheapest verified plan by declared (or measured) costs — no model.

how to treat the alternative producers of a fact (provides=):

"declared" (default) — as the deterministic strategist does: a fallback chain in declaration order (the first is the preferred one), except that producers whose inputs cannot be computed (dead ends) are dropped instead of making the whole fact unreachable. Answers are those of the deterministic strategist wherever it can answer. "equivalent" — the producers of a fact are interchangeable (any accepted output is the same fact): one is chosen as the primary, the cheapest valid plan by declared cost= (unit when undeclared); the others stay as run-time fallbacks when their inputs are already computed (keep_alternatives=True).

on_failure: when the verified plan cannot be built — "code" (the code plan), "deterministic" (solvi.core.plan.strategist.plan) or "abstain" (every question abstains). keep_alternatives: keep the other producers of a fact as run-time fallbacks. record: write the plan record into the trace. (0.7 names, removed in 0.9: fallback= for on_failure=, fallbacks= for keep_alternatives=.)

plan

plan(catalog, questions, init_keys, heads=None, costs=None)

→ Flow. costs: {producer: cost} from the caller (until 1.0 System(cost_policy="measured") passed measured run times), under the strategist's own costs= (which wins where both name a producer).

narrowed

narrowed(group, tried)

A fact's producer group narrowed to the producers that ran (replay of a plan that dropped the others).

replay_plan

replay_plan(r, catalog, init_keys)

Re-verify a plan record (kind "plan", solvi.core.plan.cost) against the catalog: every chosen producer exists and provides its fact, and its inputs are given or chosen facts → [(step, name, reason)].

alternatives

alternatives(p)

The producers of a part's fact: its alternatives, or the part itself.

reachable

reachable(catalog, init_keys)

Facts computable from init_keys when a fact needs only ONE of its producers (the deterministic strategist needs the inputs of every alternative).

search

search(catalog, questions, init_keys, costs=None, heads=None, fixed=None, extra=(), unit=UNIT, method='auto')

Cheapest valid selection for the questions' targets, their required parts and extra parts (e.g. mandatory checks). fixed: {fact: producer name} choices that must be kept. method: "milp" (exact: a 0/1 program solved by scipy's HiGHS), "bnb" (branch and bound, capped at MAX_EXPAND nodes) or "auto" (milp when scipy has it). Ties go to the producer declared first. → Selection (feasible=False when a target cannot be computed; why says which).

mandatory_checks

mandatory_checks(catalog, questions, init_keys, heads=None)

Mandatory milestones: the hard checks that govern each question in the flow the deterministic strategist would build if every fact needed only its usable producers (dead ends dropped) — independent of which producer a plan chooses, so a shortcut that skips the fact such a check reads cannot drop the check. → {question: [check]}

view

view(catalog, choice, fallbacks=True, reach=None)

A shallow copy of the catalog in which every fact with alternative producers keeps only the chosen one (first) and, with fallbacks, the other producers whose inputs the plan already computes (after it, in declaration order) — unless such a producer reads, through any fact, the fact it would produce (then the plan could not run).

governing

governing(catalog, flow, questions)

Hard checks in a flow that govern each question (they decide its answer when false) → {question: [check]}.

validate

validate(catalog, flow, questions, init_keys, mandatory=None)

The plan checks → [problem]: every step's inputs are given or made by an earlier step (for a fact with alternative producers: every producer kept in the flow), the order is acyclic, producer and reader types fit, no question is left unresolved, and every mandatory hard check of a question is in its flow.

build

build(catalog, questions, init_keys, sel, heads=None, fallbacks=True, gov=None)

Selection → Flow (the deterministic strategist on the narrowed catalog, with mandatory checks as required parts).

plan_record

plan_record(flow)

The hashed trace record of a planned flow (kind "plan"): the chosen producers and the mandatory checks.