A dumb introduction to z3

(asibahi.github.io)

153 points | by kfl 1 day ago

7 comments

  • cube2222 7 hours ago
    I’d be curious if someone here could explain the initial nonsensical answer for the coin change problem.

    I understand that not giving the lower bounds effectively lets it find an arbitrarily low (generally very negative) number of coins that satisfy the problem (so the minimization would basically go to negative infinity).

    But why would it respond with “to get 37 in the lowest number of coins I’ll give you 37 ones”?

    Or is this kind of like “if the answer to your minimization problem is negative infinity, the answer is undefined behavior”?

    • gopalv 6 hours ago
      > is this kind of like “if the answer to your minimization problem is negative infinity, the answer is undefined behavior”?

      Yes, it picked a valid result and gave up.

      I got a similar nonsensical result in my run-ins[1] in with SAT solvers too, until I added a lowerbound=0.

      The "real life" follow-up question in interviews was how to minimize the total number of intermediate rows for a series of joins within in a cost-based optimizer.

      [1] - https://gist.github.com/t3rmin4t0r/44d8e09e17495d1c24908fc0f...

    • zdance 5 hours ago
      37 is irreducible in the problem statement, so the answer is 37.

      Think about it purely in the set-theoretic sense "what is the minimal set containing 37 elements?" the answer is "the set containing 37 elements."

      • Dylan16807 4 hours ago
        The fixed 37 is the value of the coins. It's very easy to reduce the number of coins.

        Either you misunderstood something or please explain more. Note that both the working and broken versions have the same 37 in them.

        And the problem statement starts with no coins chosen. It had to actively choose pennies to get that broken result. If you told it about the coins in a different order, it probably would have given a different answer.

  • joek1301 5 hours ago
    I also was inspired to play around with Z3 after reading a Hillel Wayne article.

    I used it to solve the new NYT game, Pips: https://kerrigan.dev/blog/nyt-pips

    • QuadmasterXLII 42 minutes ago
      I guess z3 is fine with it, but it confuses me that they decided pips wouldn't have unique solutions
  • adsharma 35 minutes ago
    Those of you wondering about how to use z3, please consider coding in static python (not z3py) and then transpile to smt2.

    You'll be able to solve bigger problems with familiar/legible syntax and even use the code in production.

  • throwaway81523 7 hours ago
    • pkoird 6 hours ago
      imo this is the pdf that many people like me used to learn SAT/SMT.
  • th1nhng0 1 hour ago
    I have read that article too and very interest in the solver
  • sophacles 7 hours ago
    Constraint solvers are a cool bit of magic. The article underplays how hard it is to model problems for them, but when you do have a problem that you can shape into a sat problem... it feels like cheating.
    • ngruhn 6 hours ago
      I took a course on SMT solvers in uni. It's so cool! They're densely packed with all these diverse and clever algorithms. And there is still this classic engineering aspect: how to wire everything up, make it modular...
    • pkoird 6 hours ago
      Going one abstraction deeper, SAT solvers are black magic.
      • metadat 4 hours ago
        Yes, explaining the "why / how did the SAT solver produce this answer?" can be more challenging than explaining some machine learning model outputs.

        You can literally watch as the excitement and faith of the execs happens when the issue of explainability arises, as blaming the solver is not sufficient to save their own hides. I've seen it hit a dead end at multiple $bigcos this way.

    • almostgotcaught 2 hours ago
      The solution is to look at a lot of examples

      https://www.hakank.org/

    • mikestorrent 6 hours ago
      If you're good at doing this, you should check out the D-Wave constrained quadratic model solvers - very exciting stuff in terms of the quality of solution it can get in a very short runtime on big problems.
  • forrestthewoods 2 hours ago
    This is great. So many times I’ve wondered how to actually use z3 and I couldn’t figure it out. Thank you!