Skip to main content

Express constraint programming problem with python and solve it with minizinc

Project description

zython

zython intro image Express constraint programming problem with python and solve it with minizinc.

Constraint programming (CP) is a paradigm for solving combinatorial problems. Minizinc is used for model and optimization problems solving using CP. You can express a model as a number of parameter, variables and constraints - minizinc will solve it (or said it if there isn't any solution).

If you are wonder which digit should be assigned to letters, so the expression SEND+MORE=MONEY will be hold, or how many color you should have to brush map of Australia and two states with the same border won't have any common color, or try to understand which units you should hire in your favourite strategy game, so you will have the strongest army for that amount of money you can use CP.

Zython lets you express such model with pure python, so there is no need to learn a new language, and you can easily integrate CP into your python programs.

Getting Started

Prerequisites

  • You should have minizinc >=2.5 install and have it executable in $PATH. You can download it from official site.
  • Python 3.7+

Installation

pip install zython

Usage

Our first example will be quadratic equation solving.

It can be expressed in minizinc as:

var -100..100: x;
int: a; int: b; int: c;
constraint a*(x*x) + b*x = c;
solve satisfy;

or using minizinc-python package as

import minizinc

# Create a MiniZinc model
model = minizinc.Model()
model.add_string("""
var -100..100: x;
int: a; int: b; int: c;
constraint a*(x*x) + b*x = c;
solve satisfy;
""")

# Transform Model into a instance
gecode = minizinc.Solver.lookup("gecode")
inst = minizinc.Instance(gecode, model)
inst["a"] = 1
inst["b"] = 4
inst["c"] = 0

# Solve the instance
result = inst.solve(all_solutions=True)
for i in range(len(result)):
    print("x = {}".format(result[i, "x"]))

While zython makes it possible to describe this model using python only:

class MyModel(zython.Model):
    def __init__(self, a: int, b: int, c: int):
        self.a = var(a)
        self.b = var(b)
        self.c = var(c)
        self.x = var(range(-100, 101))
        self.constraints = [self.a * self.x ** 2 + self.b * self.x + self.c == 0]

model = MyModel(1, 4, 0)
result = model.solve_satisfy(all_solutions=True)

Collaboration

Zython uses the following libraries:

  • Test is created with pytest library
  • nox for test execution
  • flake8 for coding style checking
  • sphinx for documentation

Requirements necessary for zython run specified in requirements.txt file, while testing, development requirements are specified in requirements_dev.txt, and documentation requirements are in requirements_doc.txt. For example, if you decided to fix bug, and you need no documentation fixes, you shouldn't install requirements_doc.txt. Project can be cloned from github and all dependencies can be installed via pip.

git clone git@github.com:ArtyomKaltovich/zython.git
python -m venv /path/to/new/venv if needed
pip install -r requirements.txt
pip install -r requirements_dev.txt

Note: flake8 isn't specified as dependency in any file, this is made for avoiding it installation for every python version tested in CI. You should install it manually if you want to check your code before submitting PR. You can do it with the following commands:

pip install flake8
nox -s lint

You can also run all checks locally:

nox --reuse-existing-virtualenvs

It is recommended to open new issue and describe a bug or feature request before submitting PR. While implementing new feature or fixing bug it is necessary to add tests to cover it.

Good Luck and thank you for improvements. :)

Project details


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 Distribution

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

zython-0.2.4-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file zython-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: zython-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 29.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for zython-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 12c4a566c58a20886147e72833919b3c495a0e9c9ccc337246682a1dc6feeb1f
MD5 9dbd1f560906ec19cad6865e87c00f46
BLAKE2b-256 d8b82b95703d2d9aea5e11ab6b6a371b0fcfb16cf63b38ab6f1870a9f92dc766

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