Skip to main content

Constraint-programming backends for Pyomo: a discretization transform plus CP solver interfaces (CP-SAT first).

Project description

pyomo-cp

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. See ROADMAP.md.

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

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}).

License

Apache License 2.0. See 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

pyomo_cp-0.1.0.tar.gz (20.1 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.1.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyomo_cp-0.1.0.tar.gz
  • Upload date:
  • Size: 20.1 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.1.0.tar.gz
Algorithm Hash digest
SHA256 e0cec1d2f1aed95be86d6fa0bf7cb9c0123456035a5e35f8150de308aa6d921a
MD5 cc6c4f57c767b67a1a66e3a266db4ba5
BLAKE2b-256 706df982b6f871cd1492f7995a85d30aa30e29ffef4fbd98482a4978e1860cee

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyomo_cp-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyomo_cp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56da08349954b2ee4ea51db9c667733c138b9594a43da3497c335f2fbc790ef7
MD5 8417d0ccab92dfe2a8e1c45c027a7557
BLAKE2b-256 6ae7a6c1e31794cbaca48c045809ec9731691dd283d8832a9b9543c7614c443b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyomo_cp-0.1.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.

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