You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

  1. # coding: utf-8
  2. import os, sys
  3. from z3 import *
  4. # No need to parse anything for this example
  5. #################################### Farm Animals ####################################
  6. solver = Solver()
  7. # todo
  8. # Create Z3 variables which hold the number of animals and the price respectively
  9. amount_of_animals = Int("amount_of_animals")
  10. total_price = Int("total_price")
  11. target_price = 10000
  12. target_amount = 100
  13. # todo
  14. # Create Z3 variables which hold the amount of the different animals our farmes is going to buy
  15. cows = Int("cows")
  16. mice = Int("mice")
  17. chicken = Int("chicken")
  18. # todo
  19. # Add constraints which model our real life requirements
  20. solver.add(cows + chicken + mice == amount_of_animals)
  21. solver.add(cows * 1000 + chicken * 300 + mice * 25 == total_price)
  22. solver.add(total_price == target_price)
  23. solver.add(amount_of_animals == target_amount)
  24. # Done?
  25. solver.add(cows > 1)
  26. solver.add(chicken > 1)
  27. solver.add(mice > 1)
  28. # Check for satisfiability
  29. res = solver.check()
  30. if res != sat:
  31. print("unsat")
  32. sys.exit(1)
  33. ################################################################################
  34. # print the model
  35. # get the model
  36. m = solver.model()
  37. # print all Z3 variables which have an assignment in the model
  38. for d in m.decls():
  39. print("%s = %s" % (d.name(), m[d]))
  40. ################################################################################