-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
399 lines (316 loc) · 12.5 KB
/
Copy pathmodel.py
File metadata and controls
399 lines (316 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"""
This module contains the main Module class.
"""
# pylint: disable=line-too-long,no-name-in-module
import functools
from typing import List, Optional
from ._vec_typing import VariableIs, VariableSoft, VectorLike
from ._model_typing import IVariableHard, IVector, VariableHard, VectorSized
from ._native import model as native_model
from .var import Variable
from .vec import Vector
def _int_to_vector_(number: int):
"""
Converts an int to a list of bools.
Args:
number (int): number to convert to binary
Returns:
list of bool: A list of bools representing number.
"""
return list(map(lambda x: bool(int(x)), bin(number)[2:]))
class Model:
"""
An object describing a single SAT solver and its corresponding solver.
Contains information about its variables, constraints, clauses, and
solutions.
Example usage:
>>> m = Model()
>>> a = m.var()
>>> b = m.var()
>>> m.add_assert(a & b)
>>> m.solve()
True
>>> assert bool(a) and bool(b)
"""
solver: native_model
sat: Optional[bool] = None
def __init__(self):
"""
Constructor for Model. Must be called, but no configuration currently.
"""
self.solver = native_model()
def __enter__(self):
return self
def __exit__(self, exception_type=None, value=None, traceback=None):
self.cleanup()
return False
def var(self) -> Variable:
"""
Create a new variable.
Returns:
Variable: the newly created variable.
"""
return self.variable()
def variable(self) -> Variable:
"""
Create a new variable.
Returns:
Variable: the newly created variable.
"""
return Variable(self, self.solver.var())
def to_vec(self, other: VectorLike, width: Optional[int] = None) -> Vector:
"""
Converts the specified object into a Vector of width, if present.
Args:
other (list, tuple, int): object to coerce to Vector.
Returns:
Vector: A vector representing the input parameter.
"""
return self.to_vector(other, width=width)
def to_vector(self, other: VectorLike, width: Optional[int] = None) -> Vector:
"""
Converts the specified object into a Vector of width, if present.
Args:
other (list, tuple, int): object to coerce to Vector.
Returns:
Vector: A vector representing the input parameter.
"""
if isinstance(other, Vector):
if width:
return self.to_vector(other.variables, width)
return other
if isinstance(other, int):
return self.to_vector(_int_to_vector_(other), width)
if width:
l_other: int = len(other)
if l_other > width:
raise ValueError("Length of object exceeds width")
difference: List[VariableIs] = [False]*(width - l_other)
return Vector(self, vector=difference + list(other))
return Vector(self, vector=other)
def vec(self, width: int) -> Vector:
"""
Create a new vector of the specified width.
Args:
width (int): width of the new vector.
Returns:
Vector: A new vector of the specified width.
"""
return self.vector(width)
def vector(self, width: int) -> Vector:
"""
Create a new vector of the specified width.
Args:
width (int): width of the new vector.
Returns:
Vector: A new vector of the specified width.
"""
return Vector(self, width=width)
def join_vec(self, vectors: IVector) -> Vector:
"""
Returns a single Vector out of a list or tuple of Vectors, appending
them together.
Args:
vectors (list of Vector, tuple of Vector): collection of Vectors
to combine.
Returns:
Vector: a single vector of all passed Vectors.
"""
interior: List[VariableSoft] = []
for vec in vectors:
interior.extend(vec.variables)
return self.to_vector(interior)
def split_vec(self, vector: VectorSized, width: int) -> List[Vector]:
"""
Returns a list of Vectors splitting the specified vector into a series
of different vectors.
Args:
vector (Vector): a vector to split.
width (int): width to split the Vector at.
Returns:
list of Vector: a collection of Vectors of the specified width.
"""
assert len(vector) % width == 0
vectors = [
self.to_vec(vector[index:index+width])
for index in range(0, len(vector), width)
]
return vectors
def neg_var(self, var: VariableHard) -> Variable:
"""
Negate the specified variable. e.g., var -> -var
Args:
var (int or Variable): the variable or its identifier to negate.
Returns:
Variable: the negation of var.
"""
if isinstance(var, int):
return Variable(self, identifier=-var)
if isinstance(var, Variable):
return Variable(self, identifier=-var.identifier)
raise TypeError(f"Unknown type to negate: {type(var)}")
def add_constraint(self, left: int, operator: str, right: int) -> Variable:
"""
Add a constraint to the model.
Args:
left (int): the left variable identifier for the gate
operator (str): the gate to add
right (int): the right variable identifier for the gate
Returns:
Variable: the result of the gate
"""
assert isinstance(left, int)
assert isinstance(operator, str)
assert isinstance(right, int)
if abs(left) > self.solver.num_constraint_vars():
msg = f"Passed left identifier {left} exceeds number of vars: "
msg += f"{self.solver.num_constraint_vars()}"
raise ValueError(msg)
if abs(right) > self.solver.num_constraint_vars():
msg = f"Passed right identifier {right} exceeds number of vars: "
msg += f"{self.solver.num_constraint_vars()}"
raise ValueError(msg)
if operator == 'and':
return Variable(self, self.solver.v_and(left, right))
if operator == 'nand':
return Variable(self, self.solver.v_nand(left, right))
if operator == 'or':
return Variable(self, self.solver.v_or(left, right))
if operator == 'nor':
return Variable(self, self.solver.v_nor(left, right))
if operator == 'xor':
return Variable(self, self.solver.v_xor(left, right))
raise ValueError(f"Unknown operator: {operator}")
def add_assert(self, var: VariableHard) -> None:
"""
Add an assertion that a single Variable is true via adding a clause
with only that variable. Note that clauses cannot be removed.
Args:
var (int or Variable): variable or its identifier to assert.
"""
if isinstance(var, bool):
raise ValueError(f"Expected Variable or int; got bool: {var}")
self.solver.v_assert(int(var))
def add_assume(self, var: VariableHard) -> None:
"""
Adds an assumption about the truthiness of a variable in the model.
Assertions can be removed at a later time via remove_assume(...).
Also adds an or clause in case the specified variable isn't otherwise
present in the model (the clause `var | -var` will not impact the
satisfiability of the overall model, but ensures that var is seen).
Note that assumptions are slower in general than adding a clause,
however, unlike clauses, they can be removed.
Args:
var (int or Variable): variable or its identifier to assume.
"""
if isinstance(var, bool):
raise ValueError(f"Expected Variable or int; got bool: {var}")
if isinstance(var, int):
if abs(var) > self.solver.num_constraint_vars():
msg = f"Passed identifier {var} exceeds number of vars: "
msg += f"{self.solver.num_constraint_vars()}"
raise ValueError(msg)
self.solver.v_assume(var)
else:
if abs(var.identifier) > self.solver.num_constraint_vars():
msg = f"Passed identifier {var.identifier} exceeds number of vars: "
msg += f"{self.solver.num_constraint_vars()}"
raise ValueError(msg)
self.solver.v_assume(var.identifier)
def remove_assume(self, var: VariableHard) -> None:
"""
Removes an assumption about the truthiness of a variable.
Args:
var (int or Variable): variable or its identifier to assume.
"""
if isinstance(var, bool):
raise ValueError(f"Expected Variable or int; got bool: {var}")
if isinstance(var, int):
self.solver.v_unassume(var)
else:
self.solver.v_unassume(var.identifier)
def __to_ident__(self, var: VariableHard) -> int:
if isinstance(var, bool):
msg = "Cannot pass bool as an identifier into CNF clause"
raise ValueError(msg)
if isinstance(var, int):
if abs(var) > self.solver.num_constraint_vars():
msg = f"Passed identifier {var} exceeds number of vars: "
msg += f"{self.solver.num_constraint_vars()}"
raise ValueError(msg)
return var
if abs(var.identifier) > self.solver.num_constraint_vars():
msg = f"Passed identifier {var.identifier} exceeds number of vars: "
msg += f"{self.solver.num_constraint_vars()}"
raise ValueError(msg)
return var.identifier
def negate_solution(self, variables: IVariableHard) -> VariableIs:
"""
Given a set of variables, return a variable expressing the negation
of their values. Used in incremental solving to find another solution.
Args:
variables (iterable of Variables): variables whose combined solution
should be negated.
Returns:
Variable: var which describs the combined negation.
"""
assert variables
var_list = list(variables)
bool_vars = list(map(bool, var_list))
result = False
for _index, var in enumerate(var_list):
result = result | (var != bool_vars[_index])
return result
def solve(self, max_conflicts: Optional[int] = None,
max_time: Optional[float] = None) -> Optional[bool]:
"""
Solve the current model. This adds all new clauses to the solver and
runs CMS under the present assumptions.
Returns:
bool: whether or not the model is satisfiable.
"""
if max_conflicts:
self.solver.config_conflicts(max_conflicts)
else:
self.solver.config_conflicts(-1)
if max_time:
self.solver.config_timeout(max_time)
else:
self.solver.config_timeout(-1)
self.sat = self.solver.solve()
return self.sat
def _get_value_(self, identifier: VariableHard) -> Optional[bool]:
"""
Get the value of a variable by its identifier. Returns None if the
model is unsatisfiable or unsolved.
Args:
identifier (int): identifier of the variable.
Returns:
bool: whether or not the variable is True, accounting for negation
of the identifier
"""
if self.sat:
return self.solver.val(int(identifier))
return None
def cleanup(self):
"""
Clean up the solver after use, destroying all internal variables. This
helps when you know that you won't be using this particular solver
instance any more, freeing all used memory allocated by CryptoMiniSat,
and the cmsh native bindings. Otherwise, this memory will stay
allocated until a Python GC pass is performed.
You cannot use this instance after calling cleanup().
"""
self.solver.delete_model()
self.solver = None
def with_model(func):
"""
Decorator to automatically wrap the specified function with a temporary
Model instance.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
with Model() as model:
all_args = [model] + list(args)
return func(*all_args, **kwargs)
return wrapper