Skip to content

solvi.core.plan.strategist

The strategist: the functions and checks of a catalog assembled into a flow per request (Flow, PlanError).

Strategist: given the questions and init_state, picks functions AND checks from the catalog and assembles the flow.

Targets for each question

• it has a rule → the rule's arguments; • an answer head is fitted → the facts selected during fitting; • otherwise → the uses hint; otherwise everything computable from init_state (marked "flow not narrowed").

Then it walks back through signatures down to what init_state provides; the question's required parts (requires) are always added. Checks: besides those the targets need, the strategist takes every catalog check whose inputs are all already in the flow and that touches at least one COMPUTED (non-input) fact — a check on computed facts. Nothing else from the catalog is executed. Typed facts: the flow records the type of each fact it uses (flow.types: a producer's return type, or for a given fact the type its typed readers expect); producer / consumer types were already checked when the parts were registered. Decisions with a model: decision parts that read the same facts with the same model, when the model can answer several questions in one forward pass, are grouped (flow.batches); the executor scores each group in one pass.

Flow dataclass

Flow(steps: list, per_question: dict, skipped: dict, unresolved: dict, types: dict = dict(), batches: list = list(), answer_stages: list = list())

The parts a request runs, in order (solvi.core.plan.strategist.plan builds it).

Step dataclass

Step(part: object, reasons: list = list())

One step of a flow: the part and why the strategist took it.

Strategist

Bases: Protocol

The planner of a System: which parts and checks run for these questions on these given facts.

You implement: plan(catalog, questions, init_keys, heads=None) → a Flow (solvi.core.runtime: the steps in an order their inputs allow, the parts per question, the unresolved questions with the reasons). Pass it as System(..., strategist=yours). Give the flow a strategy dict (as CostStrategist does) to have the plan hashed into every trace (solvi.core.plan.cost.plan_record).

You get for free: hard checks enforced whatever the plan says (a hard check whose then= names a question must be in that question's flow, or System(...) refuses the strategist), types checked on every step, the plan recorded and replayed, every place that plans for the System (ask, fit, serve's input schemas, solvi check) seeing the same flow.

Stability: stable. DefaultStrategist and solvi.core.plan.cost.CostStrategist are stable to use, provisional to subclass.

DefaultStrategist

The deterministic planner every System uses when it is given none (plan in this module), as an object: the targets of each question (its rule's arguments, a fitted head's facts, its uses hint), walked back to the given facts, every check on computed facts that the flow can run, the required parts. System(strategist= DefaultStrategist()) plans exactly as System() does. Subclass it to change one step and keep the rest (call super().plan(...) and edit the Flow).

plan

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

→ Flow (see the module docs).

computable

computable(catalog, init_keys)

The facts this planner can compute from these given facts (every alternative producer's inputs needed).

Binary

Binary(min_rows=20, refit_every=50, ring=2000, prior=0.5)

P(y = 1 | row), learned online.

teach

teach(row, y)

One labelled row: y is True or False.

OrderModel

OrderModel(features=None, min_rows=20, refit_every=50)

P(hard check fails | cheap facts), one online model per hard check.

ProducerPolicy

ProducerPolicy(quality=0.9, explore=0.1, seed=0, min_rows=20, refit_every=50)

Chooses the order of a fact's alternative producers per input and learns from outcomes.

Alternatives whose predicted agreement with the reference (the last declared producer, normally the most trusted) is at least quality are tried first, cheapest expected cost first (cost ÷ P(accepted)); the rest follow in the same order. With probability explore the remaining producers also run after the accepted one, as a shadow: their outputs are never used, only compared, which gives agreement labels for producers the policy would otherwise stop trying.

plan

plan(group, row, costs)

→ (ordered alternatives, notes per alternative, shadow?)

observe

observe(group, row, outcomes)

outcomes: {producer: (accepted, value)} for the producers that ran on this input.

scalar_row

scalar_row(values, keys=None)

Cheap features from a dict of facts: numbers, booleans and short strings as they are; a long string gives its length; a list / tuple / dict gives its length. Anything else is left out.

computable

computable(catalog, init_keys)

All facts computable from init_state (closure over signatures).

given_facts

given_facts(catalog, questions=())

The facts the catalog reads but no part produces — what init_state has to provide: arguments of parts, rules and hard checks' then functions, and the questions' uses hints. (A rule that reads a question's name reads a given fact: answers are not facts.)