python-sat 1.9.dev25__tar.gz → 1.9.dev26__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. {python_sat-1.9.dev25/python_sat.egg-info → python_sat-1.9.dev26}/PKG-INFO +1 -1
  2. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/lsu.py +3 -3
  3. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/mcsls.py +4 -2
  4. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/__init__.py +1 -1
  5. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/engines.py +6 -0
  6. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/formula.py +16 -8
  7. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/integer.py +110 -22
  8. {python_sat-1.9.dev25 → python_sat-1.9.dev26/python_sat.egg-info}/PKG-INFO +1 -1
  9. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/python_sat.egg-info/SOURCES.txt +8 -0
  10. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/pysolvers.cc +11 -1
  11. python_sat-1.9.dev26/tests/test_alien.py +42 -0
  12. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_boolengine.py +18 -0
  13. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_integer.py +115 -0
  14. python_sat-1.9.dev26/tests/test_itotalizer.py +126 -0
  15. python_sat-1.9.dev26/tests/test_lbx.py +247 -0
  16. python_sat-1.9.dev26/tests/test_lsu.py +93 -0
  17. python_sat-1.9.dev26/tests/test_mcsls.py +277 -0
  18. python_sat-1.9.dev26/tests/test_musx.py +94 -0
  19. python_sat-1.9.dev26/tests/test_negate.py +57 -0
  20. python_sat-1.9.dev26/tests/test_primer.py +90 -0
  21. python_sat-1.9.dev26/tests/test_rc2.py +185 -0
  22. python_sat-1.9.dev25/tests/test_lsu.py +0 -15
  23. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/LICENSE.txt +0 -0
  24. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/MANIFEST.in +0 -0
  25. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/README.rst +0 -0
  26. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/allies/__init__.py +0 -0
  27. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/allies/approxmc.py +0 -0
  28. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/allies/unigen.py +0 -0
  29. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/bitwise.hh +0 -0
  30. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/card.hh +0 -0
  31. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/clset.hh +0 -0
  32. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/common.hh +0 -0
  33. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/iseq.hh +0 -0
  34. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/itot.hh +0 -0
  35. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/ladder.hh +0 -0
  36. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/mto.hh +0 -0
  37. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/pairwise.hh +0 -0
  38. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/ptypes.hh +0 -0
  39. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/pycard.cc +0 -0
  40. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/seqcounter.hh +0 -0
  41. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/sortcard.hh +0 -0
  42. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/cardenc/utils.hh +0 -0
  43. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/__init__.py +0 -0
  44. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/bbscan.py +0 -0
  45. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/bica.py +0 -0
  46. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/fm.py +0 -0
  47. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/genhard.py +0 -0
  48. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/hitman.py +0 -0
  49. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/lbx.py +0 -0
  50. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/models.py +0 -0
  51. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/musx.py +0 -0
  52. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/optux.py +0 -0
  53. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/primer.py +0 -0
  54. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/rc2.py +0 -0
  55. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/examples/usage.py +0 -0
  56. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_parse.cc +0 -0
  57. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_plus.cc +0 -0
  58. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_plus.hh +0 -0
  59. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_py.hh +0 -0
  60. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_scan.hh +0 -0
  61. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_weight.cc +0 -0
  62. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pf_weight.hh +0 -0
  63. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pyformula.cc +0 -0
  64. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/formula/pyformula.hh +0 -0
  65. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/_fileio.py +0 -0
  66. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/_utils.py +0 -0
  67. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/card.py +0 -0
  68. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/pb.py +0 -0
  69. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/process.py +0 -0
  70. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/solvers.py +0 -0
  71. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/pysat/vector.py +0 -0
  72. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/python_sat.egg-info/dependency_links.txt +0 -0
  73. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/python_sat.egg-info/requires.txt +0 -0
  74. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/python_sat.egg-info/top_level.txt +0 -0
  75. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/requirements.txt +0 -0
  76. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/setup.cfg +0 -0
  77. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/setup.py +0 -0
  78. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/cadical103.tar.gz +0 -0
  79. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/cadical153.tar.gz +0 -0
  80. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/cadical195.tar.gz +0 -0
  81. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/cadical300.tar.gz +0 -0
  82. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/ergo.tar.gz +0 -0
  83. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/glucose30.tar.gz +0 -0
  84. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/glucose41.tar.gz +0 -0
  85. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/glucose421.tar.gz +0 -0
  86. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/lingeling.tar.gz +0 -0
  87. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/maplechrono.zip +0 -0
  88. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/maplecm.zip +0 -0
  89. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/maplesat.zip +0 -0
  90. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/mergesat3.tar.gz +0 -0
  91. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/minicard.tar.gz +0 -0
  92. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/minisat22.tar.gz +0 -0
  93. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/minisatgh.zip +0 -0
  94. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/cadical103.patch +0 -0
  95. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/cadical153.patch +0 -0
  96. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/cadical195.patch +0 -0
  97. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/cadical300.patch +0 -0
  98. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/ergo.patch +0 -0
  99. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/glucose30.patch +0 -0
  100. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/glucose41.patch +0 -0
  101. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/glucose421.patch +0 -0
  102. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/gluecard30.patch +0 -0
  103. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/gluecard41.patch +0 -0
  104. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/kissat404.patch +0 -0
  105. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/lingeling.patch +0 -0
  106. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/maplechrono.patch +0 -0
  107. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/maplecm.patch +0 -0
  108. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/maplesat.patch +0 -0
  109. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/mergesat3.patch +0 -0
  110. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/minicard.patch +0 -0
  111. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/minisat22.patch +0 -0
  112. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/minisatep.patch +0 -0
  113. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/patches/minisatgh.patch +0 -0
  114. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/solvers/prepare.py +0 -0
  115. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_accum_stats.py +0 -0
  116. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_activities.py +0 -0
  117. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_atmost.py +0 -0
  118. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_atmost1.py +0 -0
  119. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_atmostk.py +0 -0
  120. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_clausification.py +0 -0
  121. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_cnf.py +0 -0
  122. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_empty_clause.py +0 -0
  123. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_empty_extclause.py +0 -0
  124. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_encode_pb_conditional.py +0 -0
  125. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_equals1.py +0 -0
  126. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_flip_bits.py +0 -0
  127. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_formula_parsing.py +0 -0
  128. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_formula_unique.py +0 -0
  129. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_hitman.py +0 -0
  130. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_idpool.py +0 -0
  131. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_iseqcounter.py +0 -0
  132. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_oom.py +0 -0
  133. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_pb.py +0 -0
  134. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_process.py +0 -0
  135. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_projected_models.py +0 -0
  136. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_proof.py +0 -0
  137. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_propagate.py +0 -0
  138. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_unique_model.py +0 -0
  139. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_unique_mus.py +0 -0
  140. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_vector.py +0 -0
  141. {python_sat-1.9.dev25 → python_sat-1.9.dev26}/tests/test_warmstart.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-sat
