Skip to main content

A Python package of utilities for Google's ortools CP-SAT solver.

Project description

cpsat-utils

Testing utilities for Google's OR-Tools CP-SAT solver. Designed for test-driven development of constraint programming models.

For a full walkthrough of test-driven optimization with CP-SAT, see the TDD chapter of the CP-SAT Primer.

Installation

pip install cpsat-utils

Usage

Context Managers

Assert feasibility or infeasibility of a model built inside a with block. The model is solved automatically when the block exits.

from cpsat_utils.testing import AssertModelFeasible, AssertModelInfeasible

def test_feasible():
    with AssertModelFeasible() as model:
        x = model.new_bool_var("x")
        y = model.new_int_var(0, 10, "y")
        model.add(x + y == 1)

def test_infeasible():
    with AssertModelInfeasible() as model:
        x = model.new_bool_var("x")
        y = model.new_bool_var("y")
        model.add(x + y == 3)

Assert that the optimal objective matches an expected value:

from cpsat_utils.testing import AssertObjectiveValue

def test_objective():
    with AssertObjectiveValue(objective=1.0) as model:
        x = model.new_bool_var("x")
        y = model.new_bool_var("y")
        model.add(x + y >= 1)
        model.minimize(x + y)

Assert optimality within a time limit:

from cpsat_utils.testing import AssertOptimalWithinTime

def test_optimal():
    with AssertOptimalWithinTime(time_limit=2.0) as model:
        x = model.new_bool_var("x")
        model.minimize(x)

Standalone Functions

For cases where you build the model separately (e.g., testing individual modules of a larger model):

from ortools.sat.python import cp_model
from cpsat_utils.testing import (
    assert_feasible,
    assert_infeasible,
    assert_optimal,
    assert_objective,
)

model = cp_model.CpModel()
x = model.new_bool_var("x")
model.add(x == 1)
model.minimize(x)

# Each function solves the model and checks the result.
assert_feasible(model)
assert_optimal(model)

# Check objective value (solves internally).
solver = assert_objective(model, expected=1.0)
# The returned solver can be inspected further.
assert solver.value(x) == 1

You can also pass an explicit solver to inspect variable values afterward:

solver = cp_model.CpSolver()
assert_objective(model=model, solver=solver, expected=1.0)
assert solver.value(x) == 1

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

cpsat_utils-0.2.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

cpsat_utils-0.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file cpsat_utils-0.2.0.tar.gz.

File metadata

  • Download URL: cpsat_utils-0.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cpsat_utils-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b408e22b1674c18def49c6db1c0ecf9d2e63e1e5b4e1e15d53812d4d62f0a008
MD5 f7a51e0137eecdf1bc72ba11bb8f8719
BLAKE2b-256 ad4ed35fc12ceaa033f00592017f373cb110dcc6149c45975ecaff4ee87cf7c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpsat_utils-0.2.0.tar.gz:

Publisher: release.yml on d-krupke/cpsat-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpsat_utils-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cpsat_utils-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cpsat_utils-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abd51e9650874b5459fd774b2388aaa6922d43290bf895170ca83674a7c0a93f
MD5 79b11825d1a23377142d3bdf646ba2b2
BLAKE2b-256 4045430f8c1401da2134efcf5872a19dd35c1e97caf5690e20119d6ee780ee31

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpsat_utils-0.2.0-py3-none-any.whl:

Publisher: release.yml on d-krupke/cpsat-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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