Zython (minizinc python-wrapper) after year of development
More then a year ago I've started to create python wrapper for minizinc. Which is, probably, the most popular constraint programming tool. You can find more info on what is constraint programming, minizinc, what is it used for in my previous article.
At the release in January 2021 zython supported variables and parameters declaration, arrays, all solving types (satisfy, maximize, minimize), many predefined operations and constraints (and CI of course). It could solve a number of models, some of which were specified in the documentation.
Adding float variable and parameters
But also it lacks some of minizinc features: float and enum types support and sets. I've started with float support. It seems natural and essential for "usual" programming paradigms, e.g. I can't name any popular language without float support (only brainfuck), but in constraint programming it is not so essential, many problems can be solved using integers only, many algorithm were developed only for discreet models. By the fact not every solver support floats (maybe not even the most of them). For example, default zython's solver gecode doesn't fully support them, so it was necessary to add a way the user could specify the solver (of course I've understood it only after I've implemented float variables).
Lets see, how you can use float variable, by solving an easy equation:
import zython as znclass Model(zn.Model): def __init__(self, a, b, c, d, e, f): self.x = zn.var(float) self.constraints = [a ** 5 * self.x + b ** 4 * self.x + c ** 3 * self.x + d ** 2 * self.x + e * self.x + f == 0]m = Model(2, 3, 4, 5, 6, 7)result = m.solve_satisfy(solver="cbc")print(result["x"]) # -0.03365384615384615
Enums and sets
Adding enums and sets was harder task. First of all, because I've started with adding enums, then I've understood, they are quite useless without sets, and start to add them. This task leads to massive refactoring (some of which still should be done), but it leads to a better code, and now everyone can use enums and sets in zython. Below I will provide an example from documentation:
Lets imagine youve should to fight against Mike Tyson (dont worry, you have a week to prepare). You should learn several boxing moves, each of them has strength, but you should invest some time to learn it and some money to hire a coach.
Move | Power | Time to learn | Money to learn |
---|---|---|---|
jab | 1 | 1 | 3 |
cross | 2 | 2 | 3 |
uppercut | 1 | 1 | 3 |
overhand | 2 | 2 | 2 |
hook | 3 | 1 | 1 |
import enumimport zython as znclass Moves(enum.Enum): jab = enum.auto() cross = enum.auto() hook = enum.auto() uppercut = enum.auto() slip = enum.auto()class Model(zn.Model): def __init__( self, moves, time_available, money_available, power, time, money, ): self.time_available = time_available self.money_available = money_available self.power = zn.Array(power) self.time = zn.Array(time) self.money = zn.Array(money) self.to_learn = zn.Set(zn.var(moves)) self.constraints = [ zn.sum(self.to_learn, lambda move: self.time[move]) < self.time_available, zn.sum(self.to_learn, lambda move: self.money[move]) < self.money_available, ]model = Model( Moves, 5, 10, [1, 2, 1, 2, 3], [1, 2, 1, 3, 1], [3, 4, 3, 2, 1],)result = model.solve_maximize( zn.sum(model.to_learn, lambda move: model.power[move]))print(f"Moves to learn: {result['to_learn']}, " f"power: {result['objective']}")
Other changes
The two changes described above, are not the only ones, which was add to zython. I've added support of increasing
, decreasing
and allequal
constraints, except_0
parameter to alldifferent
constraint. New python version was released, so now zython
support cpython 3.7 - 3.10.
And a warning in case minizinc
wasn't found in $PATH
, I hope it will help in installation and integrating zython code.
Conclusion
This year wasn't easy (well not only for me, for everyone), but I've somehow managed to find time to improve zython, which becomes better and better in every version[citation needed]. It is an interesting experience and if you ever thought about starting your own project, you should try.
Original Link: https://dev.to/artyomkaltovich/zython-minizinc-python-wrapper-after-year-of-development-1o71
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To