3
- Version: 1.9.dev25
3
+ Version: 1.9.dev26
4
4
  Summary: A Python library for prototyping with SAT oracles
5
5
  Home-page: https://github.com/pysathq/pysat
6
6
  Author: Alexey Ignatiev, Joao Marques-Silva, Antonio Morgado
@@ -164,8 +164,8 @@ class LSU:
164
164
  # (ITotalizer must be extended to support PB constraints first)
165
165
  self.topv += 1
166
166
  selv = self.topv
167
- cl.append(self.topv)
168
- self.oracle.add_clause(cl)
167
+ # the selector is added to a copy: the input formula must stay intact
168
+ self.oracle.add_clause(cl + [self.topv])
169
169
  self.sels.append(selv)
170
170
 
171
171
  if self.verbose > 1:
@@ -285,7 +285,7 @@ class LSU:
285
285
  cost = 0
286
286
 
287
287
  for i, cl in enumerate(formula.soft):
288
- cost += formula.wght[i] if all(l not in model_set for l in filter(lambda l: abs(l) <= self.formula.nv, cl)) else 0
288
+ cost += formula.wght[i] if all(l not in model_set for l in cl) else 0
289
289
 
290
290
  return cost
291
291
 
@@ -175,7 +175,9 @@ class MCSls(object):
175
175
 
176
176
  for cl in formula.soft:
177
177
  new_cl = cl[:]
178
- if len(cl) > 1 or cl[0] < 0:
178
+ # a unit soft clause can serve as its own selector unless an
179
+ # identical one has already taken it (selectors must be unique)
180
+ if len(cl) > 1 or cl[0] < 0 or cl[0] in self.smap:
179
181
  self.topv += 1
180
182
  sel = self.topv
181
183
 
@@ -277,7 +279,7 @@ class MCSls(object):
277
279
  else:
278
280
  # soft clauses should be augmented with a selector
279
281
  sel = cl[0]
280
- if len(cl) > 1 or cl[0] < 0:
282
+ if len(cl) > 1 or cl[0] < 0 or cl[0] in self.smap:
281
283
  self.topv += 1
282
284
  sel = self.topv
283
285
 
@@ -10,7 +10,7 @@
10
10
 
