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.

59 lines
1.6 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. print(solver.sexpr())
  29. # Check for satisfiability
  30. res = solver.check()
  31. if res != sat:
  32. print("unsat")
  33. sys.exit(1)
  34. ################################################################################
  35. # print the model
  36. # get the model
  37. m = solver.model()
  38. # print all Z3 variables which have an assignment in the model
  39. result = str(m[cows]) + " cows and "
  40. result += str(m[chicken]) + " chicken and "
  41. result += str(m[mice]) + " mice -> "
  42. print(result)
  43. print(m[cows].as_long() * 10 + m[chicken].as_long() * 3 + m[mice].as_long() * 0.25)
  44. solver.add(mice != m[mice])
  45. res = solver.check()
  46. if res != sat:
  47. print("unsat")
  48. sys.exit(1)
  49. ################################################################################