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.

45 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. # Check for satisfiability
  26. res = solver.check()
  27. if res != sat:
  28. print("unsat")
  29. sys.exit(1)
  30. ################################################################################
  31. # print the model
  32. # get the model
  33. m = solver.model()
  34. # print all Z3 variables which have an assignment in the model
  35. for d in m.decls():
  36. print("%s = %s" % (d.name(), m[d]))
  37. ################################################################################