11
11
  # current version
12
12
  #==============================================================================
13
- VERSION = (1, 9, 'dev', 25)
13
+ VERSION = (1, 9, 'dev', 26)
14
14
 
15
15
 
16
16
  # PEP440 Format
@@ -173,7 +173,13 @@ class LinearConstraint:
173
173
 
174
174
  if min(self.wght.values()) == max(self.wght.values()):
175
175
  if self.wght[self.lits[0]] != 1:
176
+ unsat = self.rbnd < 0
176
177
  self.rbnd //= self.wght[self.lits[0]]
178
+
179
+ # Decimal's // rounds towards zero, so a negative
180
+ # bound, i.e. an unsatisfiable constraint, may become 0
181
+ if unsat and self.rbnd >= 0:
182
+ self.rbnd = -1
177
183
  self.wght = {l: 1 for l in lits}
178
184
 
179
185
  # unweighted / cardinality propagation
@@ -3994,6 +3994,11 @@ class CNF(Formula, object):
3994
3994
  negated.auxvars = []
3995
3995
  negated.enclits = []
3996
3996
 
3997
+ # an empty clause is falsified by every assignment,
3998
+ # which makes the negation of the formula a tautology
3999
+ if any(not cl for cl in self.clauses):
4000
+ return negated
4001
+
3997
4002
  for cl in self.clauses:
3998
4003
  auxv = -cl[0]
3999
4004
  if len(cl) > 1:
@@ -4074,7 +4079,10 @@ class CNF(Formula, object):
4074
4079
  self.encoded = clauses
4075
4080
  self.auxvars = auxvars
4076
4081
  self.enclits = enclits
4077
- self.nv = self.name
4082
+
4083
+ # the name of a single unit clause is its literal, which can be
4084
+ # negative; nv must stay the largest variable id in any case
4085
+ self.nv = abs(self.name)
4078
4086
 
4079
4087
  def _atoms(self, dest):
4080
4088
  """
@@ -4614,11 +4622,11 @@ class WCNF(object):
4614
4622
  soft.append(cl)
4615
4623
  else:
4616
4624
  hard.append([topv] + cl)
4617
- soft.append([topv])
4625
+ soft.append([-topv])
4618
4626
  topv += 1
4619
4627
 
4620
4628
  if format == 'opb':
4621
- print('* #variable= {0} #constraint= {1}'.format(self.nv, len(self.hard) + len(hard)),
4629
+ print('* #variable= {0} #constraint= {1}'.format(topv - 1, len(self.hard) + len(hard)),
4622
4630
  file=file_pointer)
4623
4631
  print('min:',
4624
4632
  ' '.join(['{0}{1} x{2}'.format('-' if s[0] > 0 else '+', w, abs(s[0])) for s, w in zip(soft, self.wght)]),
@@ -4630,7 +4638,7 @@ class WCNF(object):
4630
4638
  file=file_pointer)
4631
4639
  print('Subject To', file=file_pointer)
4632
4640
  elif format == 'smt':
4633
- for v in range(1, self.nv + 1):
4641
+ for v in range(1, topv):
4634
4642
  print('(declare-fun x{0} () Bool)'.format(v), file=file_pointer)
4635
4643
 
4636
4644
  for i, cl in enumerate(self.hard + hard, 1):
@@ -5096,7 +5104,7 @@ class CNFPlus(CNF, object):
5096
5104
  print(cchars[format], c, file=file_pointer)
5097
5105
 
5098
5106
  if format == 'opb':
5099
- print('* #variable= {0} #constraint= {1}'.format(self.nv, len(self.clauses)),
5107
+ print('* #variable= {0} #constraint= {1}'.format(self.nv, len(self.clauses) + len(self.atmosts)),
5100
5108
  file=file_pointer)
5101
5109
  elif format == 'lp':
5102
5110
  print('Minimize', file=file_pointer)
@@ -5742,11 +5750,11 @@ class WCNFPlus(WCNF, object):
5742
5750
  soft.append(cl)
5743
5751
  else:
5744
5752
  hard.append([topv] + cl)
5745
- soft.append([topv])
5753
+ soft.append([-topv])
5746
5754
  topv += 1
5747
5755
 
5748
5756
  if format == 'opb':
5749
- print('* #variable= {0} #constraint= {1}'.format(self.nv, len(self.hard) + len(hard)),
5757
+ print('* #variable= {0} #constraint= {1}'.format(topv - 1, len(self.hard) + len(hard) + len(self.atms)),
5750
5758
  file=file_pointer)
5751
5759
  print('min:',
5752
5760
  ' '.join(['{0}{1} x{2}'.format('-' if s[0] > 0 else '+', w, abs(s[0])) for s, w in zip(soft, self.wght)]),
@@ -5758,7 +5766,7 @@ class WCNFPlus(WCNF, object):
5758
5766
  file=file_pointer)
5759
5767
  print('Subject To', file=file_pointer)
5760
5768
  elif format == 'smt':
5761
- for v in range(1, self.nv + 1):
5769
+ for v in range(1, topv):
5762
5770
  print('(declare-fun x{0} () Bool)'.format(v), file=file_pointer)
5763
5771
 
5764
5772
  for i, cl in enumerate(self.hard + hard, 1):
@@ -157,6 +157,18 @@
157
157
  float and decimal coefficients must not be mixed in the same constraints,
158
158
  hence one cannot mix integers with float and decimal numeric modes.
159
159
 
160
+ .. warning::
161
+
162
+ Since floats are binary approximations of decimal numbers, e.g.
163
+ ``0.1`` is not exactly one tenth, so the arithmetic used to build and
164
+ propagate constraints is subject to rounding. As a result, constraints
165
+ with fractional float coefficients or bounds may be decided
166
+ incorrectly, especially if they are reified. (Integer coefficients are
167
+ exact in both modes.) For fractional coefficients, use
168
+ ``numeric='decimal'`` with :class:`decimal.Decimal` values, e.g.
169
+ ``Decimal('0.1')``, which are exact as long as the numbers fit the
170
+ precision of the decimal context (28 significant digits by default).
171
+
160
172
  Below is a compact 4x4 Sudoku example (digits 1..4). It demonstrates
161
173
  direct-encoded integer variables, AllDifferent constraints, and the use
162
174
  of the propagator. This is intentionally lightweight and meant to be
@@ -222,6 +234,7 @@
222
234
  #==============================================================================
223
235
  from collections import defaultdict
224
236
  from decimal import Decimal
237
+ from fractions import Fraction
225
238
  import math
226
239
  import numbers
227
240
 
@@ -972,6 +985,14 @@ class LinearExpr:
972
985
 
973
986
  return NotImplemented, None
974
987
 
988
+ def _integral(self):
989
+ """
990
+ Check if the expression can only take integer values, i.e. if
991
+ its constant and all its coefficients are integers.
992
+ """
993
+
994
+ return all(IntegerEngine._intlike(c) for c in [self.const, *self.terms.values()])
995
+
975
996
  def bounds(self):
976
997
  """
