Custom Solvers
Guide for integrating custom solvers with the use case framework.
Solver Interface
Any solver that can accept a Model and return a Solution works with the framework.
Example: Custom Solver Wrapper
Wrap your solver so that solve takes the Model produced by a formulation and
returns a Solution the use case can interpret. The wrapper below delegates to
SCIP; replace the body of solve with the call into your own solver, converting
to and from its native format as needed.
from luna_quantum.algorithms import SCIP
class CustomSolver:
"""Wrapper for custom optimization solver."""
def __init__(self, **config):
self.config = config
def solve(self, model):
"""Solve the model and return solution."""
# Convert the model to your solver's format, solve, and convert the
# raw result back to a Solution. Here that is one delegation:
job = SCIP(**self.config).run(model)
return job.result()
Using with Use Cases
import numpy as np
from luna_usecases.traveling_salesperson_problem import (
TspData,
TspFormulation,
TspInstance,
)
data = TspData(
data_name="three_cities",
city_names=["Berlin", "Hamburg", "Munich"],
distance_matrix=np.array(
[[0.0, 289.0, 585.0], [289.0, 0.0, 796.0], [585.0, 796.0, 0.0]]
),
start_city="Berlin",
)
instance = TspInstance(data=data, formulation=TspFormulation())
model = instance.formulate()
# Use custom solver
solver = CustomSolver()
solution = solver.solve(model)
# Interpret results
result = instance.interpret(solution)
print(result.to_string())