Skip to main content

Pyomo interface to CP-SAT

Project description

pyomo-cpsat

A Pyomo direct interface to the CP-SAT solver.

pyomo-cpsat is limited to solving pure integer linear programs with CP-SAT, or optimization models with

  • a linear objective function with real coefficients,
  • linear constraints with integral coefficients, and
  • bounded integer variables.

pyomo-cpsat does not implement other CP-SAT constraint types, such as cumulative constraints, reservoir constraints, etc.

Through a keyword argument, pyomo-cpsat can find infeasible subsystems of constraints for infeasible models, using the approach illustrated here.

pyomo-cpsat is currently experimental - it is based on the future Pyomo solver interface documented here, still under active development.

Examples

Solving a simple model

import pyomo.environ as pyo
from pyomo.contrib.solver.common.factory import SolverFactory
import pyomo_cpsat

model = pyo.ConcreteModel()

model.I = pyo.Set(initialize=[1, 2, 3])
model.w = pyo.Param(model.I, initialize={1: 10, 2: 20, 3: 30})
model.x = pyo.Var(model.I, domain=pyo.Integers, bounds=(0, 100))


def con_rule(model):
    return pyo.quicksum(model.w[i] * model.x[i] for i in model.I) <= 20

model.con = pyo.Constraint(rule=con_rule)

def obj_rule(model):
    return pyo.quicksum(model.x[i] for i in model.I)

model.obj = pyo.Objective(rule=obj_rule, sense=pyo.maximize)

solver = SolverFactory('cpsat')
results = solver.solve(
    model,
    tee=False,          # sets log_search_progress in CP-SAT
    threads=8,          # sets num_workers in CP-SAT
    time_limit=300,     # sets max_time_in_seconds in CP-SAT
    rel_gap=0.1,        # sets relative_gap_limit in CP-SAT
    abs_gap=1e-6,       # sets absolute_gap_limit in CP-SAT
    solver_options={    # passes CP-SAT parameters
        'subsolvers': ['pseudo_costs', 'probing']
    },
)

print(f'Termination condition: {results.termination_condition}')
print(f'Solution status: {results.solution_status}')
print('Solution:')
for i in model.I:
    print(f'  x[{i}] = {pyo.value(model.x[i])}')

Resulting output:

Termination condition: TerminationCondition.convergenceCriteriaSatisfied
Solution status: SolutionStatus.optimal
Solution:
  x[1] = 2
  x[2] = 0
  x[3] = 0

Finding an infeasible subsystem of constraints

import pyomo.environ as pyo
from pyomo.contrib.solver.common.factory import SolverFactory
import pyomo_cpsat

model = pyo.ConcreteModel()

model.I = pyo.Set(initialize=[1, 2, 3])
model.K = pyo.Set(initialize=['a', 'b'])
model.a = pyo.Param(
    model.K,
    model.I,
    initialize={
        ('a', 1): 1,
        ('a', 2): 2,
        ('a', 3): 3,
        ('b', 1): -1,
        ('b', 2): -1,
        ('b', 3): -1,
    },
)
model.b = pyo.Param(
    model.K,
    initialize={'a': 5, 'b': -10},
)

model.x = pyo.Var(model.I, domain=pyo.Integers, bounds=(0, 1))

def con_rule(model, k):
    return pyo.quicksum(model.a[k, i] * model.x[i] for i in model.I) <= model.b[k]

model.con = pyo.Constraint(model.K, rule=con_rule)

def obj_rule(model):
    return pyo.quicksum(model.x[i] for i in model.I)

model.obj = pyo.Objective(rule=obj_rule, sense=pyo.maximize)

solver = SolverFactory('cpsat')
results = solver.solve(model, find_infeasible_subsystem=True)

Resulting output:

Infeasible subsystem of constraints
-----------------------------------
con[b]

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_cpsat-2026.1.8.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

pyomo_cpsat-2026.1.8-py2.py3-none-any.whl (7.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyomo_cpsat-2026.1.8.tar.gz.

File metadata

  • Download URL: pyomo_cpsat-2026.1.8.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pyomo_cpsat-2026.1.8.tar.gz
Algorithm Hash digest
SHA256 877a8a55a4709043033ccab9a322e1357a4457d79359abc8d97a8b30bebb1418
MD5 d408aae597c7c765f8838321efaa242d
BLAKE2b-256 3b901bc36391011427bc4bd432b52601fd1ad29cd243efbc960cc3aa90d1365b

See more details on using hashes here.

File details

Details for the file pyomo_cpsat-2026.1.8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pyomo_cpsat-2026.1.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 92f25987b9d8e3e9924cfca36c5e5c19f6143a08fc5578f4ee55a9b17e30bc9e
MD5 70a12b4432eb564f4309300807280d68
BLAKE2b-256 dc4227db537022d20321328fc3bef6b66872d561d6436d65a537517bfb9a61ba

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