977
998
  Compute a (min, max) pair for the expression over variable domains.
@@ -1129,12 +1150,13 @@ class LinearExpr:
1129
1150
  return NotImplemented
1130
1151
 
1131
1152
  if expr:
1132
- return (self - expr) <= -1
1153
+ return (self - expr) < 0
1133
1154
 
1134
- if self.numeric == 'decimal':
1135
- return self <= val.next_minus()
1155
+ constr = self <= val
1156
+ if self._trivial(constr):
1157
+ return constr
1136
1158
 
1137
- return self <= math.nextafter(float(val), float('-inf'))
1159
+ return self._strict(constr, val)
1138
1160
 
1139
1161
  def __gt__(self, other):
1140
1162
  """
@@ -1146,12 +1168,63 @@ class LinearExpr:
1146
1168
  return NotImplemented
1147
1169
 
1148
1170
  if expr:
1149
- return (self - expr) >= 1
1171
+ return (self - expr) > 0
1150
1172
 
1151
- if self.numeric == 'decimal':
1152
- return self >= val.next_plus()
1173
+ constr = self >= val
1174
+ if self._trivial(constr):
1175
+ return constr
1176
+
1177
+ # self >= val is built as (-self) <= -val
1178
+ return (-self)._strict(constr, -val)
1179
+
1180
+ @staticmethod
1181
+ def _trivial(constr):
1182
+ """
1183
+ Check if a ``'linear'`` constraint is trivially satisfied or
1184
+ falsified, e.g. due to an infinite bound. The weights are
1185
+ non-negative, so the sum lies within ``[0, sum(weights)]``.
1186
+ """
1187
+
1188
+ _, bound, weights = constr[1]
1189
+ return bound > sum(weights.values()) or bound < 0
1190
+
1191
+ def _strict(self, constr, val):
1192
+ """
1193
+ Turn constraint ``self <= val`` into ``self < val``. The sum of
1194
+ the linearized constraint is a multiple of ``1 / den``, where
1195
+ ``den`` is the common denominator of the coefficients of
1196
+ variables with more than one value. Hence ``< val`` is the same
1197
+ as ``<=`` the largest such multiple below the exact bound.
1198
+ """
1199
+
1200
+ lits, _, weights = constr[1]
1201
+
1202
+ # integer coefficients => den is 1 and the bound is exact
1203
+ if self._integral():
1204
+ bound = math.ceil(val) - 1 - self.const
1205
+ for var, coeff in self.terms.items():
1206
+ bound -= min(coeff * var.lb, coeff * var.ub)
1153
1207
 
1154
- return self >= math.nextafter(float(val), float('inf'))
1208
+ return ('linear', [lits, bound, weights])
1209
+
1210
+ # exact bound, i.e. val - const + shift
1211
+ bound, den = Fraction(val) - Fraction(self.const), 1
1212
+ for var, coeff in self.terms.items():
1213
+ coeff = Fraction(coeff)
1214
+ bound -= min(coeff * var.lb, coeff * var.ub)
1215
+ if var.lb != var.ub:
1216
+ den = den * coeff.denominator // math.gcd(den, coeff.denominator)
1217
+
1218
+ # computing the largest multiple of 1 / den below the bound
1219
+ bound = Fraction(math.ceil(bound * den) - 1, den)
1220
+ if bound.denominator == 1:
1221
+ bound = int(bound)
1222
+ elif self.numeric == 'decimal':
1223
+ bound = Decimal(bound.numerator) / bound.denominator
1224
+ else:
1225
+ bound = float(bound)
1226
+
1227
+ return ('linear', [lits, bound, weights])
1155
1228
 
1156
1229
  def __eq__(self, other):
1157
1230
  """
