Skip to main content

Python bindings to the Choco Constraint Programming solver

Project description

pychoco

ubuntu_build macos_build windows_build codecov PyPI version Documentation Status PyPI Downloads License DOI

Current choco-solver version: 4.10.18

Python bindings for the Choco Constraint programming solver (https://choco-solver.org/).

Choco-solver is an open-source Java library for Constraint Programming (see https://choco-solver.org/). It comes with many features such as various types of variables, various state-of-the-art constraint, various search strategies, etc.

The pychoco library uses a native-build of the original Java Choco-solver library, in the form of a shared library, which means that it can be used without any JVM. This native-build is created with GraalVM native-image tool.

We heavily relied on JGraphT Python bindings source code to understand how such a thing could be achieved, so many thanks to JGraphT authors!

Installation

We automatically build 64-bit wheels for Python versions >= 3.6 on Linux, Windows and MacOSX. They can be directly downloaded from PyPI (https://pypi.org/project/pychoco/) or using pip:

pip install pychoco

Documentation

If you do not have any knowledge about Constraint Programming (CP) and Choco-solver, you can have a look at https://choco-solver.org/tutos/ for a quick introduction to CP and to Choco-solver features. The tutorial in this website includes both Java and Python examples. For this Python API, we also provide an API documentation which is available online at https://pychoco.readthedocs.io/.

You can also have a look at the pychoco Cheat Sheet : pychoco cheat sheet

Finally, we designed a few notebooks examples that you can find in the examples directory.

Quickstart

pychoco's API is quite close to Choco's Java API. The first thing to do is to import the library and create a model object:

from pychoco import Model

model = Model("My Choco Model")

Then, you can use this model object to create variables:

intvars = model.intvars(10, 0, 10)
sum_var = model.intvar(0, 100)

You can also create views from this Model object:

b6 = model.int_ge_view(intvars[6], 6)

Create and post (or reify) constraints:

model.all_different(intvars).post()
model.sum(intvars, "=", sum_var).post()
b7 = model.arithm(intvars[7], ">=", 7).reify()

Solve your problem:

model.get_solver().solve()

And retrieve the solution:

print("intvars = {}".format([i.get_value() for i in intvars]))
print("sum = {}".format(sum_var.get_value()))
print("intvar[6] >= 6 ? {}".format(b6.get_value()))
print("intvar[7] >= 7 ? {}".format(b7.get_value()))
> intvars = [3, 5, 9, 6, 7, 2, 0, 1, 4, 8]
> sum = 45
> intvar[6] >= 6 ? False
> intvar[7] >= 7 ? False

Configuring search

Generic search strategies

Currently, the main limitation of pychoco is the customization of search strategies, which is not as advanced as the Java version. This is mainly due to the fact that pychoco's need to rely on a compiled C entrypoint to Choco-solver, which does not allow Python routines to be injected into the solving procedure. One possible solution would be to implement a parsing system to define custom search strategy in Choco-solver, and rely on this system in pychoco. However, this represents a considerable amount of work that we cannot commit to in the short term. Note: please do not hesitate to let us know, or open a pull request if you want to implement this feature, or suggest an alternative solution.

However, it is possible to rely on the generic search heuristics available in Choco-solver, through the Solver object. Currently available search strategies are: default_search, dom_over_w_deg_search, dom_over_w_deg_ref_search, activity_based_search, min_dom_lb_search, min_dom_ub_search, random_search, conflict_history_search, input_order_lb_search, input_order_ub_search, failure_length_based_search, failure_rate_based_search, pick_on_dom_search, pick_on_fil_search.

Example:

solver.set_dom_over_w_deg_search(decision_variables)

Hints

Hints can improve the search procedure by defining a partial solution and drive the search toward a solution. Hints apply on integer variables, and consist of couples of (variable, value).

Example:

solver.add_hint(cost, min_cost)

Parallel portfolio

The parallel portfolio is a powerful feature of Choco-solver which allows to solve a problem in parallel with different search strategies. Each solving thread can inform other when he finds a solution, leading them to update their bounds in case of an optimization process. To set up a parallel portfolio, it is necessary to construct as many identical models as the number of threads. This feature can very efficient to boost the optimization procedure.

Example:

from pychoco.model import Model
from pychoco.parallel_portfolio import ParallelPortfolio

pf = ParallelPortfolio()
pf.steal_nogoods_on_restarts()
for i in range(0, 5):
    m = Model()
    vars = m.intvars(10, 0, 100)
    nv = m.intvar(3, 4)
    m.n_values(vars, nv).post()
    s = m.intvar(0, 1000)
    m.sum(vars, "=", s).post()
    m.set_objective(s, True)
    pf.add_model(m)
sol = pf.find_best_solution()

Build from source

The following system dependencies are required to build pychoco from sources:

Once these dependencies are satisfied, clone the current repository:

git clone --recurse-submodules https://github.com/chocoteam/pychoco.git

The --recurse-submodules is necessary as the choco-solver-capi is a separate git project included as a submodule (see https://github.com/chocoteam/choco-solver-capi). It contains all the necessary to compile Choco-solver as a shared native library using GraalVM native-image.

Ensure that the $JAVA_HOME environment variable is pointing to GraalVM, and from the cloned repository execute the following command:

sh build.sh

This command will compile Choco-solver into a shared native library and compile the Python bindings to this native API using SWIG.

Finally, run:

pip install .

And voilà !

Citation

Justeau-Allaire D, Prud’homme C (2025). pychoco: all-inclusive Python bindings for the Choco-solver constraint programming library. Journal of Open Source Software, 10(113), 8847, https://doi.org/10.21105/joss.08847

Getting help or contribute

We do our best to maintain pychoco and keep it up-to-date with choco-solver. However, if you see missing features, if you have any questions about using the library, suggestions for improvements, or if you detect a bug, please open an issue.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

pychoco-0.3.0-cp314-cp314t-win_amd64.whl (21.3 MB view details)

Uploaded CPython 3.14tWindows x86-64

pychoco-0.3.0-cp314-cp314t-manylinux_2_34_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.34+ x86-64

pychoco-0.3.0-cp314-cp314t-macosx_15_0_arm64.whl (19.1 MB view details)

Uploaded CPython 3.14tmacOS 15.0+ ARM64

pychoco-0.3.0-cp314-cp314-win_amd64.whl (21.3 MB view details)

Uploaded CPython 3.14Windows x86-64

pychoco-0.3.0-cp314-cp314-manylinux_2_34_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

pychoco-0.3.0-cp314-cp314-macosx_15_0_arm64.whl (19.1 MB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

pychoco-0.3.0-cp313-cp313-win_amd64.whl (20.8 MB view details)

Uploaded CPython 3.13Windows x86-64

pychoco-0.3.0-cp313-cp313-manylinux_2_34_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

pychoco-0.3.0-cp313-cp313-macosx_15_0_arm64.whl (19.1 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

pychoco-0.3.0-cp312-cp312-win_amd64.whl (20.8 MB view details)

Uploaded CPython 3.12Windows x86-64

pychoco-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

pychoco-0.3.0-cp312-cp312-macosx_15_0_arm64.whl (19.1 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

pychoco-0.3.0-cp311-cp311-win_amd64.whl (20.8 MB view details)

Uploaded CPython 3.11Windows x86-64

pychoco-0.3.0-cp311-cp311-manylinux_2_34_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

pychoco-0.3.0-cp311-cp311-macosx_15_0_arm64.whl (19.1 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

pychoco-0.3.0-cp310-cp310-win_amd64.whl (20.8 MB view details)

Uploaded CPython 3.10Windows x86-64

pychoco-0.3.0-cp310-cp310-manylinux_2_34_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

pychoco-0.3.0-cp310-cp310-macosx_15_0_arm64.whl (19.1 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file pychoco-0.3.0-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: pychoco-0.3.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 21.3 MB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychoco-0.3.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 85314727aeeb15fd50bd1d1f2e81cdf81d16373c6b5cd8ff326802074d256a3f
MD5 9c0e6eda3820164b59b8af263d9b6b0c
BLAKE2b-256 ec988aace5e918880301939735b21ff10c8e864e3017d0f93bf8ddaeef06490a

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp314-cp314t-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp314-cp314t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a872f5965a2d4897f2dae26bdbb5ca3666a3fc7f42b422b808a20e9e13a8840a
MD5 d21ece5f0cb54766e76301a9a5ff020d
BLAKE2b-256 443801e301eaacd45f05bfd1cda9af71a262dcf5922f5c3cfe46e78da1631afd

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp314-cp314t-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp314-cp314t-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 33bc410b665d08bb44cfa58b897554212218624a919f1a44cb98b505e4f0fe73
MD5 38f0396ddb04ef0043fa895233b3eafe
BLAKE2b-256 5627a400f3767d936a69a1fdc3ca492f4cedf896d2a65de706f3114ab0f7b99c

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: pychoco-0.3.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 21.3 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychoco-0.3.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 3ff76cf46ef0145583113b4a8103406621dc2327fb31d9375ac0fa3ca0994db8
MD5 30345b5725dd306bc45a5049466e7818
BLAKE2b-256 4bbdcf2cb6d692a20f744de7d5b86b2c2e459196b9cef6cc2c23804c2288742a

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 ea3b5fb875bfcd671e6e49dad0ebf47ef818dbe71316799108cce0303a8c1afa
MD5 a8059ddb5a342db862f8f2d015be09b3
BLAKE2b-256 aa8029484ae0135df9c2770d13aaa66cb16eed74c0e781be19e72e7b3c3dbf84

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 67d4113b46fedf284a8fb796df6e17502837171d9f00008fdfa073ddebccb6a4
MD5 5166e8c9216773c704982fde2dce58fe
BLAKE2b-256 1626df1d93dcba0cdf5448734bd8fd65123ed8b7523f67c7cbe16f833d93e9c0

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: pychoco-0.3.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 20.8 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychoco-0.3.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7ae5b3a31a8bf46960abaf7b09d2d86bd696ddb4897abed74129346a8818f2a0
MD5 4cc8ca22b28dcc571f6bce4e2a42f0cd
BLAKE2b-256 a4cb520a0144f7a255ea63aaf17216d3e686b3ebaac55e5296ce226b4368bfd7

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 24f3a4248d6e079c41b8fbc1abd7851a838f0615d0625dd49a3470bb05648181
MD5 9899a730e07e1ff47c1992a9d3222683
BLAKE2b-256 c5201b57d93d16b8d6e6fc3a18eb5082a07fff47c4007e6cf848c1ff477c999d

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 6a127862743e13ac9c31045225c5c071cd3d96ec47219073169502b06b72c957
MD5 9aae9e57dac85defb152187e3c876667
BLAKE2b-256 2b63af4ceac95f8db6d4744a68ab1a8f864ce470eb04ff43edd8e0cab0df06e2

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pychoco-0.3.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 20.8 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychoco-0.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 df6b2f7e943cc0b6ae0787deb594a385b93ff765e41c5153bf5a9bb8f41c3efe
MD5 bab08992ba83d2b30d076ebbec7129a1
BLAKE2b-256 f09570ef8d27c2c7d4394b05b2bfcf2539b682e3a8d6a57dd8bdc54e06c66e43

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e6c5edf98b05c846d55b776155d7a7b9c91cab4f2c3c4203fe6df0bafd977bdc
MD5 0048614c971e091bef12aaf106209e5e
BLAKE2b-256 f746556bbc9cda5c4081978c00e3db34a9814cff6e58c07db63d2b11641e55a7

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 63867a04279d50bb75cdf14daf28b0c34c3596f3475dad362f3ef63af84f2807
MD5 30f2059bbb7bcabdfbdecc73351e8bc1
BLAKE2b-256 db148eab2ed05bc5ca200362e7d131368c32154815554e98635661fe9c8a6746

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pychoco-0.3.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 20.8 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychoco-0.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0d19fa458400b4cf8a9af3cd679bf6a4935bd7d7e7117139106ba7cd2bb9899a
MD5 9f903d2abad632c79b68fa6760a9fdb6
BLAKE2b-256 49e5b6de75fc3f44a445a643b0d28659f9478fd32c6040f09fa1fad4c5d1f948

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4b5619efbe8eb18f65e50583b96700eed3eddf6f3d7a50093e51d484b0d4e322
MD5 096f0bd175844b752da25773ccd8b7c7
BLAKE2b-256 febfe0b7a94119a649b5d898203b766adaaa96e711447fff34452463f5737ca5

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 289dd7617192f1c1098b52704e6fc4e6d2e3aab44db05519fcf1a4e82be8a0f2
MD5 4e00e4c25a32e0f77fb465e38d2fd798
BLAKE2b-256 0f1b9e05f6dbfe8bda19f7c8bb0337f52353d47125d240e0ef1dc93d545bede6

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pychoco-0.3.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 20.8 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pychoco-0.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 360157ee8cb2607fa8a3a8383bb28fecbf506fa98a331d5bf7c1edf26ffc81c7
MD5 dfb1452b73366a0ac7195a97b033b569
BLAKE2b-256 f155f0a9c039f97e124b866c97df717962cda2f77dd2b39d0b6235f6bea69193

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4c5efe2f7e25882cb5f8c0a0a4e6fc9da1b0cd3fcc47ca2ca0fb14d877d0b741
MD5 b6bd5ea4f7f3b43fe4ab857c76cf48be
BLAKE2b-256 39431cdc072d387491ab58872440379ecda84abd36df64665f533541e4e9d1eb

See more details on using hashes here.

File details

Details for the file pychoco-0.3.0-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pychoco-0.3.0-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 473c2644e9259d323aa85d4e75ee0e0fb6506a84bfc4e61a80beaeaf5d9e4a66
MD5 07a6dd3a7fde9102f3d7a0dfc6d9bdfb
BLAKE2b-256 02c0af6e740a80107e48f948a1cbec32537446ce47f794d44859cb7d9c95dc80

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