Model-Based Local Search algorithm designer built on Routix
Project description
mbls
A Python toolkit for Model-Based Local Search (MBLS) algorithm design, built on top of Routix. This library provides a modular framework for orchestrating subroutines, managing experimental runs, and integrating mathematical models into heuristic search routines, with a focus on CP-SAT (OR-Tools) integration.
Core Components
CustomCpModel
A powerful extension of the standard OR-Tools CpModel. It simplifies the development of complex models by providing:
- Automatic Callback Registration: Automatically attaches callbacks for recording objective values and bounds during the search.
- Dynamic Constraint Management: Easily add or remove constraints between solver calls, which is ideal for iterative approaches like Large Neighborhood Search (LNS).
- Simplified Solver Interface: A streamlined API for configuring and running the CP-SAT solver.
CpSubroutineController
A specialized SubroutineController from routix for managing CP-SAT subroutines. It orchestrates the entire lifecycle of a model-solving process:
- Model Lifecycle Management: Handles the creation, modification, and solving of
CustomCpModelinstances. - Progress Tracking: Aggregates objective and bound logs from multiple solver runs into a single, coherent history.
- Flexible Algorithm Design: Enables the implementation of sophisticated metaheuristics where a CP-SAT model is solved repeatedly with different parameters or modifications.
Key Features
- Automated Progress Tracking: Built-in, type-safe callbacks automatically record objective values and bounds over time.
- Advanced Timeout Control: Stop the search not just by time limit, but also when there's no improvement for a specified duration.
- Comprehensive Solver Reports: Get detailed reports (
CpsatSolverReport) after each solve, including status, timings, and progress logs. - Dynamic Model Modification: Add and remove constraints on the fly, essential for implementing complex repair and destroy methods in LNS.
- Visualization Tools: A
paintermodule with tools to plot objective value/bound curves and other time-series data. - Modular and Extensible: Built on
routix, allowing you to create complex, multi-stage algorithms with clear and manageable code.
Installation
pip install mbls
- Requires Python 3.11
- Core dependency:
routix - For CP-SAT features:
ortools
Quick Example
Here is a simple example of how to use CustomCpModel to build and solve a model.
from mbls.cpsat import CustomCpModel
# 1. Define a custom model
class MyModel(CustomCpModel):
def __init__(self, n: int):
super().__init__()
self.x = self.new_int_var(0, n - 1, "x")
self.y = self.new_int_var(0, n - 1, "y")
self.add(self.x != self.y)
self.maximize(self.x + self.y)
# 2. Create an instance and solve it
model = MyModel(n=10)
status, elapsed_time, obj_value, obj_bound = model.solve_with_callbacks(
computational_time=10.0,
num_workers=4,
)
# 3. Access the results and progress logs
print(f"Status: {status.name}")
print(f"Objective Value: {obj_value}")
print("Objective value records:", model.get_obj_value_records())
print("Objective bound records:", model.get_obj_bound_records())
For more advanced use cases, like building an LNS-based heuristic, you would typically use a CpSubroutineController to manage the solving process.
Project Structure
src/mbls/
├── __init__.py
├── cpsat/
│ ├── __init__.py
│ ├── callbacks/
│ │ ├── __init__.py
│ │ ├── base_solution_callback.py
│ │ ├── by_call_recorder.py
│ │ ├── by_callback_recorder.py
│ │ ├── improv_timeout_callback.py
│ │ ├── improv_timeout_value_recorder.py
│ │ ├── objective_bound_recorder.py
│ │ └── objective_value_recorder.py
│ ├── cp_model_with_fixed_interval.py
│ ├── cp_model_with_optional_fixed_interval.py
│ ├── cp_subroutine_controller.py
│ ├── custom_cp_model.py
│ ├── obj_value_bound_store.py
│ ├── solver_report.py
│ └── status.py
├── painter/
│ ├── __init__.py
│ ├── obj_value_bound_plotter.py
│ └── time_series_plotter.py
└── time_stamped_recorder.py
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mbls-0.0.13.tar.gz.
File metadata
- Download URL: mbls-0.0.13.tar.gz
- Upload date:
- Size: 38.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cab06fb2f11180c295195cfb94dfd7b3eb2c0d673e856bfe7b483a1a3211b65
|
|
| MD5 |
e8616fdecd561633e7f8f7bc1c3ddd80
|
|
| BLAKE2b-256 |
91ecce6ec2e8863064aeb5cbab55ca1ca6d2d5b3626df409df9ce39cd2582024
|
File details
Details for the file mbls-0.0.13-py3-none-any.whl.
File metadata
- Download URL: mbls-0.0.13-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dd4649a75db5baee33073d281850f4a84e081e635b1b40e81197371b9e64683
|
|
| MD5 |
faacb066fbc008606ebb402b7c761d2a
|
|
| BLAKE2b-256 |
29b6b35bb235c0ea10b3f239caa066acf3c3c42ee6eab3c332aa8c0d66a77602
|