Metadata-Version: 2.4
Name: pyminizinc
Version: 0.1.0
Summary: MiniZinc for Python with the compiler linked in-process: the minizinc-python API, no minizinc install needed
Keywords: minizinc,constraint programming,optimization,nanobind
Author-Email: Shakeeb Alireza <shakfu@users.noreply.github.com>
License-Expression: MIT AND MPL-2.0
License-File: LICENSE
License-File: thirdparty/chuffed/LICENSE
License-File: thirdparty/gecode/LICENSE
License-File: thirdparty/libminizinc/LICENSE.txt
License-File: thirdparty/minizinc-python/LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: C++
Classifier: Typing :: Typed
Project-URL: Repository, https://github.com/shakfu/pyminizinc
Project-URL: Issues, https://github.com/shakfu/pyminizinc/issues
Project-URL: Changelog, https://github.com/shakfu/pyminizinc/blob/main/CHANGELOG.md
Requires-Python: >=3.12
Provides-Extra: dzn
Requires-Dist: lark>=1.1; extra == "dzn"
Description-Content-Type: text/markdown

# pyminizinc

[MiniZinc](https://www.minizinc.org) 2.11 for Python, with the compiler linked in-process via nanobind. The API is [minizinc-python](https://github.com/MiniZinc/minizinc-python)'s: existing code runs unchanged after switching the import, and no `minizinc` installation is needed. The wheel ships the standard library and the Gecode 6.6 and Chuffed 0.14 solvers.

```python
import pyminizinc as minizinc

model = minizinc.Model()
model.add_string("set of int: S; var S: x; var S: y; constraint x < y; solve maximize x + y;")
model["S"] = {1, 5, 9}

result = minizinc.Instance(minizinc.Solver.lookup("gecode"), model).solve()
result.status            # Status.OPTIMAL_SOLUTION
result["x"], result["y"]  # (5, 9)
result.objective          # 14
```

The package is imported as `pyminizinc`, so it can be installed next to minizinc-python (`import minizinc`). Importing it as `minizinc` keeps existing code unchanged.

Wheels are built for Linux (x86_64, aarch64), macOS (arm64, x86_64) and Windows (x86_64).

See the [minizinc-python documentation](https://python.minizinc.dev/) for `Model`, `Instance`, `Solver`, `solve_async`, `solutions`, `analyse`, `flat` and the rest.

## Compatibility

minizinc-python's own test suite runs against this package (`tests/compat`). Only the test that needs COIN-BC is skipped. `default_driver` is an `InProcessDriver`. `Driver.find()` and `Driver(path)` still drive an external `minizinc` executable.

Behaviour that differs from minizinc-python with a subprocess:

- Runs are serialised: one solve at a time per Python process. Concurrent solves from `asyncio.gather` or threads give correct results, one after another.

- A crash inside libminizinc ends the interpreter. Solvers still run as child processes.

- On Windows, `terminate()` (e.g. breaking out of `solutions()`) stops compilation but not a running solver.

- `default_driver.executable` is the path of the extension module.

Additions: `minizinc.run(args, model)` gives the `minizinc` command line in-process and returns `(code, stdout, stderr)`. `MZN_VERSION` and `STDLIB_DIR` describe the bundled MiniZinc.

The `dzn` extra (`pip install pyminizinc[dzn]`) adds DZN parsing, as in minizinc-python.

## Solvers

**Gecode** is bundled as a static `fzn-gecode` executable and is the default solver. MiniZinc runs it as a child process, so Ctrl-C, `terminate()` and time limits stop it. Gecode is also linked into the compiler, for solution checkers (`.mzc.mzn`) and the `-O3`+ presolver. It is built without MPFR, so float trigonometric, `exp` and logarithm constraints raise `MiniZincError`. Linear and polynomial float constraints, `sqrt`, and integer `pow` work. See `docs/dev/mpfr-gecode.md`.

**Chuffed** is bundled the same way, as a static `fzn-chuffed`. It is a lazy clause generation solver and has no float variables. It is not linked into the compiler: it keeps process-global state and exits the process on some failures, so it runs only as a child process.

Other solvers are found the same way the `minizinc` executable finds them:

- **FlatZinc executables** (OR-Tools, ...) via `.msc` files on the solver search path. That path includes `MZN_SOLVER_PATH` and the usual system locations, e.g. Homebrew's `share/minizinc/solvers`. A same-version Gecode or Chuffed in a system location is ignored, because the bundled config is found first. One in `MZN_SOLVER_PATH` or `~/.minizinc/solvers` is found before it and wins. Setting `MZN_STDLIB_DIR` to another install also drops the bundled solver.

- **MIP libraries** (HiGHS, CPLEX, Gurobi, SCIP, Xpress) are loaded with `dlopen` at first use. If the library is not on the loader path, pass it as `solve(**{"--highs-dll": path})`. These run inside the process and cannot be interrupted.

## Development

```bash
make build   # compile the extension (first build fetches tree-sitter and Gecode)
make test
make qa      # lint, format check, mypy, tests
```

Design notes are in `docs/dev/`:

- `minizinc-python.md`: upstream provenance, the in-process driver, syncing.

- `libminizinc-patches.md`: local libminizinc changes.

## License

- MIT for this package's own code.

- MPL-2.0 for the minizinc-python modules (`thirdparty/minizinc-python/LICENSE`).

- MPL-2.0 for libminizinc and its standard library, which the wheel links and bundles (`thirdparty/libminizinc/LICENSE.txt`).

- MIT for the bundled Gecode (`thirdparty/gecode/LICENSE`) and Chuffed (`thirdparty/chuffed/LICENSE`).