@@ -1714,9 +1787,9 @@ class IntegerEngine(BooleanEngine):
1714
1787
  lkeys = tuple(sorted(self._linear_key(c)[:3] for c in constr[1]))
1715
1788
  return self._add_lin_list(constr[1], ('lin_eq', lkeys), reified)
1716
1789
 
1717
- # ne_linear: (left - right <= -1) OR (left - right >= 1)
1790
+ # ne_linear: (left - right < 0) OR (left - right > 0)
1718
1791
  diff = constr[1] - constr[2]
1719
- c1, c2 = diff <= -1, diff >= 1
1792
+ c1, c2 = diff < 0, diff > 0
1720
1793
  lks = sorted([self._linear_key(c1)[:3], self._linear_key(c2)[:3]])
1721
1794
 
1722
1795
  key = ('lin_ne', tuple(lks))
@@ -1762,6 +1835,18 @@ class IntegerEngine(BooleanEngine):
1762
1835
  return False
1763
1836
 
1764
1837
  self._lcons_seen.add(key)
1838
+
1839
+ # trivial constraints, e.g. with an infinite bound or no literals
1840
+ _, wtup, bound = key
1841
+ if bound < 0:
1842
+ # the weights are non-negative => unsatisfiable
1843
+ self._add_clause([])
1844
+ return False
1845
+
1846
+ if not wtup or bound > sum(wtup):
1847
+ # the constraint is unfalsifiable
1848
+ return False
1849
+
1765
1850
  self._lcons.append(constr)
1766
1851
  return True
1767
1852
 
@@ -1834,14 +1919,9 @@ class IntegerEngine(BooleanEngine):
1834
1919
  return
1835
1920
 
1836
1921
  rkey = ('lin_le', (ltup, wtup, bound))
1837
- if self.vpool is None:
1838
- self.vpool = Formula.export_vpool()
1839
-
1840
- if rkey in self.vpool.obj2id:
1841
- return self.vpool.id(rkey)
1842
-
1843
1922
  selv = self._get_selv(rkey)
1844
- self._guard_linear(selv, parts)
1923
+ if self._is_new_constr(rkey, selv):
1924
+ self._guard_linear(selv, parts)
1845
1925
  return selv
1846
1926
 
1847
1927
  def _reify_lin_conj(self, conss, key):
@@ -2290,14 +2370,22 @@ class IntegerEngine(BooleanEngine):
2290
2370
  continue
2291
2371
 
2292
2372
  # the weights are not all the same => PB constraint
2293
- # first checking the bound and literal weights for being int-like
2294
- if not self._intlike(bound):
2295
- raise ValueError('PB encoding requires integer bound')
2296
2373
  if any(l not in weights for l in lits):
2297
2374
  raise ValueError('PB encoding requires explicit weights for all literals')
2298
2375
  wlist = [weights[l] for l in lits]
