clojure2minizinc-handdrawn-cut.png

Table of Contents

1 Summary

clojure2minizinc provides an interface between state-of-the-art constraint solvers (via MiniZinc) and a very high-level programming language with excellent abstraction capabilities, Clojure. The clojure2minizinc user models in Clojure constraint satisfaction or optimisation problems over Boolean, integer, real number, and/or set variables. clojure2minizinc translates them into MiniZinc, they are solved in the background by a compatible solver, and the result is read back into Clojure. clojure2minizinc code can be very similar to the corresponding MiniZinc code, but in addition the full power of Clojure is at hand.

2 Usage: A Minimal Example

The following model declares two decision variables a and b, both with the domain [-1 .. 1], and constraints them to be different.

(ns clojure2minizinc.examples
  (:require [clojure2minizinc.core :as mz]))  

(mz/minizinc 
 (mz/clj2mnz
  (let [a (mz/variable (mz/-- -1 1)) 
        b (mz/variable (mz/-- -1 1))]
    (mz/constraint (mz/!= a b))
    (mz/solve :satisfy)
    (mz/output-map {:a a :b b})))
 :num-solutions 3)

The model asks for three solutions, and the default solver outputs the following.

; => ({:a 0, :b -1} {:a 1, :b -1} {:a -1, :b 0})

3 Installation

3.1 Installation of MiniZinc tools

  • Install the G12 MiniZinc Distribution (includes software that translates MiniZinc to the intermediate simpler format FlatZinc, which is understood by 3rd-party solvers, but also ready-to-use solvers)
    • Follow the installation instructions and do not forget to run the install script as described
    • The default clojure2minizinc settings expect executables minizinc and its friends are in your PATH
  • Optionally, install additional solvers. For example, see FlatZinc implementations at http://www.minizinc.org/software.html

3.2 Installation of clojure2minizinc

Add this Leiningen dependency to your project:

[minizinc/clojure2minizinc "0.2.0"]

4 Documentation

5 Related Work

5.1 Clojure: core.logic

In the Clojure community there already exists an interest in Constraint Programming (and the related Logic Programming paradigm), and solvers have been developed for Clojure.

core.logic implements logic programming facilities from scratch directly in Clojure. More specifically, it implements miniKanren. It also implements some of its extensions, e.g., cKanren for Constraint Logic Programming.

By contrast, clojure2minizinc provides an interface to a range of existing state-of-the-art constraint solvers. Unlike core.logic, it does not support Logic Programming (e.g., it does not provide unification of arbitrary terms). However, Constraint Programming is supported in a clearly more mature way by MiniZinc, and clojure2minizinc inherits its capabilities. MiniZinc supports more variable domains than core.logic (Booleans, integers, floats, and set of integers), more constraints (including more global constraints), reified constraints (i.e., the truth value of constraints can in turn be constrained by logic relations such as implication or equivalence), and optimisation support.

Perhaps most importantly, MiniZinc's 3rd-party solvers implement highly efficient search strategies developed by the Constraint Programming community, which clearly outperform core.logic. The MiniZinc to FlatZinc transformation adds a certain overhead, but this overhead is small and can be neglected, in particular for more complex constraint problems. The translation from clojure2minizinc to MiniZinc is so simple that its overhead is even less and can also be neglected.

(TODO: reading results back into Clojure possibly causes another overhead – more careful testing and improving of performance necessary, once clojure2minizinc is somewhat more mature)

5.2 C++: libmzn

A project with similar goals as clojure2minizinc is libmzn, which provides a C++ interface to MiniZinc. It is planned to be released as part of MiniZinc 2.0.

6 Contribute

Most clojure2minizinc functions simply generate a string with the corresponding MiniZinc code. So, this library is very easy to extend to support by and by the full feature set of MiniZinc, and also MiniZinc extensions proposed by various research projects.

Your contribution is welcome!

7 Source

8 License

Distributed under the GNU General Public License.

Copyright © 2014 Torsten Anders

Author: Torsten Anders

Created: 2014-09-30 Tue 20:02

Emacs 24.3.50.2 (Org mode 8.2.7b)

Validate