Solver

Configure and run the solver to find optimal solutions.

The solver is the engine that finds optimal solutions to your planning problems. This section covers how to configure and run it.

Topics

Quick Example

from solverforge_legacy.solver import SolverFactory
from solverforge_legacy.solver.config import (
    SolverConfig, ScoreDirectorFactoryConfig, TerminationConfig, Duration
)

# Configure the solver
solver_config = SolverConfig(
    solution_class=Timetable,
    entity_class_list=[Lesson],
    score_director_factory_config=ScoreDirectorFactoryConfig(
        constraint_provider_function=define_constraints
    ),
    termination_config=TerminationConfig(
        spent_limit=Duration(seconds=30)
    )
)

# Create and run the solver
solver_factory = SolverFactory.create(solver_config)
solver = solver_factory.build_solver()

problem = load_problem()  # Your problem data
solution = solver.solve(problem)

print(f"Best score: {solution.score}")

Termination

The solver needs to know when to stop. Common termination conditions:

ConditionDescription
spent_limitStop after a time limit (e.g., 30 seconds)
best_score_limitStop when a target score is reached
unimproved_spent_limitStop if no improvement for a duration
step_count_limitStop after a number of optimization steps

Solver Configuration

Configure the solver with SolverConfig and related classes.

Running the Solver

Execute the solver synchronously with Solver.solve().

SolverManager

Manage concurrent and asynchronous solving jobs.

SolutionManager

Analyze and explain solutions with SolutionManager.

Benchmarking

Compare solver configurations and tune performance.