11
11
from __future__ import annotations
12
12
13
13
import random
14
+ from typing import Iterable
14
15
15
16
16
17
class Clause :
@@ -27,12 +28,12 @@ class Clause:
27
28
True
28
29
"""
29
30
30
- def __init__ (self , literals : list [int ]) -> None :
31
+ def __init__ (self , literals : list [str ]) -> None :
31
32
"""
32
33
Represent the literals and an assignment in a clause."
33
34
"""
34
35
# Assign all literals to None initially
35
- self .literals = {literal : None for literal in literals }
36
+ self .literals : dict [ str , bool | None ] = {literal : None for literal in literals }
36
37
37
38
def __str__ (self ) -> str :
38
39
"""
@@ -52,7 +53,7 @@ def __len__(self) -> int:
52
53
"""
53
54
return len (self .literals )
54
55
55
- def assign (self , model : dict [str , bool ]) -> None :
56
+ def assign (self , model : dict [str , bool | None ]) -> None :
56
57
"""
57
58
Assign values to literals of the clause as given by model.
58
59
"""
@@ -68,7 +69,7 @@ def assign(self, model: dict[str, bool]) -> None:
68
69
value = not value
69
70
self .literals [literal ] = value
70
71
71
- def evaluate (self , model : dict [str , bool ]) -> bool :
72
+ def evaluate (self , model : dict [str , bool | None ]) -> bool | None :
72
73
"""
73
74
Evaluates the clause with the assignments in model.
74
75
This has the following steps:
@@ -97,7 +98,7 @@ class Formula:
97
98
{{A1, A2, A3'}, {A5', A2', A1}} is ((A1 v A2 v A3') and (A5' v A2' v A1))
98
99
"""
99
100
100
- def __init__ (self , clauses : list [Clause ]) -> None :
101
+ def __init__ (self , clauses : Iterable [Clause ]) -> None :
101
102
"""
102
103
Represent the number of clauses and the clauses themselves.
103
104
"""
@@ -139,14 +140,14 @@ def generate_formula() -> Formula:
139
140
"""
140
141
Randomly generate a formula.
141
142
"""
142
- clauses = set ()
143
+ clauses : set [ Clause ] = set ()
143
144
no_of_clauses = random .randint (1 , 10 )
144
145
while len (clauses ) < no_of_clauses :
145
146
clauses .add (generate_clause ())
146
- return Formula (set ( clauses ) )
147
+ return Formula (clauses )
147
148
148
149
149
- def generate_parameters (formula : Formula ) -> ( list [Clause ], list [str ]) :
150
+ def generate_parameters (formula : Formula ) -> tuple [ list [Clause ], list [str ]] :
150
151
"""
151
152
Return the clauses and symbols from a formula.
152
153
A symbol is the uncomplemented form of a literal.
@@ -173,8 +174,8 @@ def generate_parameters(formula: Formula) -> (list[Clause], list[str]):
173
174
174
175
175
176
def find_pure_symbols (
176
- clauses : list [Clause ], symbols : list [str ], model : dict [str , bool ]
177
- ) -> ( list [str ], dict [str , bool ]) :
177
+ clauses : list [Clause ], symbols : list [str ], model : dict [str , bool | None ]
178
+ ) -> tuple [ list [str ], dict [str , bool | None ]] :
178
179
"""
179
180
Return pure symbols and their values to satisfy clause.
180
181
Pure symbols are symbols in a formula that exist only
@@ -198,11 +199,11 @@ def find_pure_symbols(
198
199
{'A1': True, 'A2': False, 'A3': True, 'A5': False}
199
200
"""
200
201
pure_symbols = []
201
- assignment = dict ()
202
+ assignment : dict [ str , bool | None ] = dict ()
202
203
literals = []
203
204
204
205
for clause in clauses :
205
- if clause .evaluate (model ) is True :
206
+ if clause .evaluate (model ):
206
207
continue
207
208
for literal in clause .literals :
208
209
literals .append (literal )
@@ -225,8 +226,8 @@ def find_pure_symbols(
225
226
226
227
227
228
def find_unit_clauses (
228
- clauses : list [Clause ], model : dict [str , bool ]
229
- ) -> ( list [str ], dict [str , bool ]) :
229
+ clauses : list [Clause ], model : dict [str , bool | None ]
230
+ ) -> tuple [ list [str ], dict [str , bool | None ]] :
230
231
"""
231
232
Returns the unit symbols and their values to satisfy clause.
232
233
Unit symbols are symbols in a formula that are:
@@ -263,7 +264,7 @@ def find_unit_clauses(
263
264
Ncount += 1
264
265
if Fcount == len (clause ) - 1 and Ncount == 1 :
265
266
unit_symbols .append (sym )
266
- assignment = dict ()
267
+ assignment : dict [ str , bool | None ] = dict ()
267
268
for i in unit_symbols :
268
269
symbol = i [:2 ]
269
270
assignment [symbol ] = len (i ) == 2
@@ -273,8 +274,8 @@ def find_unit_clauses(
273
274
274
275
275
276
def dpll_algorithm (
276
- clauses : list [Clause ], symbols : list [str ], model : dict [str , bool ]
277
- ) -> ( bool , dict [str , bool ]) :
277
+ clauses : list [Clause ], symbols : list [str ], model : dict [str , bool | None ]
278
+ ) -> tuple [ bool | None , dict [str , bool | None ] | None ] :
278
279
"""
279
280
Returns the model if the formula is satisfiable, else None
280
281
This has the following steps:
0 commit comments