Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

""" 

Abstract SAT Solver 

  

All SAT solvers must inherit from this class. 

  

.. NOTE:: 

  

Our SAT solver interfaces are 1-based, i.e., literals start at 

1. This is consistent with the popular DIMACS format for SAT 

solving but not with Pythion's 0-based convention. However, this 

also allows to construct clauses using simple integers. 

  

AUTHORS: 

  

- Martin Albrecht (2012): first version 

""" 

from __future__ import absolute_import 

  

cdef class SatSolver: 

def __cinit__(self, *args, **kwds): 

""" 

Constuct a new SATSolver. 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

""" 

pass 

  

def var(self, decision=None): 

""" 

Return a *new* variable. 

  

INPUT: 

  

- ``decision`` - is this variable a decision variable? 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.var() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def nvars(self): 

""" 

Return the number of variables. 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.nvars() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def add_clause(self, lits): 

""" 

Add a new clause to set of clauses. 

  

INPUT: 

  

- ``lits`` - a tuple of integers != 0 

  

.. NOTE:: 

  

If any element ``e`` in ``lits`` has ``abs(e)`` greater 

than the number of variables generated so far, then new 

variables are created automatically. 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.add_clause( (1, -2 , 3) ) 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def read(self, filename): 

r""" 

Reads DIMAC files. 

  

Reads in DIMAC formatted lines (lazily) from a 

file or file object and adds the corresponding 

clauses into this solver instance. Note that the 

DIMACS format is not well specified, see 

http://people.sc.fsu.edu/~jburkardt/data/cnf/cnf.html, 

http://www.satcompetition.org/2009/format-benchmarks2009.html, 

and http://elis.dvo.ru/~lab_11/glpk-doc/cnfsat.pdf. 

The differences were summarized in the discussion on 

the ticket :trac:`16924`. This method assumes the following 

DIMACS format 

  

- Any line starting with "c" is a comment 

- Any line starting with "p" is a header 

- Any variable 1-n can be used 

- Every line containing a clause must end with a "0" 

  

INPUT: 

  

- ``filename`` - The name of a file as a string or a file object 

  

EXAMPLES:: 

  

sage: from six import StringIO # for python 2/3 support 

sage: file_object = StringIO("c A sample .cnf file.\np cnf 3 2\n1 -3 0\n2 3 -1 0 ") 

sage: from sage.sat.solvers.dimacs import DIMACS 

sage: solver = DIMACS() 

sage: solver.read(file_object) 

sage: solver.clauses() 

[((1, -3), False, None), ((2, 3, -1), False, None)] 

""" 

if isinstance(filename,str): 

file_object = open(filename, "r") 

else: 

file_object = filename 

for line in file_object: 

if line.startswith("c"): 

continue # comment 

if line.startswith("p"): 

continue # header 

line = line.split(" ") 

clause = [int(e) for e in line if e] 

clause = clause[:-1] # strip trailing zero 

self.add_clause(clause) 

  

def __call__(self, assumptions=None): 

""" 

Solve this instance. 

  

INPUT: 

  

- ``assumptions`` - assumed variable assignments (default: ``None``) 

  

OUTPUT: 

  

- If this instance is SAT: A tuple of length ``nvars()+1`` 

where the ``i``-th entry holds an assignment for the 

``i``-th variables (the ``0``-th entry is always ``None``). 

  

- If this instance is UNSAT: ``False`` 

  

- If the solver was interrupted before deciding satisfiability 

``None``. 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def conflict_clause(self): 

""" 

Return conflict clause if this instance is UNSAT and the last 

call used assumptions. 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.conflict_clause() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def learnt_clauses(self, unitary_only=False): 

""" 

Return learnt clauses. 

  

INPUT: 

  

- ``unitary_only`` - return only unitary learnt clauses (default: ``False``) 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.learnt_clauses() 

Traceback (most recent call last): 

... 

NotImplementedError 

  

sage: solver.learnt_clauses(unitary_only=True) 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def __repr__(self): 

""" 

TESTS:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver 

a generic SAT solver (don't use me, inherit from me) 

""" 

return "a generic SAT solver (don't use me, inherit from me)" 

  

def clauses(self, filename=None): 

""" 

Return original clauses. 

  

INPUT: 

  

- ``filename'' - if not ``None`` clauses are written to ``filename`` in 

DIMACS format (default: ``None``) 

  

OUTPUT: 

  

If ``filename`` is ``None`` then a list of ``lits, is_xor, rhs`` 

tuples is returned, where ``lits`` is a tuple of literals, 

``is_xor`` is always ``False`` and ``rhs`` is always ``None``. 

  

If ``filename`` points to a writable file, then the list of original 

clauses is written to that file in DIMACS format. 

  

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.clauses() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

  

def __getattr__(self, name): 

""" 

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.gens() # __getattr__ points this to clauses 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

if name == "gens": 

return self.clauses 

else: 

raise AttributeError("'%s' object has no attribute '%s'"%(self.__class__.__name__,name)) 

  

def trait_names(self): 

""" 

Allow alias to appear in tab completion. 

  

EXAMPLES:: 

  

sage: from sage.sat.solvers.satsolver import SatSolver 

sage: solver = SatSolver() 

sage: solver.trait_names() 

['gens'] 

""" 

return ["gens"] 

  

def SAT(solver=None, *args, **kwds): 

r""" 

Return a :class:`SatSolver` instance. 

  

Through this class, one can define and solve `SAT 

<https://en.wikipedia.org/wiki/Boolean_satisfiability_problem>`__ problems. 

  

INPUT: 

  

- ``solver`` (string) -- select a solver. Admissible values are: 

  

- ``"cryptominisat"`` -- note that the cryptominisat package must be 

installed. 

  

- ``"LP"`` -- use :class:`~sage.sat.solvers.sat_lp.SatLP` to solve the 

SAT instance. 

  

- ``None`` (default) -- use CryptoMiniSat if available, and a LP solver 

otherwise. 

  

EXAMPLES:: 

  

sage: SAT(solver="LP") 

an ILP-based SAT Solver 

  

TESTS:: 

  

sage: SAT(solver="Wouhouuuuuu") 

Traceback (most recent call last): 

... 

ValueError: Solver 'Wouhouuuuuu' is not available 

  

Forcing CryptoMiniSat:: 

  

sage: SAT(solver="cryptominisat") # optional - cryptominisat 

CryptoMiniSat solver: 0 variables, 0 clauses. 

""" 

if solver is None: 

import pkgutil 

if pkgutil.find_loader('pycryptosat') is None: 

solver = "LP" 

else: 

solver = "cryptominisat" 

  

if solver == 'cryptominisat': 

from sage.sat.solvers.cryptominisat import CryptoMiniSat 

return CryptoMiniSat(*args, **kwds) 

elif solver == "LP": 

from .sat_lp import SatLP 

return SatLP() 

else: 

raise ValueError("Solver '{}' is not available".format(solver))