CP-SAT#

CP-SAT is the constraint programming solver of Google OR-Tools. It combines a SAT solver with lazy clause generation, linear relaxations and large neighborhood search, and solves models with integer variables: scheduling, assignment, timetabling and other combinatorial problems. The driver is built on the MP library: logical and counting constraints are passed to CP-SAT natively, and other expressions are reformulated.

Learn More | Modeling guide | Features | Options | Changes

Download CP-SAT

How to use it#

How to install using amplpy:

# Install Python API for AMPL:
$ python -m pip install amplpy --upgrade

# Install AMPL & solver modules:
$ python -m amplpy.modules install cpsat # install CP-SAT

# Activate your license (e.g., free ampl.com/ce or ampl.com/academia licenses):
$ python -m amplpy.modules activate <your-license-uuid>

How to use:

from amplpy import AMPL
ampl = AMPL()
...
ampl.solve(solver="cpsat", cpsat_options="option1=value1 option2=value2")

Learn more about what we have to offer to implement and deploy Optimization in Python.

AMPL APIs give access to AMPL from a programming language:

ampl: option solver cpsat; # change the solver
ampl: option cpsat_options 'option1=value1 option2=value2'; # specify options
ampl: solve; # solve the problem

Resources#

Features#

Problem types and algorithms#

Problem types

Models with integer variables only; continuous variables are rejected (“CP-SAT supports integer variables only”). Fractional coefficients are accepted. Finite bounds on the variables help the search.

Passed to CP-SAT natively: linear constraints and objectives, alldiff, counting (count, numberof, atmost, atleast, exactly), if-then-else, implications (==>), and/forall, or/exists, min, max, abs, products and integer powers of variables, div and mod. MP reformulates other expressions where possible. Real-valued functions (/, exp, log, …) need continuous variables and are therefore rejected. See the modeling guide and the acceptance options (cpsat -=acc:).

All constructs: native constraints and expressions by driver.

Search

CP-SAT runs a portfolio of search workers in parallel (tech:threads, default 0: automatic). tech:seed changes the random seed, which can change the search noticeably.

ampl: option cpsat_options 'threads=8 timelim=60 outlev=1';
Linear relaxation

alg:linearization sets how much of the model CP-SAT also keeps as a linear relaxation (0 none, 1 some constraints, the default, 2 full), which gives bounds and guides the search. Level 2 can help models that are mostly linear, at a cost per search node.

Search log

tech:outlev=1 prints the CP-SAT log; tech:logfile writes it to a file, whatever tech:outlev is.

Instrumentation#

For a generic summary of the options and features used most often, see Important features in the MP documentation.

For all models

Model export

Option tech:writemodel. Details

Multiple solutions

Option sol:stub. Details

Multiple objectives

Emulated by MP. Option obj:multi. Details

Warm start

Option alg:start. Details

Initial values are passed as CP-SAT solution hints.

For MIP models

MIP gap

Option mip:return_gap. Details

Best dual bound

Option mip:bestbound. Details

Stop on a plateau

Option mip:plateau:time. Details

Stop on a plateau of the MIP gap

Option mip:plateau:relgaptol. Details

Solver options#

Full list of solver options:

More details on solver options: MP solver options.

Retrieving solutions#

The outcome of the last optimization is stored in the AMPL parameter solve_result_num and the relative message in solve_result.

display solve_result_num, solve_result;

CP-SAT solve result codes can be obtained by running cpsat -! or ampl: shell "cpsat -!";:

Solve result table for CP-SAT 9.15.9999
          0- 99 solved: optimal for an optimization problem,
                feasible for a satisfaction problem 
        100-199 solved? solution candidate returned but error likely 
            150 solved? MP solution check failed (option sol:chk:fail) 
        200-299 infeasible 
        300-349 unbounded, feasible solution returned 
        350-399 unbounded, no feasible solution returned 
        400-449 limit, feasible: stopped, e.g., on iterations or Ctrl-C 
        450-469 limit, problem is either infeasible or unbounded.
                Disable dual reductions or run IIS finder for definitive answer.
        470-499 limit, no solution returned 
        500-999 failure, no solution returned 
            550 failure: numeric issue, no feasible solution

For general information, see MP result codes guide.

Changelog#