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-2025.5.1.tar.gz (10.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_cpsat-2025.5.1-py2.py3-none-any.whl (7.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pyomo_cpsat-2025.5.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for pyomo_cpsat-2025.5.1.tar.gz
Algorithm Hash digest
SHA256 029ec0da24bcd425948c24ce19db63a4418396d1d910b724f765d443b3325b00
MD5 b0a2240533a383f73037aed85291217e
BLAKE2b-256 237c4888305a6142103f468740c50d4d5d0d7c6683a3279d7b3c5c3ac89fa04a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyomo_cpsat-2025.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 71ee8bd6cc82887af540c34bac585298984b35187bdb56d32da3d82575199bb8
MD5 cd368198e0b53b172630b0839a333b96
BLAKE2b-256 a79b0b1700db76005d39ba131604b3a2adee591343cd9ad5a10b94d6971c6738

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