关键词 > Python代写

Logic Coursework Computational Thinking 2020/21

发布时间:2021-04-26

Computational Thinking 2020/21

Logic Coursework


Barnaby Martin


You should submit a single ZIP file containing (i) one PDF document containing your answers to all the theoretical/mathematical questions, and (ii) a single Python file as with your code. Please name the Python file according to your username (e.g. mpll19.py).

The coding part of the coursework will be to write a SAT-solver in Python. Note that you will be restricted in some of your choices for data structures and function names. The data structure for literal will be an integer, where a negative integer indicates the negation of the variable denoted by the corresponding positive integer. The data structure for partial assignment should be a list of literals. The data structure for a clause set should be a list of lists of literals.

1. Answer the following questions about complete sets of logical connectives, in each case justify-ing your answer. [9 marks]

(i). Show {¬, ∨, ∧} is a complete set of connectives.

(ii). Is {¬, →} a complete set of connectives?

(iii). Is {∨, ∧} a complete set of connectives?

(iv). {→} is not a complete set of connectives. With which constant can it be made complete?

2. Answer the following question about Conjunctive Normal Form (CNF), in each case justifying your answer. [11 marks]

(i). Show that any formula may be rewritten to an equivalent formula in CNF.

(ii). Is there a polynomial p so that a general formula of size n can be rewritten to an equivalent formula in CNF of size at most p(n)?

(iii). What if we change equivalent to equisatisfiable in the previous question?

(iv). Use Tseitin’s algorithm to convert ((p ∨ (q ∧ r)) → ((x ∧ y) ∨ (u ∧ v))) to CNF.

3. Write some Python code that loads a textual file in DIMACS format into an internal represen-tation of a clause set (for which we will use a list of lists). [5 marks]

4. Write a Python function simple_sat_solve in a single argument clause_set that solves the satisfiability of the clause set by running through all truth assignments. In case the clause set is satisfiable it should output a satisfying assignment. [5 marks]

5. Write a recursive Python function branching_sat solve in the two arguments clause_set and partial_assignment that solves the satisfiability of the clause set by branching on the two truth assignments for a given variable. In case the clause set is satisfiable under the partial assignment it should output a satisfying assignment. When this is run with an empty partial assignment it should act as a SAT-solver. [10 marks]

6. Write a Python function unit_propagate in the two arguments literal and clause_set which outputs a new clause set after iteratively applying unit propagation until it cannot be applied further. [10 marks]

7. Write a Python function pure_literal_eliminate in a single argument clause_set which outputs a new clause set after iteratively applying the pure literal assignment scheme until it cannot be applied further. [10 marks]

8. Write a recursive Python function dpll_sat_solve in the two arguments clause_set and partial assignment that solves the satisfiability of the clause set by applying unit propa-gation and pure literal elimination before branching on the two truth assignments for a given variable (this is the famous DPLL algorithm). In case the clause set is satisfiable under the partial assignment it should output a satisfying assignment. When this is run with an empty partial assignment it should act as a SAT-solver. [20 marks]

9. There are three people: Stihl, Moller and Einstein. It is known that exactly one of them is Russian, while the other two are Germans. Moreover, every Russian must be a spy. When Stihl meets Moller in a corridor, he makes the following joke: “you know, Moller, you are as German as I am Russian”. It is known that Stihl always tells the truth when he is joking. We aim to establish that Einstein is not a Russian spy by using your SAT-solver. Use propositional variables from the Cartesian product of {Stihl, Moller, Einstein} and {Russian, German, Spy}, e.g. Einstein-Spy is true iff Einstein is a spy. Write out a propositional encoding for this problem justifying your constructed clauses. Make a DIMACS format instance and run it through your SAT-solver. [10 marks]

10. The final 10 marks of the coursework will be allocated according to the speed of your functions unit_propagate, pure_literal_eliminate and dpll_sat_solve run on some bench-mark instances. If your code is faster than mine, you receive 10 marks; within a factor of 2, 6 marks; within a factor of 3, 4 marks; within a factor of 4, 2 marks. [10 marks]