 ### Python海龟宝典含200多个原创的用turtle模块制作的创意程序，原名《Python趣味编程200例》。准备参加全国创意编程与智能设计大赛的同学们可以用来做参考。

Let ?1,?2,p1,p2,… be propositional variables. A SAT problem, represented in conjunctive normal form, consists in a conjunction of disjunctions of propositional variables and their complements, such as

(?1?¯2?3)(?2?5).(p1∨p¯2∨p3)∧(p2∨p5).

We call each conjuct a clause; in the above example, we have two clauses, ?1=?1?¯2?3c1=p1∨p¯2∨p3 and ?2=?2?5c2=p2∨p5. The disjuncts in a clause are called literals: for example, the first clause ?1=?1?¯2?3c1=p1∨p¯2∨p3 contains the literals ?1p1?¯2p¯2, and ?3p3.

The satisfiability question is: can we find a truth assignment to the variables that makes the expression true? In the above case, the answer is yes: we can take:

?1=????,?5=????p1=True,p5=True

and any value for ?2,?3p2,p3.

## SAT representation

To represent an instance of SAT, we represent literals, clauses, and the overall expression, as follows.

Literals. We represent the literal ??pk via the positive integer ?k, and the literal ?¯?p¯k via the negative integer ?−k.

Clauses. We represent a clause via the set of integers representing the clause literals.
For instance, we represent the clause ?1?¯3?4p1∨p¯3∨p4 via the set {1,3,4}{1,−3,4}.

SAT problem. We represent a SAT problem (again, in conjunctive normal form) via the set consisting in the representation of its clauses. For instance, the problem

(?1?¯2?3)(?2?5)(p1∨p¯2∨p3)∧(p2∨p5)

is represented by the set of sets:

{{1,2,3},{2,5}}.{{1,−2,3},{2,5}}.

There are various operations that we need to do on clauses, and on the overall SAT problem, to solve it. Thus, we encapsulate both clauses, and the SAT problem, in python classes, so we can associate the operations along with the representations.

### Clauses

We first define an auxiliary function, which tells us whether a set contains both an integer and its negative. This will be used, for instance, to detect whether a clause contains both a literal and its complement.

### Truth assignments

We are seeking a truth assignment for the propositional variables that makes the expression true, and so, that makes each clause true.

We represent the truth assignment that assigns True to ??pk via the integer ?k, and the truth assignment that assigns False to ??pk via ?−k. Thus, if you have a (positive or negative) literal ?i, the truth assignment ?i will make it true.

We represent truth assignments to multiple variables simply as the set of assignments to individual variables. For example, the truth assignment that assigns True to ?1p1 and False to ?2p2 will be represented via the set {1,2}{1,−2}.

## Question 1: Define Clause simplification

To solve a SAT instance, we need to search for a truth assignment to its propositional variables that will make all the clauses true. We will search for such a truth assignment by trying to build it one variable at a time. So a basic operation on a clause will be:

Given a clause, and a truth assignment for one variable, compute the result on the clause.

What is the result on the clause? Consider a clause with representation ?c (thus, ?c is a set of integers) and a truth assignment ?i (recall that ?i can be positive or negative, depending on whether it assigns True or False to ??pi). There are three cases:

• If ??i∈c, then the ?i literal of ?c is true, and so is the whole clause. We return True to signify it.
• If ??−i∈c, then the ?−i literal of ?c is false, and it cannot help make the clause true. We return the clause ?{?}c∖{−i}, which corresponds to the remaining ways of making the clause true under assignment ?i.
• If neither ?i nor ?−i is in ?c, then we return ?c itself, as ?c is not affected by the truth assignment ?i.

Based on the above discussion, implement a simplify method for a Clause that, given a truth assignment, returns either a simplified clause, if some literals 