Skip to main content

pyomo-cp

PyPI Python versions CI License

Constraint-programming backends for Pyomo: a discretization transform plus CP solver interfaces, starting with CP-SAT (OR-Tools).

Pyomo can build and solve disjunctive/logical models via pyomo.gdp, but it can only solve them by reformulating to MILP/MINLP (big-M, hull) and calling a MIP solver. pyomo-cp adds the missing path: take the same model and solve it with a constraint-programming solver, where disjunctions map to native reified constraints instead of being reformulated.

Status: alpha. Integer models, pyomo.gdp disjunctions, logical constraints / Boolean variables, and discretization of continuous variables (any step) work end-to-end via SolverFactory('cpsat'). Global constraints are not yet supported.

Scope

pyomo-cp is a backend framework, not a full CP modelling frontend. It translates what Pyomo can already express:

  • integer and (once discretized) continuous variables,
  • linear constraints and a linear objective,
  • logical constraints (BooleanVar / LogicalConstraint),
  • pyomo.gdp disjunctions.

Global constraints (alldifferent, no_overlap, element, cumulative, ...) are the heart of CP's modelling power, and Pyomo has no vocabulary for them. Adding one is roadmap, not part of the initial scope.

CP solvers are finite-domain, so continuous variables must be discretized first. That is an explicit step (TransformationFactory('cp.discretize')), never automatic, because the integrality assumption is a modelling decision that changes the problem.

Relationship to pyomo.contrib.cp

Pyomo ships an in-tree constraint-programming module, pyomo.contrib.cp. It is a different tool for a different job, and the two are complementary:

pyomo.contrib.cp this package
Role CP frontend CP backend
Built for scheduling: IntervalVar, sequence vars, Pulse / step functions, precedence solving models Pyomo already expresses — no new constructs
Input a CP model written with the interval API an existing pyomo.gdp / integer / logical model
Continuous variables none (finite-domain only) explicit cp.discretize onto a grid
Solver IBM CP Optimizer (commercial, via docplex) CP-SAT / OR-Tools (open-source)

In one line: pyomo.contrib.cp is write an interval/scheduling model and solve it with CP Optimizer; this package is take a disjunctive/integer model you'd otherwise reformulate to MILP and solve it (optionally discretized) with CP-SAT. They overlap only on logical constraints; the paradigm (interval scheduling vs disjunctive/geometric) and the solver (commercial vs open) otherwise differ.

Install

pip install "pyomo-cp[cpsat]"   # includes OR-Tools for the CP-SAT backend

Usage

import pyomo.environ as pyo
from pyomo.gdp import Disjunct, Disjunction
import pyomo_cp  # registers cp.discretize and the cpsat solver

# Two boxes (lengths 2 and 3) that must not overlap on a line; minimize extent.
m = pyo.ConcreteModel()
m.x1 = pyo.Var(bounds=(0, 10))          # continuous positions
m.x2 = pyo.Var(bounds=(0, 10))
m.L = pyo.Var(bounds=(0, 10))
m.e1 = pyo.Constraint(expr=m.L >= m.x1 + 2)
m.e2 = pyo.Constraint(expr=m.L >= m.x2 + 3)
m.d1 = Disjunct(); m.d1.c = pyo.Constraint(expr=m.x1 + 2 <= m.x2)  # box1 left of box2
m.d2 = Disjunct(); m.d2.c = pyo.Constraint(expr=m.x2 + 3 <= m.x1)  # box2 left of box1
m.no = Disjunction(expr=[m.d1, m.d2])
m.obj = pyo.Objective(expr=m.L)

pyo.TransformationFactory("cp.discretize").apply_to(m)      # explicit; unit grid
res = pyo.SolverFactory("cpsat").solve(m, time_limit=10)
print(res.solver.termination_condition, pyo.value(m.obj))  # optimal 5.0

See examples/plant_layout.ipynb for a complete worked example: a 17-block plant-layout GDP model solved to proven optimality by CP-SAT via cp.discretize.

Solver options are passed as friendly aliases (time_limit, workers, seed, gap) or as raw CP-SAT parameter names via options={...}, e.g. solve(m, workers=8, options={"log_search_progress": True}).

Maintainer

Maintained by @devin-griff. Issues and pull requests welcome.

License

BSD 3-Clause License. See LICENSE.

Download files

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

Source Distribution

pyomo_cp-0.2.0.tar.gz (51.2 kB view details)

Uploaded Source

Built Distribution

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

pyomo_cp-0.2.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyomo_cp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fcb96c2516810894e779ddfdb37ec5d00e742c4c8e80ca5a55d634616764ad8b
MD5 688a4a42969b20a32722fa10c7b4b232
BLAKE2b-256 4da9ca5ed0e9bbee0648033602809b6c0c69a5728e7055b6e6cf561acfa24d6b

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on devin-griff/pyomo-cp

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

File details

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

File metadata

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

File hashes

Hashes for pyomo_cp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 873fac7f4c2371a20d995df4159baf57ff4b0b736d211182f4266f91118845f7
MD5 7e467795488314625b97fedc9db734c5
BLAKE2b-256 a0247d5470b79b938562501767dd12d4b9cf625ac6d4a1861f7daa4b3d9bee2d

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on devin-griff/pyomo-cp

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

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page