-1

I'm running into a situation where I'd really love a copy feature for the Z3 Solver. By this I mean, I have a solver instantiated with some constraints. I now want to copy it so that I have two independent solvers. At the moment, I'm doing this by creating a new solver and just iterating over s.assertions and adding them back in. For small solvers this is fine. For larger solvers, this can severely impact the time make a copy as Z3 is re-creating work it has already done.

While this isn't a show stopper, it would be very beneficial to be able to copy the solver directly. The normal deepcopy method throws an error about not being able to deepcopy ctypes (which makes sense), so I'm guessing any better solution would have to be implemented by z3 or z3py proper.

Anyone know of a more efficient way to copy the solver as stated and not incur the overhead of Z3 re-solving what it already knows?

Owl Owl
  • 181
  • 1
  • 10

1 Answers1

1

If you build the latest source of Z3, Solver objects have a translate method that takes a new context as the parameter (it can be the same context), and creates a copy of the solver in that context.

s = Solver()
...add some assertions...
solver2 = s.translate(main_ctx()) # create a copy in the same context
solver3 = s.translate(ctx) # create a copy in some other context
dlshriver
  • 156
  • 1
  • 15