Documentation
Run synchronous solves, score analysis, compiled solver.toml config, retained jobs, parallel move evaluation, per-phase telemetry, candidate diagnostics, snapshots, and dynamic move selectors.
Python Solving & Runtime
SolverForge Python has two runtime entry points:
Solverfor synchronous solve and score-analysis callsSolverManagerfor retained jobs, lifecycle events, snapshots, pause, resume, cancel, and delete
Both paths compile the authored Python schema into one immutable SolverForge 0.31.2 runtime graph. The binding supplies dynamic state, callbacks, slot capabilities, assignment groups, providers, and candidate metrics; the core owns phase construction, cursor execution, foraging, lifecycle control, and telemetry. There is no wrapper-owned fallback runner.
Synchronous Solve
solved = Solver.solve(schedule)
Solver.solve(...) reads the Python model, runs the solver, then returns a
solution object with updated planning variables and score.
Pass config explicitly when the solve needs a seed, termination budget, or custom phase list:
solved = Solver.solve(
schedule,
config={
"random_seed": 7,
"termination": {"seconds_spent_limit": 5},
},
)
Score Analysis
score = Solver.analyze(schedule)
print(score)
Solver.analyze(...) returns the calculated score and writes it to
schedule.score. It evaluates the named constraints for the current solution
state. Eligible
rules, including list precedence/makespan, use their native evaluators; rules
that need Python callbacks keep that path. Analysis does not run search or
change the planning assignments. See
Compiled Evaluation Boundary.
Config Loading
Config can be:
None- a
SolverConfig - a
dict[str, object] - a
solver.tomlfile in the current directory when the argument isNone
Supported termination fields are:
seconds_spent_limitminutes_spent_limitbest_score_limitstep_count_limitunimproved_step_count_limitunimproved_seconds_spent_limit
Termination fields can be set at the top level or under termination, but not
with conflicting values in both places. Every configured solver-level criterion
applies, combined with OR: the solve stops at whichever is reached first. A
zero seconds_spent_limit or minutes_spent_limit is a zero budget, not an
unlimited one.
No time limit is added for you. Without any termination criterion and without
phases, a solve runs construction and stops, because the default local search
is assembled only when a criterion exists. An explicit local_search phase
with no solver or phase limit runs until the process or a retained
cancel(...) stops it.
The core validates the whole config before the solve starts. Unknown keys,
count fields that must be positive (selected_count_limit, max_nearby on
the nearby selectors, min_ruin_count, min_sublist_size, conflict-repair
limits) set to zero, and out-of-range values such as a k_opt k outside
2 to 5 raise a runtime error before any search runs; a retained job enters
FAILED with the same message.
score_director.constraint_provider names the constraint provider by the
__name__ of the function passed to @planning_solution(constraints=...).
Omitting it selects that provider. A name the solution does not declare, or any
name on a solution without constraints, fails before solving:
[score_director]
constraint_provider = "constraints"
random_seed = 7
[termination]
seconds_spent_limit = 5
[[phases]]
type = "construction_heuristic"
[[phases]]
type = "local_search"
move_selector = { type = "change_move_selector" }
Parallel Move Evaluation
Acceptor-forager local search, including default local search, can evaluate candidate moves concurrently within one solve. Candidate generation and acceptance decisions remain ordered on one coordinator; workers score trials on separate working-state replicas. Each evaluator constructs its own constraint set from the same compiled schema and callbacks.
move_thread_count |
Behavior |
|---|---|
"auto" |
default; per step, sequential evaluation or parallel windows on up to four evaluators, whichever the measured trial, window, and replica costs favor |
"none" |
disable parallel move evaluation |
{"count": 4} |
request four evaluators, including the main director |
The effective count is bounded by the solve’s Rayon thread count and the
32-candidate evaluation-window ceiling. {"count": 1} stays sequential and
{"count": 0} is a configuration error. Because "auto" can evaluate in
parallel, scoring callbacks may run on several threads without any
move_thread_count key; pass "none" to keep a solve strictly sequential.
Construction and variable neighborhood descent remain sequential. The binding
passes this config to the core; it does not create a Python thread pool.
This fixed-step solve reuses the Schedule and Shift classes from
Getting Started.
Run it from the directory that holds that page’s schedule.py; importing the
module also runs and prints that page’s solves first:
from schedule import Schedule, Shift
from solverforge import Solver, SolverConfig
config = SolverConfig.from_dict({
"random_seed": 7,
"move_thread_count": {"count": 4},
"phases": [
{"type": "construction_heuristic"},
{"type": "local_search", "termination": {"step_count_limit": 60}},
],
})
solved = Solver.solve(Schedule([Shift() for _ in range(30)], [0, 1, 2]), config)
assert solved.score["family"] == "hard_soft"
assert solved.score["levels"][0] == 0
assert all(shift.nurse in (0, 1, 2) for shift in solved.shifts)
move_thread_count is not a direct SolverConfig(...) constructor keyword.
Use from_dict, from_toml, or extra={"move_thread_count": {"count": 4}}.
The equivalent TOML, loadable with SolverConfig.from_toml(text) or from
solver.toml, is:
random_seed = 7
move_thread_count = { count = 4 }
[[phases]]
type = "construction_heuristic"
[[phases]]
type = "local_search"
termination = { step_count_limit = 60 }
Reproducibility Limits
Use the same seed, input, configuration, and fixed work budget when comparing runs. Deterministic stateless neighborhoods can preserve assignments and score across evaluator counts. This is not a universal cross-count guarantee: stateful ruin/recreate selectors and custom candidate providers can advance differently when a parallel window pulls candidates ahead of the decision point. For those paths, parity is only at a fixed evaluator count. Keep that count unchanged when reproducing a result. Time limits can stop at different points regardless of the seed or environment mode.
Solver.solve(...) releases the GIL for the native solve so evaluator workers
can attach to Python. A GIL-enabled CPython build still serializes Python
callbacks; use free-threaded CPython 3.14 for concurrent callback execution.
Parallel execution is not a promise of faster solves: replica upkeep, callback
cost, and discarded speculative work affect throughput.
Retained Jobs
Use SolverManager when the application needs lifecycle state, progress,
snapshots, or cancellation.
from solverforge import SolverManager
manager = SolverManager(config={"termination": {"seconds_spent_limit": 10}})
handle = manager.solve(schedule)
status = manager.wait(handle.job_id, timeout_seconds=15)
snapshot = manager.snapshot(handle.job_id)
print(status["lifecycle_state"])
print(snapshot.score)
The manager exposes:
| Method | Purpose |
|---|---|
solve(solution) |
start a retained job and return a JobHandle |
get_status(job_id) |
read lifecycle state and current job metadata |
telemetry_detail(job_id) |
atomically read detailed telemetry and an optional candidate trace |
events(job_id) |
drain retained lifecycle events |
wait(job_id, timeout_seconds=...) |
block until completed, cancelled, or failed |
snapshot(job_id, snapshot_revision=None) |
export a deep-copied Python solution snapshot |
pause(job_id) |
request pause |
resume(job_id) |
resume a paused job |
cancel(job_id) |
request cancellation |
delete(job_id) |
remove retained job state |
Treat snapshots as point-in-time Python objects. Status and event telemetry
contain run totals and a phases list, with one record per phase started so
far in execution order. Before a phase starts the list can be empty. Each entry
includes phase_index, phase_type, elapsed_ms, step/move/score counters,
moves_speculative, generation_ms, and evaluation_ms. In a progress event,
the last entry is the running phase; after termination it is the last phase
that started, not an assertion that the job is still running.
Read per-phase state from phases, not a singular phase dictionary:
telemetry = status["telemetry"]
phases = telemetry["phases"]
last_phase = phases[-1] if phases else None
for phase in phases:
print(phase["phase_index"], phase["phase_type"], phase["moves_speculative"])
print("discarded look-ahead trials:", telemetry["moves_speculative"])
moves_evaluated counts trials consumed by the decision loop.
moves_speculative separately counts evaluated look-ahead trials discarded
after an early decision or interruption; it is not accepted or applied work.
Both run totals and phase records expose it. Sequential phases report zero.
Run totals also carry problem_changes_applied, which stays 0 because the
Python manager queues no problem changes.
telemetry_detail(job_id) adds selector_telemetry, move_telemetry, and
applied_move_trace records to the same totals, plus resolved_phase_plan:
the tree of phases, construction stages, and default local-search selectors the
core actually ran, with each node’s kind, attributes, and children. It is
None until the solve ends. Ordinary get_status(...) and events(...)
payloads leave it out.
detail = manager.telemetry_detail(handle.job_id)
plan = detail["status"]["telemetry"]["resolved_phase_plan"]
if plan is not None:
print(plan["kind"])
for phase in plan["children"]:
print(" ", phase["kind"], phase["attributes"])
Construction and local search publish the same engine-owned phase lifecycle and progress telemetry. The first observed construction work is reported promptly, and long-running phases continue at roughly one-second intervals when verbose logging is enabled. The Python layer does not synthesize construction counters.
Mandatory Completion And Limits
Configured solver and phase limits remain binding during construction. A best or completed solution is published only after every planning-list element, required assignment row, and non-optional scalar variable is assigned.
If a limit is reached first, Solver.solve(...) raises a runtime error. A
retained job enters FAILED without a latest snapshot revision, and
snapshot(...) remains unavailable instead of returning an incomplete plan. A
pause requested before mandatory construction completes preserves resumable
internal state, but it does not expose a partial snapshot.
Candidate Tracing
Candidate tracing is opt-in, bounded, and available only for retained manager jobs:
manager = SolverManager(
{
"termination": {"seconds_spent_limit": 10},
"candidate_trace": {"max_entries": 256},
}
)
handle = manager.solve(schedule)
manager.wait(handle.job_id, timeout_seconds=15)
detail = manager.telemetry_detail(handle.job_id)
trace = detail["candidate_trace"]
max_entries must be greater than zero. The format-3 trace reports canonical
configured input, execution policy, resolved phase-plan provenance, candidate
identities and dispositions, prefix digests, explicit completeness state, and
truncation. The total pull count continues after the bounded identity prefix is
full.
Discarded look-ahead window suffixes carry the discarded_speculative
disposition. An interrupted decision-point candidate can instead carry
interrupted_before_evaluation even if its speculative trial already ran.
Do not classify this work as acceptor rejections or applied moves.
Pull counts and trace prefixes can differ across evaluator
counts even when the final plan agrees. Compare global ordinal plus
identity; candidate_index_scope is source_local_only, so a leaf-local
candidate_index is not a cross-runtime identity.
Ordinary get_status(...), events(...), and snapshot(...) payloads do not
clone the trace. Solver.solve(...) rejects candidate_trace because the
synchronous API has no retained detail channel.
Use QualifiedCandidateTraceProvenance when an external harness has already
attested the schema, instance, initial state, core tree, and loaded build:
from solverforge import QualifiedCandidateTraceProvenance
provenance = QualifiedCandidateTraceProvenance(
schema_sha256="01" * 32,
instance_sha256="02" * 32,
initial_state_sha256="03" * 32,
core_tree_sha256="04" * 32,
build_sha256="05" * 32,
producer="solverforge-bench",
)
handle = manager.solve(
schedule,
qualified_candidate_trace_provenance=provenance,
)
The digests are keyword-only lowercase SHA-256 strings and producer must be
non-blank. Qualified provenance is accepted per job only when the manager has
candidate tracing enabled. It is never inferred from a solution or accepted as
ordinary serializable solver config.
Dynamic Move Support
Construction phases available to Python dynamic models include ordinary scalar first-fit and cheapest insertion. Assignment groups additionally support first-fit decreasing, weakest-fit, weakest-fit decreasing, strongest-fit, and strongest-fit decreasing; decreasing variants require entity order and weakest/strongest variants require value order. Dynamic list construction supports list round robin, cheapest insertion, regret insertion, Clarke-Wright, and k-opt polish when the model supplies the metadata bundle each phase requires.
An explicit assignment group_name obeys its configured obligation and
termination. Omitted construction resolves required and optional stages in the
core, but mandatory work never bypasses a configured limit. Default local
search is assembled only when the top-level termination has an effective finite
limit, so an empty or invalid termination cannot create an unbounded solve.
Scalar selectors available to Python dynamic models:
change_move_selectorswap_move_selectornearby_change_move_selectornearby_swap_move_selectorpillar_change_move_selectorpillar_swap_move_selectorruin_recreate_move_selectorgrouped_scalar_move_selectorconflict_repair_move_selectorcompound_conflict_repair_move_selector
List selectors available to Python dynamic models:
list_change_move_selectornearby_list_change_move_selectorlist_swap_move_selectornearby_list_swap_move_selectorsublist_change_move_selectorsublist_swap_move_selectorlist_reverse_move_selectorlist_permute_move_selectorlist_precedence_move_selectork_opt_move_selectorlist_ruin_move_selector
Selector combinators available to Python dynamic models:
limited_neighborhoodunion_move_selector- two-child
cartesian_product_move_selector
Required Selector Fields
Every selector config takes a type key. These selectors also require the
listed keys; the solver rejects a config without them as invalid solver config:
missing field. entity_class and variable_name are optional and pick the
planning variable when a model has more than one.
Selector type |
Required keys |
|---|---|
change_move_selector |
none |
swap_move_selector |
none |
nearby_change_move_selector |
max_nearby |
nearby_swap_move_selector |
max_nearby |
pillar_change_move_selector |
minimum_sub_pillar_size, maximum_sub_pillar_size |
pillar_swap_move_selector |
minimum_sub_pillar_size, maximum_sub_pillar_size |
ruin_recreate_move_selector |
min_ruin_count, max_ruin_count, recreate_heuristic_type |
grouped_scalar_move_selector |
group_name |
conflict_repair_move_selector |
constraints |
compound_conflict_repair_move_selector |
constraints |
list_change_move_selector |
none |
nearby_list_change_move_selector |
max_nearby |
list_swap_move_selector |
none |
nearby_list_swap_move_selector |
max_nearby |
sublist_change_move_selector |
min_sublist_size, max_sublist_size |
sublist_swap_move_selector |
min_sublist_size, max_sublist_size |
list_reverse_move_selector |
none |
list_permute_move_selector |
min_window_size, max_window_size |
list_precedence_move_selector |
none |
k_opt_move_selector |
k, min_segment_len, max_nearby |
list_ruin_move_selector |
min_ruin_count, max_ruin_count |
limited_neighborhood |
selected_count_limit, selector |
union_move_selector |
selectors |
cartesian_product_move_selector |
selectors |
recreate_heuristic_type is first_fit or cheapest_insertion. k_opt
max_nearby = 0 enumerates every reconnection.
Callbacks, Ownership, And Cursors
Grouped scalar and conflict-repair selectors require Python callbacks declared
on the solution with @scalar_group(...) and @conflict_repair(...).
Assignment-aware grouped scalar construction and grouped local search consume
scalar_assignment_group(...) metadata from the solution.
Assignment-owned variables are excluded from raw scalar, nearby, ruin, and conflict-repair selectors. Their declared group is the only construction and local-search ownership path. Multiple declarative assignment groups can still compose through selector combinators.
Every scalar selector also respects the entity row’s imported
candidate_values set as a hard domain. Change, swap, grouped-scalar, and
construction moves cannot assign a solution-level value that the row candidate
source excluded.
Dynamic neighborhoods are resumable cursor trees. Union children, limits, Cartesian branches, pillar windows, and k-opt reconnections advance only when the solver requests another candidate. Nearby Python callbacks may return any iterable; the binding consumes it once into an exact bounded top-k. Losing candidates are released when the forager no longer needs them, and only the winner crosses the ownership boundary.
Candidate Ordering
Leaf selectors can use original, seeded random, shuffled, sorted, or
probabilistic order. Sorted and probabilistic leaves name a candidate metric
registered through @candidate_metric and
@planning_solution(candidate_metrics=[...]):
[[phases]]
type = "local_search"
[phases.move_selector]
type = "change_move_selector"
entity_class = "Shift"
variable_name = "employee_idx"
selection_order = "sorted"
selection_metric = "dispatch_cost"
Sorted metrics are ascending. Metric results must be finite; probabilistic weights must also be non-negative, and zero-weight candidates are omitted.
Embedded UI Assets
The wheel embeds shared solverforge-ui assets through the native bridge.
Python HTTP hosts can serve those files without copying the UI bundle into each
example:
from solverforge import ui
asset = ui.asset("sf.js")
for path in ui.asset_paths():
print(path)
The hospital and deliveries examples use this bridge for /sf/* assets and
keep only app-specific browser modules under their own static/ directories.
Callback Threading
Python callbacks must be deterministic for the same solution state. The native extension may invoke callbacks repeatedly during scoring and search, including from Rayon evaluator workers. Filters, join keys, grouped collectors, weights, and metadata callbacks must not depend on invocation order or mutate shared Python state. Keep solution-level lookup context immutable throughout a solve. On free-threaded CPython 3.14, callback code and third-party extension modules used inside callbacks need to be safe for concurrent execution. Releasing the GIL in the calling thread does not make an unsafe callback safe.
The native working solution stores scalar, list, and candidate values by compiled descriptor index and shares immutable metadata across clones. Callback views project entity and fact collections from that Rust-owned state, so working, preview, and best-solution clones do not share mutable Python row objects. After the first full callback-view sync, only changed rows are synchronized.
Callback Errors And Interrupts
Synchronous solve and analysis re-raise the original Python callback exception
with its traceback, including failures originating in parallel evaluation.
KeyboardInterrupt raised by a callback or an attribute hook follows the same
error path; catch it explicitly if the application needs cleanup, because
except Exception does not catch it.
Retained jobs report background callback failures differently: the job enters
FAILED and the failed event’s error field contains the formatted Python
traceback. This includes a KeyboardInterrupt raised inside the job; it does
not turn into a KeyboardInterrupt raised by manager.wait(...). Errors during
schema compilation or model import, before a background job starts, can still
raise directly from manager.solve(...).
For callback-view, score-conversion, and dynamic compound-edit probes, only
AttributeError means an optional attribute is absent. Other exceptions,
including KeyboardInterrupt, propagate when the binding checks planning-field
descriptors while synchronizing callback views or looks up a score object’s
to_native. Compound edits that infer a target from an entity object read
its _solverforge_entity_class and _solverforge_entity_index markers when
explicit target fields are omitted; failures in those attribute lookups also
propagate. A failing attribute hook is not a missing field, a fallback score
format, or an absent edit target.
The binding suppresses the extra Rust thread panicked diagnostic only for
its recognized callback-error payload (SolverPanicPayload). Direct callers
still receive the original Python exception and traceback, and retained jobs
still record the failure. Other Rust panics are forwarded to the previously
installed panic hook; this is not a blanket suppression of panic diagnostics.
For example, this complete model deliberately raises an exception in a filter. The checks distinguish direct exception handling from retained failure events:
import traceback
from solverforge import (
Solver, SolverManager, constraint_provider,
planning_entity, planning_solution, planning_variable,
)
@planning_entity
class FailingTask:
worker = planning_variable(value_range_provider="workers", allows_unassigned=True)
def __init__(self, interrupt: bool = False):
self.worker = None
self.interrupt = interrupt
@constraint_provider
def failing_constraints(factory):
def fail(task):
if task.interrupt:
raise KeyboardInterrupt("callback interrupted")
raise ValueError("callback failed")
return [factory.for_each(FailingTask).filter(fail).penalize(1).named("failing rule")]
@planning_solution(constraints=failing_constraints)
class FailingPlan:
tasks: list[FailingTask]
def __init__(self, interrupt: bool = False):
self.tasks = [FailingTask(interrupt)]
self.workers = [0]
self.score = None
for interrupt, expected in [(False, ValueError), (True, KeyboardInterrupt)]:
for direct_call in (Solver.solve, Solver.analyze):
try:
direct_call(FailingPlan(interrupt))
except expected as error:
assert any(frame.name == "fail" for frame in traceback.extract_tb(error.__traceback__))
else:
raise AssertionError("expected the original callback exception")
manager = SolverManager()
handle = manager.solve(FailingPlan(interrupt))
status = manager.wait(handle.job_id, timeout_seconds=5)
assert status["lifecycle_state"] == "FAILED"
failed = [event for event in manager.events(handle.job_id) if event["event_type"] == "FAILED"]
assert len(failed) == 1
assert expected.__name__ in failed[0]["error"]
assert "in fail" in failed[0]["error"]
manager.delete(handle.job_id)
Use manager.cancel(job_id) for retained-job cancellation. A Python-raised
KeyboardInterrupt is an exception, not a lifecycle cancellation request;
these error-path semantics do not guarantee prompt handling of process-level
SIGINT in every solver phase or native operation.