Skip to main content

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 CustomCpModel instances.
  • 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 painter module 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mbls-0.0.15.tar.gz (79.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mbls-0.0.15-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

Details for the file mbls-0.0.15.tar.gz.

File metadata

  • Download URL: mbls-0.0.15.tar.gz
  • Upload date:
  • Size: 79.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mbls-0.0.15.tar.gz
Algorithm Hash digest
SHA256 699c087d64be26cb4b114f57b82f774eac72fdca6b743edc4e24e377e8d5746c
MD5 64c207af9f9d094d71eb462e3ba32a5c
BLAKE2b-256 24788ef9055159ce6625c3e90a7936f33743279e4aa1336c4b745c05060a7cca

See more details on using hashes here.

File details

Details for the file mbls-0.0.15-py3-none-any.whl.

File metadata

  • Download URL: mbls-0.0.15-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mbls-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 f833b64fe90cab81179b6c2175bfaca53c6932adec6acf18f30084fd2bb5a26b
MD5 6e200265d74ea03a2fcdca3c79c80411
BLAKE2b-256 5897b015319991cf4d38cc67e4cd55e22026842f38937443aba5097176bdadd7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page