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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pyomo_cpsat-2025.5.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.tar.gz
Algorithm Hash digest
SHA256 2e20843e049d8b2218a4a1026d236450aa8fc1c4bb471f2929cde48a0039db5e
MD5 8041b8d882d31337cb9f0b5ec848326e
BLAKE2b-256 3f307e37bafd012c96c2b7d96994200bc6f8f398b20dbd7a16fb75039d89aa1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyomo_cpsat-2025.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6266a99da447d9a54f6d3319ee2169d713ac9594bdcf15398360ef67b48c0117
MD5 32e6c1fc24ce7ef4bd4339d5a8e3796f
BLAKE2b-256 80b484f47be52b92e06d5c009ebbb81893db6c1505a5c0c4feda2020eedccf54

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