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.5 KiB

7 months ago
  1. \item \lect Complete the following python script with the necessary statements.
  2. The final script should map each integer of a list of five integers to four possible colours such that the mapping returns different colours for adjacent integers.
  3. % You need to declare and populate a z3 \texttt{Datatype}
  4. % Furthermore, you need to create a list of five distinct integers and bound them.
  5. % Finally, enforce the constraints on the uninterpreted function ....
  6. \begin{pythonSourceCode}
  7. from itertools import combinations
  8. from z3 import *
  9. solver = Solver()
  10. # Declare a Datatype and populate it with 4 colours.
  11. Colours = Datatype("Colours")
  12. Colours.declare("RED")
  13. Colours.declare("BLUE")
  14. Colours.declare("GREEN")
  15. Colours.declare("YELLOW")
  16. Colour = Colours.create()
  17. # Declare a function from integers to your custom datatype
  18. f = Function('f', IntSort(), Colour)
  19. variables = list()
  20. for i in range(0,5):
  21. variables.append(Int(i))
  22. # Bound each integer i such that 0 <= i < 5
  23. solver.add(0 <= variables[-1])
  24. solver.add(variables[-1] <= 5)
  25. # Enfore that all i are distinct
  26. solver.add(Distinct(variables))
  27. # Enforce that colours are different for adjacent integers
  28. for combi in combinations(variables,2):
  29. solver.add(Implies(Abs(combi[0] - combi[1]) == 1, f(combi[0]) != f(combi[1])))
  30. result = solver.check()
  31. if result == sat:
  32. print(solver.model())
  33. \end{pythonSourceCode}