I'm trying to use z3py as an optimization solver to maximise the volume of a cuboid cut out from a piece of paper. The python API provides the Optimize() object but using it seems to be unreliable, giving me solutions that are obviously inaccurate.
I've tried using h = opt.maximise
followed by opt.upper(h)
as well as simply checking the model, as well as defining the volume of the cuboid before adding it to the model v = w*b*l
and after, as well as setting the objective as w*b*l
instead of v
. None of them have given me anything resembling a good solution.
from z3 import *
l = Real("l")
w = Real("w")
b = Real("b")
v = Real("v")
opt = Optimize()
width = 63.6
height = 51
opt.add(b+l <= width)
opt.add(w+b+w+l+w <= height)
opt.add(w > 0)
opt.add(b > 0)
opt.add(l > 0)
opt.add(v == w*b*l)
opt.maximize(w * b * l)
# h = opt.maximize(v)
print(opt.check())
# print(opt.upper(h))
print(opt.model())
Outputs:
unknown
[w = 1, b = 1, l = 47, v = 47]
This definitely is not the maximum. Setting all values to 10 gives a greater solution that fulfills the constraints.