Skip to main content

python-minizinc-maker

Create pure Minizinc .mzn files from Python using python-minizinc-maker and minizinc-python.

Model

Model: model = pymzm.Model()
Variable: x = model.add_variable(name, vtype, val_min, val_max)
Variables: xs = model.add_variables(name, indicies, vtype, val_min, val_max)
Constraint: model.add_constraint(expr)
Solve: model.set_solve_criteria(solve_criteria, expr)

Global constraints

alldifferent, among, all_equal, count, increasing, decreasing, ..(more to be added later)..

Solve

  • model.set_solve_criteria(pymzm.SOLVE_MAXIMIZE, pymzm.Expression.sum(xs))
  • model.set_solve_criteria(pymzm.SOLVE_SATISFY)

Examples

intfact.py - Integer Factorization example

intfact.py

import pymzm
model = pymzm.Model()

x = model.add_variable("x", val_min=1, val_max=99999999)
y = model.add_variable("y", val_min=1, val_max=99999999)

model.add_constraint(x * y == 7829 * 6907)
model.add_constraint(y > 1)
model.add_constraint(x > y)

model.set_solve_criteria(pymzm.SOLVE_SATISFY)
model.generate()
model.write("model.mzn")
...

model.mzn

var 1..99999999: x;
var 1..99999999: y;
constraint x * y = 54074903;
constraint y > 1;
constraint x > y;
solve satisfy;

Now you can use the minizinc library to solve the model directly.

intfact.py

...
import minizinc
gecode = minizinc.Solver.lookup("gecode")
result = minizinc.Instance(gecode, model).solve(all_solutions=True)
print(f"x = {result[0].x}")  # x = 7829
print(f"y = {result[0].y}")  # y = 6907

bibd.py - Balanced Incomplete Block Design

https://www.csplib.org/Problems/prob028/

v = 7; b = 7; r = 3; k = 3; l = 1

model = pymzm.Model()

indicies = [(i, j) for i in range(v) for j in range(b)]
# bool if object v is in block b
xs = model.add_variables("x", indices=indicies, vtype=pymzm.Variable.VTYPE_BOOL)

for i in range(b):
    model.add_constraint(pymzm.Expression.sum(xs[i, j] for j in range(v)) == r)
for i in range(v):
    model.add_constraint(pymzm.Expression.sum(xs[j, i] for j in range(b)) == k)

for i in range(b):
    for j in range(i):
        model.add_constraint(pymzm.Expression.sum(xs[i, k] * xs[j, k] for k in range(v)) == l)

model.set_solve_criteria(pymzm.SOLVE_SATISFY)
model.generate()

gecode = minizinc.Solver.lookup("gecode")
result = minizinc.Instance(gecode, model).solve(all_solutions=False)

for i in range(v):
    print(" ".join([str(int(result[f"x_{i}_{j}"])) for j in range(b)]))

outputs

0 0 1 0 0 1 1
0 1 0 0 1 1 0
1 0 0 1 0 1 0
0 0 1 1 1 0 0
0 1 0 1 0 0 1
1 1 1 0 0 0 0
1 0 0 0 1 0 1

sat.py - Satisfiability

model = pymzm.Model()

xs = model.add_variables("x", indices=range(4), vtype=pymzm.Variable.VTYPE_BOOL)
model.add_constraint(pymzm.Expression.AND([
    xs[0] | xs[1] | ~xs[2],
    xs[1] | xs[2] | ~xs[3],
    xs[0] | ~xs[1] | xs[3],
]))
model.set_solve_criteria(pymzm.SOLVE_SATISFY)
model.generate()

gecode = minizinc.Solver.lookup("gecode")
result = minizinc.Instance(gecode, model).solve(all_solutions=False)

for i in range(4):
    print(f"x_{i} = {result[f'x_{i}']}")

Metadata

Release files for pymzm 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymzm 0.0.1
File Size Uploaded Provenance Actions
pymzm-0.0.1.tar.gz 14.3 kB Without provenance Details for pymzm-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pymzm 0.0.1
File Interpreter ABI Platform Actions
pymzm-0.0.1-py3-none-any.whl Python 3 none any Details for pymzm-0.0.1-py3-none-any.whl

Total release size: 26.1 kB

Release files / pymzm-0.0.1.tar.gz

Download URL pymzm-0.0.1.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
692d44af37309fd864ed5d52ba536ef4f03ff80cdfbc85308e5360e0cccf9434
BLAKE2b-256 checksum
How to use checksums
210ea96ee16922ce857c805bd82e773db4b61d7614079fb59763f2f0ebe4006d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release files / pymzm-0.0.1-py3-none-any.whl

Download URL pymzm-0.0.1-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7eb7e31460a9ed39de2841297aede63de9f470504305f4cbefe153ad284e582
BLAKE2b-256 checksum
How to use checksums
b25790b9549f8bda2205aa31e94c4c42edff6e8ebaa644367407c90a8888aa6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Security for pymzm 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page