Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/sat/solvers/cryptominisat/solverconf.pyx
8823 views
1
"""
2
Configuration for CryptoMiniSat.
3
4
EXAMPLE:
5
6
We construct a new configuration object::
7
8
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
9
sage: s = SolverConf() # optional - cryptominisat
10
sage: s.doxorsubsumption # optional - cryptominisat
11
True
12
13
and modify it such that we disable xor subsumption::
14
15
sage: s.doxorsubsumption = False # optional - cryptominisat
16
17
Finally, we pass it on to CryptoMiniSat::
18
19
sage: from sage.sat.solvers.cryptominisat import CryptoMiniSat # optional - cryptominisat
20
sage: cms = CryptoMiniSat(s) # optional - cryptominisat
21
22
Note that we can achieve the same effect by::
23
24
sage: from sage.sat.solvers.cryptominisat import CryptoMiniSat # optional - cryptominisat
25
sage: cms = CryptoMiniSat(doxorsubsumption=False) # optional - cryptominisat
26
27
AUTHORS:
28
29
- Martin Albrecht (2012): first version
30
"""
31
##############################################################################
32
# Copyright (C) 2012 Martin Albrecht <[email protected]>
33
# Distributed under the terms of the GNU General Public License (GPL)
34
# The full text of the GPL is available at:
35
# http://www.gnu.org/licenses/
36
##############################################################################
37
38
###
39
# NOTE: To add new options edit solverconf_helper.cpp which maintains
40
# a map from names to C++ attributes.
41
###
42
43
from libc.stdint cimport uint32_t, uint64_t
44
45
include "sage/ext/stdsage.pxi"
46
47
cdef class SolverConf(object):
48
"""
49
Configuration for cls:`CryptoMiniSat`
50
51
This class implements an interface to the C++ SolverConf class
52
which allows to configure the behaviour of the cls:`CryptoMiniSat`
53
solver.
54
"""
55
def __cinit__(self, **kwds):
56
"""
57
Construct a new configuration object.
58
59
INPUT:
60
61
- ``kwds`` - see string representation of any instance of this
62
class for a list of options.
63
64
EXAMPLE::
65
66
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
67
sage: s = SolverConf() # optional - cryptominisat
68
sage: s # optional - cryptominisat
69
random_var_freq: 0.001000 # the frequency with which the decision heuristic tries to choose a random variable. (default 0.02)
70
...
71
greedyunbound: True # if set, then variables will be greedily unbounded (set to l_undef). this is experimental
72
origseed: 0 #
73
"""
74
self._conf = new SolverConfC()
75
self._nopts = setup_map(self._map, self._conf[0], 100)
76
77
for k,v in kwds.iteritems():
78
self[k] = v
79
80
def __dealloc__(self):
81
"""
82
TESTS::
83
84
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
85
sage: for i in range(100): # optional - cryptominisat
86
... s = SolverConf()
87
... del s
88
89
"""
90
del self._conf
91
92
def __setitem__(self, name, value):
93
"""
94
Set options using dictionary notation.
95
96
EXAMPLE::
97
98
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
99
sage: s = SolverConf() # optional - cryptominisat
100
sage: s['polarity_mode'] = 3 # optional - cryptominisat
101
sage: s['polarity_mode'] # optional - cryptominisat
102
3
103
104
TESTS::
105
106
sage: s['foobar'] = 1.2 # optional - cryptominisat
107
Traceback (most recent call last):
108
...
109
AttributeError: SolverConf has no option 'foobar'
110
111
"""
112
for i in range(self._nopts):
113
name_i = self._map[i].name.lower()
114
if name != name_i:
115
continue
116
if self._map[i].type == t_int:
117
(<int*>self._map[i].target)[0] = value
118
elif self._map[i].type == t_float:
119
(<float*>self._map[i].target)[0] = value
120
elif self._map[i].type == t_double:
121
(<double*>self._map[i].target)[0] = value
122
elif self._map[i].type == t_Var:
123
(<uint32_t*>self._map[i].target)[0] = value
124
elif self._map[i].type == t_bool:
125
(<bint*>self._map[i].target)[0] = value
126
elif self._map[i].type == t_uint32_t:
127
(<uint32_t*>self._map[i].target)[0] = value
128
elif self._map[i].type == t_uint64_t:
129
(<uint64_t*>self._map[i].target)[0] = value
130
else:
131
raise NotImplementedError("Type %d of CryptoMiniSat is not supported yet."%self._map[i].type)
132
return
133
raise AttributeError("SolverConf has no option '%s'"%name)
134
135
def __setattr__(self, name, value):
136
"""
137
Set options using attributes.
138
139
EXAMPLE::
140
141
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
142
sage: s = SolverConf() # optional - cryptominisat
143
sage: s.dovarelim = False # optional - cryptominisat
144
sage: s.dovarelim # optional - cryptominisat
145
False
146
147
TESTS::
148
149
sage: s.foobar = 1.2 # optional - cryptominisat
150
Traceback (most recent call last):
151
...
152
AttributeError: SolverConf has no option 'foobar'
153
"""
154
self[name] = value
155
156
def __getitem__(self, name):
157
"""
158
Read options using dictionary notation.
159
160
EXAMPLE::
161
162
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
163
sage: s = SolverConf() # optional - cryptominisat
164
sage: s['simpstartmult'] # optional - cryptominisat
165
300.0
166
167
TESTS::
168
169
sage: s['foobar'] # optional - cryptominisat
170
Traceback (most recent call last):
171
...
172
AttributeError: SolverConf has no option 'foobar'
173
174
"""
175
for i in range(self._nopts):
176
name_i = self._map[i].name.lower()
177
if name_i != name:
178
continue
179
if self._map[i].type == t_int:
180
return int((<int*>self._map[i].target)[0])
181
elif self._map[i].type == t_float:
182
return float((<float*>self._map[i].target)[0])
183
elif self._map[i].type == t_double:
184
return float((<double*>self._map[i].target)[0])
185
elif self._map[i].type == t_Var:
186
return int((<uint32_t*>self._map[i].target)[0])
187
elif self._map[i].type == t_bool:
188
return bool((<bint*>self._map[i].target)[0])
189
elif self._map[i].type == t_uint32_t:
190
return int((<uint32_t*>self._map[i].target)[0])
191
elif self._map[i].type == t_uint64_t:
192
return int((<uint64_t*>self._map[i].target)[0])
193
else:
194
raise NotImplementedError("Type %d of CryptoMiniSat is not supported."%self._map[i].type)
195
raise AttributeError("SolverConf has no option '%s'"%name)
196
197
def __getattr__(self, name):
198
"""
199
Read options using dictionary notation.
200
201
EXAMPLE::
202
203
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
204
sage: s = SolverConf() # optional - cryptominisat
205
sage: s.restrictpickbranch # optional - cryptominisat
206
0
207
208
TESTS::
209
210
sage: s.foobar # optional - cryptominisat
211
Traceback (most recent call last):
212
...
213
AttributeError: SolverConf has no option 'foobar'
214
"""
215
216
return self[name]
217
218
def __repr__(self):
219
"""
220
Print the current configuration.
221
222
EXAMPLE::
223
224
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
225
sage: s = SolverConf(); s # optional - cryptominisat
226
random_var_freq: 0.001000 # the frequency with which the decision heuristic tries to choose a random variable. (default 0.02)
227
...
228
greedyunbound: True # if set, then variables will be greedily unbounded (set to l_undef). this is experimental
229
origseed: 0 #
230
"""
231
rep = []
232
for i in range(self._nopts):
233
name = self._map[i].name.lower()
234
doc = self._map[i].doc.lower()
235
if self._map[i].type == t_int:
236
val = "%10d"%(<int*>self._map[i].target)[0]
237
elif self._map[i].type == t_float:
238
val = "%10f"%(<float*>self._map[i].target)[0]
239
elif self._map[i].type == t_double:
240
val = "%10f"%(<double*>self._map[i].target)[0]
241
elif self._map[i].type == t_Var:
242
val = "%10d"%(<uint32_t*>self._map[i].target)[0]
243
elif self._map[i].type == t_bool:
244
val = "%10s"%bool((<bint*>self._map[i].target)[0])
245
elif self._map[i].type == t_uint32_t:
246
val = "%10d"%(<uint32_t*>self._map[i].target)[0]
247
elif self._map[i].type == t_uint64_t:
248
val = "%10d"%(<uint64_t*>self._map[i].target)[0]
249
else:
250
val = "UNKNOWN"
251
rep.append("%20s: %10s # %50s"%(name,val,doc))
252
return "\n".join(rep)
253
254
def trait_names(self):
255
"""
256
Return list of all option names.
257
258
EXAMPLE::
259
260
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
261
sage: s = SolverConf() # optional - cryptominisat
262
sage: s.trait_names() # optional - cryptominisat
263
['random_var_freq', 'clause_decay', 'restart_first', 'restart_inc', 'learntsize_factor', 'learntsize_inc',
264
'expensive_ccmin', 'polarity_mode', 'verbosity', 'restrictpickbranch', 'simpburstsconf', 'simpstartmult',
265
'simpstartmmult', 'doperformpresimp', 'failedlitmultiplier', 'dofindxors', 'dofindeqlits',
266
'doregfindeqlits', 'doreplace', 'doconglxors', 'doheuleprocess', 'doschedsimp', 'dosatelite',
267
'doxorsubsumption', 'dohyperbinres', 'doblockedclause', 'dovarelim', 'dosubsume1', 'doclausvivif',
268
'dosortwatched', 'dominimlearntmore', 'dominimlmorerecur', 'dofailedlit', 'doremuselessbins',
269
'dosubswbins', 'dosubswnonexistbins', 'doremuselesslbins', 'doprintavgbranch', 'docacheotfssr',
270
'docacheotfssrset', 'doextendedscc', 'docalcreach', 'dobxor', 'dootfsubsume', 'maxconfl', 'isplain',
271
'maxrestarts', 'needtodumplearnts', 'needtodumporig', 'maxdumplearntssize', 'libraryusage', 'greedyunbound', 'origseed']
272
"""
273
return [self._map[i].name.lower() for i in range(self._nopts)]
274
275
def __richcmp__(self, other, op):
276
"""
277
TESTS::
278
279
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
280
sage: s = SolverConf() # optional - cryptominisat
281
sage: t = copy(s) # optional - cryptominisat
282
sage: t == s # optional - cryptominisat
283
True
284
sage: t.dobxor = False # optional - cryptominisat
285
sage: t == s # optional - cryptominisat
286
False
287
sage: t < s # optional - cryptominisat
288
Traceback (most recent call last):
289
...
290
TypeError: Configurations are not ordered.
291
"""
292
if op not in (2,3):
293
raise TypeError("Configurations are not ordered.")
294
res = all(self[name] == other[name] for name in self.trait_names())
295
if op == 2: # ==
296
return res
297
if op == 3: # !=
298
return not res
299
300
def __copy__(self):
301
"""
302
Return a copy.
303
304
EXAMPLE::
305
306
sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat
307
sage: s = SolverConf() # optional - cryptominisat
308
sage: t = copy(s) # optional - cryptominisat
309
sage: t.verbosity = 1 # optional - cryptominisat
310
sage: t['verbosity'] # optional - cryptominisat
311
1
312
sage: s.verbosity # optional - cryptominisat
313
0
314
"""
315
cdef SolverConf other = PY_NEW(SolverConf)
316
other._conf = new SolverConfC()
317
other._nopts = setup_map(other._map, other._conf[0], 100)
318
for name in self.trait_names():
319
other[name] = self[name]
320
return other
321
322