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
45 lines
1.5 KiB
\item \lect Complete the following python script with the necessary statements.
|
|
|
|
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.
|
|
% You need to declare and populate a z3 \texttt{Datatype}
|
|
% Furthermore, you need to create a list of five distinct integers and bound them.
|
|
% Finally, enforce the constraints on the uninterpreted function ....
|
|
|
|
\begin{pythonSourceCode}
|
|
from itertools import combinations
|
|
from z3 import *
|
|
|
|
solver = Solver()
|
|
|
|
# Declare a Datatype and populate it with 4 colours.
|
|
Colours = Datatype("Colours")
|
|
Colours.declare("RED")
|
|
Colours.declare("BLUE")
|
|
Colours.declare("GREEN")
|
|
Colours.declare("YELLOW")
|
|
|
|
Colour = Colours.create()
|
|
# Declare a function from integers to your custom datatype
|
|
f = Function('f', IntSort(), Colour)
|
|
|
|
variables = list()
|
|
for i in range(0,5):
|
|
variables.append(Int(i))
|
|
# Bound each integer i such that 0 <= i < 5
|
|
|
|
solver.add(0 <= variables[-1])
|
|
solver.add(variables[-1] <= 5)
|
|
|
|
# Enfore that all i are distinct
|
|
solver.add(Distinct(variables))
|
|
|
|
|
|
# Enforce that colours are different for adjacent integers
|
|
for combi in combinations(variables,2):
|
|
solver.add(Implies(Abs(combi[0] - combi[1]) == 1, f(combi[0]) != f(combi[1])))
|
|
|
|
|
|
result = solver.check()
|
|
if result == sat:
|
|
print(solver.model())
|
|
\end{pythonSourceCode}
|