2299
- if not all(self._intlike(w) for w in wlist):
2300
- raise ValueError('PB encoding requires integer weights')
2376
+
2377
+ # fractional decimals are scaled to integers exactly
2378
+ if not self._intlike(bound) or not all(self._intlike(w) for w in wlist):
2379
+ if any(isinstance(v, float) for v in wlist + [bound]):
2380
+ raise ValueError('PB encoding requires integer bound and weights')
2381
+
2382
+ den = 1
2383
+ for w in wlist:
2384
+ d = Fraction(w).denominator
2385
+ den = den * d // math.gcd(den, d)
2386
+
2387
+ bound = math.floor(Fraction(bound) * den)
2388
+ wlist = [int(Fraction(w) * den) for w in wlist]
2301
2389
 
2302
2390
  bound_int = int(bound) if not isinstance(bound, Decimal) else int(bound)
2303
2391
  wlist = [int(w) if not isinstance(w, Decimal) else int(w)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-sat
3
- Version: 1.9.dev25
3
+ Version: 1.9.dev26
4
4
  Summary: A Python library for prototyping with SAT oracles
5
5
  Home-page: https://github.com/pysathq/pysat
6
6
  Author: Alexey Ignatiev, Joao Marques-Silva, Antonio Morgado
@@ -101,6 +101,7 @@ solvers/patches/minisatep.patch
101
101
  solvers/patches/minisatgh.patch
102
102
  tests/test_accum_stats.py
103
103
  tests/test_activities.py
104
+ tests/test_alien.py
104
105
  tests/test_atmost.py
105
106
  tests/test_atmost1.py
106
107
  tests/test_atmostk.py
@@ -118,13 +119,20 @@ tests/test_hitman.py
118
119
  tests/test_idpool.py
119
120
  tests/test_integer.py
120
121
  tests/test_iseqcounter.py
122
+ tests/test_itotalizer.py
123
+ tests/test_lbx.py
121
124
  tests/test_lsu.py
125
+ tests/test_mcsls.py
126
+ tests/test_musx.py
127
+ tests/test_negate.py
122
128
  tests/test_oom.py
123
129
  tests/test_pb.py
130
+ tests/test_primer.py
124
131
  tests/test_process.py
125
132
  tests/test_projected_models.py
126
133
  tests/test_proof.py
127
134
  tests/test_propagate.py
135
+ tests/test_rc2.py
128
136
  tests/test_unique_model.py
129
137
  tests/test_unique_mus.py
130
138
  tests/test_vector.py
@@ -1110,7 +1110,17 @@ static void sigint_handler(int signum)
1110
1110
  //=============================================================================
1111
1111
  static int pyint_to_cint(PyObject *i_obj)
1112
1112
  {
1113
- return PyLong_AsLong(i_obj);
1113
+ long v = PyLong_AsLong(i_obj);
1114
+
1115
+ if (v == -1 && PyErr_Occurred())
1116
+ return 0;
1117
+
1118
+ if (v > INT_MAX || v < -INT_MAX) {
1119
+ PyErr_SetString(PyExc_ValueError, "integer out of range");
1120
+ return 0;
1121
+ }
1122
+
1123
+ return (int)v;
1114
1124
  }
1115
1125
 
1116
1126
  // PyInt_fromLong()
@@ -0,0 +1,42 @@
1
+ import io
2
+ from pysat.formula import CNFPlus, WCNF, WCNFPlus
3
+
4
+ def dump(formula, format):
5
+ fp = io.StringIO()
6
+ formula.to_alien(fp, format=format)
7
+ return fp.getvalue().splitlines()
8
+
9
+ def test_wcnf_soft_clause_relaxation():
10
+ wcnf = WCNF()
11
+ wcnf.append([1, 2])
12
+ wcnf.append([-1, -2], weight=5)
13
+
14
+ opb = dump(wcnf, 'opb')
15
+ assert opb[0] == '* #variable= 3 #constraint= 2'
16
+ assert opb[1] == 'min: +5 x3 ;'
17
+ assert opb[3] == '+1 x3 -1 x1 -1 x2 >= -1 ;'
18
+
19
+ lp = dump(wcnf, 'lp')
20
+ assert lp[1] == 'obj: +5 x3'
21
+
22
+ smt = dump(wcnf, 'smt')
23
+ assert '(declare-fun x3 () Bool)' in smt
24
+ assert '(assert-soft (not x3) :weight 5)' in smt
25
+
26
+ def test_wcnfplus_soft_clause_relaxation():
27
+ wcnf = WCNFPlus()
28
+ wcnf.append([1, 2])
29
+ wcnf.append([-1, -2], weight=5)
30
+ wcnf.append([[1, 2], 1], is_atmost=True)
31
+
32
+ opb = dump(wcnf, 'opb')
33
+ assert opb[0] == '* #variable= 3 #constraint= 3'
34
+ assert opb[1] == 'min: +5 x3 ;'
35
+
36
+ def test_cnfplus_opb_header():
37
+ cnf = CNFPlus()
38
+ cnf.append([1])
39
+ cnf.append([[1, 2, 3], 2], is_atmost=True)
40
+
41
+ opb = dump(cnf, 'opb')
42
+ assert opb[0] == '* #variable= 3 #constraint= 2'
@@ -1,3 +1,4 @@
1
+ from decimal import Decimal
1
2
  from pysat.card import *
2
3
  from pysat.engines import *
3
4
  from pysat.formula import CNF, CNFPlus
@@ -95,3 +96,20 @@ def test_native_card_bootstrap(solver_name):
95
96
  nof_models = sum(1 for model in solver.enum_models())
96
97
 
97
98
  assert nof_models == encoded_count(solver_name, list(range(1, 6)))
99
+
100
+
101
+ @pytest.mark.parametrize('solver_name', ['cadical195', 'cadical300', 'minisatep'])
102
+ @pytest.mark.parametrize('bound, sat', [(Decimal('-0.5'), False), (Decimal('Infinity'), True)])
103
+ def test_decimal_bound(solver_name, bound, sat):
104
+ """
105
+ A Decimal bound has to be rounded down, not towards zero, when the
106
+ constraint is reduced to a cardinality one.
107
+ """
108
+
109
+ weights = {1: Decimal('1.5'), 2: Decimal('1.5')}
110
+ engine = BooleanEngine([('linear', [[1, 2], bound, weights])])
111
+
112
+ with Solver(name=solver_name, bootstrap_with=[[-1], [-2]]) as solver:
113
+ solver.connect_propagator(engine)
114
+ engine.setup_observe(solver)
115
+ assert solver.solve() == sat
@@ -179,6 +179,105 @@ def test_operator_constraints(solver_name):
179
179
  assert _enum_models_cnf(eng, vars) == expected, 'Operator constraints mismatch [solver]'
180
180
 
181
181
 
182
+ @pytest.mark.parametrize('solver_name', engine_solvers)
183
+ @pytest.mark.parametrize('encoding', ['direct', 'order', 'coupled'])
184
+ def test_strict_constant_bounds(solver_name, encoding):
185
+ x = Integer('x', 0, 4, encoding=encoding)
186
+ eng = IntegerEngine([x], adaptive=False)
187
+
188
+ eng.add_linear(x > 1)
189
+ eng.add_linear(2 * x < 7)
190
+
191
+ vars = [x]
192
+ expected = {(2,), (3,)}
193
+ assert _enum_models_engine(eng, vars, solver_name) == expected, 'Strict bounds mismatch [engine]'
194
+ assert _enum_models_cnf(eng, vars) == expected, 'Strict bounds mismatch [solver]'
195
+
196
+
197
+ @pytest.mark.parametrize('solver_name', engine_solvers)
198
+ @pytest.mark.parametrize('numeric', ['float', 'decimal'])
199
+ def test_strict_fractional_bounds(solver_name, numeric):
200
+ half = Decimal('0.5') if numeric == 'decimal' else 0.5
201
+ x = Integer('x', 0, 3, encoding='order', numeric=numeric)
202
+ y = Integer('y', -1, 2, encoding='order', numeric=numeric)
203
+ eng = IntegerEngine([x, y], adaptive=False)
204
+
205
+ # order encoding => common weight 0.5, hence clausifiable
206
+ eng.add_linear(half * x > 0)
207
+ eng.add_linear(half * x < half * y)
208
+
209
+ vars = [x, y]
210
+ expected = {(1, 2)}
211
+ assert _enum_models_engine(eng, vars, solver_name) == expected, 'Strict fractional bounds mismatch [engine]'
212
+ assert _enum_models_cnf(eng, vars) == expected, 'Strict fractional bounds mismatch [solver]'
213
+
214
+ # direct encoding => weights 0.5, 1, 1.5, 2; the
215
+ # strict bound has to survive the reification
216
+ z = Integer('z', 0, 4, numeric=numeric)
217
+ eng = IntegerEngine([z], adaptive=False)
218
+ sel = eng.add_linear(half * z > 1, reified=True)
219
+
220
+ expected = {(3,), (4,)}
221
+ assert _enum_models_engine_assuming(eng, [z], [sel], solver_name) == expected, 'Reified strict bound mismatch [engine]'
222
+
223
+ # a fractional coefficient of a single-value
224
+ # variable => integer weights and bound
225
+ w = Integer('w', 1, 1, numeric=numeric)
226
+ eng = IntegerEngine([z, w], adaptive=False)
227
+ eng.add_linear(z + 3 * half * w < 2 + 3 * half)
228
+
229
+ expected = {(0, 1), (1, 1)}
230
+ assert _enum_models_engine(eng, [z, w], solver_name) == expected, 'Single-value strict bound mismatch [engine]'
231
+ assert _enum_models_cnf(eng, [z, w]) == expected, 'Single-value strict bound mismatch [solver]'
232
+
233
+
234
+ @pytest.mark.parametrize('solver_name', engine_solvers)
235
+ def test_fractional_decimal(solver_name):
236
+ tenth = Decimal('0.1')
237
+ x = Integer('x', 0, 3, encoding='order', numeric='decimal')
238
+ y = Integer('y', 0, 3, numeric='decimal')
239
+
240
+ # reified => weights 0.1 and that of the selector
241
+ eng = IntegerEngine([x], adaptive=False)
242
+ sel = eng.add_linear(tenth * x > tenth, reified=True)
243
+
244
+ expected = {(2,), (3,)}
245
+ assert _enum_models_engine_assuming(eng, [x], [sel], solver_name) == expected, 'Reified decimal bound mismatch [engine]'
246
+
247
+ # the difference of the two sides is not an integer
248
+ eng = IntegerEngine([x, y], adaptive=False)
249
+ eng.add_linear(tenth * x != tenth * y)
250
+
251
+ expected = {(a, b) for a in range(4) for b in range(4) if a != b}
252
+ assert _enum_models_engine(eng, [x, y], solver_name) == expected, 'Fractional != mismatch [engine]'
253
+ assert _enum_models_cnf(eng, [x, y]) == expected, 'Fractional != mismatch [solver]'
254
+
255
+
256
+ @pytest.mark.parametrize('solver_name', engine_solvers)
257
+ def test_trivial_constraints(solver_name):
258
+ x = Integer('x', 0, 2)
259
+ y = Integer('y', 1, 1)
260
+ vars = [x, y]
261
+ inf = float('inf')
262
+
263
+ # unfalsifiable constraints, incl. those over a single-value domain
264
+ eng = IntegerEngine(vars, adaptive=False)
265
+ for constr in [x < inf, x <= inf, x > -inf, y <= 1, x + y <= 2]:
266
+ eng.add_linear(constr)
267
+
268
+ expected = {(0, 1), (1, 1)}
269
+ assert _enum_models_engine(eng, vars, solver_name) == expected, 'Trivial constraints mismatch [engine]'
270
+ assert _enum_models_cnf(eng, vars) == expected, 'Trivial constraints mismatch [solver]'
271
+
272
+ # unsatisfiable constraints
273
+ for constr in [x > inf, x < -inf, y >= 2]:
274
+ eng = IntegerEngine(vars, adaptive=False)
275
+ eng.add_linear(constr)
276
+
277
+ assert _enum_models_engine(eng, vars, solver_name) == set(), 'Unsatisfiable constraint mismatch [engine]'
278
+ assert _enum_models_cnf(eng, vars) == set(), 'Unsatisfiable constraint mismatch [solver]'
279
+
280
+
182
281
  @pytest.mark.parametrize('solver_name', engine_solvers)
183
282
  def test_abs_eq_constraint(solver_name):
184
283
  x = Integer('x', -2, 2)
@@ -695,3 +794,19 @@ def test_clausify_rounds_decimal_bounds_down():
695
794
  x = Integer('x', 0, 3, encoding='order', numeric='decimal')
696
795
  eng = IntegerEngine([x], [3 * x <= Decimal(-2)])
697
796
  assert _enum_models_cnf(eng, [x]) == set()
797
+
798
+
799
+ @pytest.mark.parametrize('solver_name', engine_solvers)
800
+ def test_reified_constraints_in_second_engine(solver_name):
801
+ x = Integer('x', 0, 1)
802
+ y = Integer('y', 0, 1)
803
+
804
+ vars = [x, y]
805
+ expected = {(0, 1), (1, 0)}
806
+
807
+ for _ in range(2):
808
+ eng = IntegerEngine([x, y], adaptive=False)
809
+ eng.add_linear(x - y != 0)
810
+
811
+ assert _enum_models_engine(eng, vars, solver_name) == expected, 'Second engine mismatch [engine]'
812
+ assert _enum_models_cnf(eng, vars) == expected, 'Second engine mismatch [solver]'