ruby-minisat 1.14.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -21
  3. data/Gemfile +4 -0
  4. data/LICENSE +3 -2
  5. data/README.md +42 -0
  6. data/Rakefile +1 -48
  7. data/examples/shikaku.rb +2 -2
  8. data/ext/minisat/extconf.rb +19 -4
  9. data/ext/minisat/minisat-wrap.cpp +11 -5
  10. data/ext/minisat/minisat.c +17 -3
  11. data/ext/minisat/minisat.h +1 -1
  12. data/minisat/minisat-2.2.0.tar.gz +0 -0
  13. data/minisat/{MiniSat_v1.14 → minisat}/LICENSE +2 -1
  14. data/minisat/minisat/README +24 -0
  15. data/minisat/minisat/core/Dimacs.h +89 -0
  16. data/minisat/minisat/core/Main.cc +192 -0
  17. data/minisat/minisat/core/Solver.cc +923 -0
  18. data/minisat/minisat/core/Solver.h +373 -0
  19. data/minisat/minisat/core/SolverTypes.h +407 -0
  20. data/minisat/minisat/mtl/Alg.h +84 -0
  21. data/minisat/minisat/mtl/Alloc.h +131 -0
  22. data/minisat/minisat/mtl/Heap.h +148 -0
  23. data/minisat/minisat/mtl/IntTypes.h +42 -0
  24. data/minisat/minisat/mtl/Map.h +193 -0
  25. data/minisat/minisat/mtl/Queue.h +69 -0
  26. data/minisat/{MiniSat_v1.14 → minisat/mtl}/Sort.h +14 -47
  27. data/minisat/minisat/mtl/Vec.h +130 -0
  28. data/minisat/minisat/mtl/XAlloc.h +45 -0
  29. data/minisat/minisat/mtl/config.mk +6 -0
  30. data/minisat/minisat/mtl/template.mk +107 -0
  31. data/minisat/minisat/simp/Main.cc +211 -0
  32. data/minisat/minisat/simp/SimpSolver.cc +717 -0
  33. data/minisat/minisat/simp/SimpSolver.h +197 -0
  34. data/minisat/minisat/utils/Options.cc +91 -0
  35. data/minisat/minisat/utils/Options.h +386 -0
  36. data/minisat/minisat/utils/ParseUtils.h +122 -0
  37. data/minisat/minisat/utils/System.cc +95 -0
  38. data/minisat/minisat/utils/System.h +60 -0
  39. data/ruby-minisat.gemspec +21 -0
  40. metadata +94 -75
  41. data/README.rdoc +0 -56
  42. data/minisat/MiniSat_v1.14.2006-Aug-29.src.zip +0 -0
  43. data/minisat/MiniSat_v1.14/Global.h +0 -274
  44. data/minisat/MiniSat_v1.14/Heap.h +0 -100
  45. data/minisat/MiniSat_v1.14/Main.C +0 -244
  46. data/minisat/MiniSat_v1.14/Makefile +0 -88
  47. data/minisat/MiniSat_v1.14/README +0 -30
  48. data/minisat/MiniSat_v1.14/Solver.C +0 -781
  49. data/minisat/MiniSat_v1.14/Solver.h +0 -206
  50. data/minisat/MiniSat_v1.14/Solver.o +0 -0
  51. data/minisat/MiniSat_v1.14/SolverTypes.h +0 -130
  52. data/minisat/MiniSat_v1.14/TODO +0 -73
  53. data/minisat/MiniSat_v1.14/VarOrder.h +0 -96
@@ -1,56 +0,0 @@
1
- = ruby-minisat
2
-
3
- ruby-minisat is ruby binding for MiniSat, which is an open-source SAT solver.
4
-
5
- == Installation
6
-
7
- $ gem install ruby-minisat
8
-
9
- == How to Use
10
-
11
- A brief example that solves a simple SAT problem:
12
-
13
- # solve (a or b) and (not a or b) and (a or not b)
14
-
15
- require "minisat"
16
- solver = MiniSat::Solver.new
17
-
18
- a = solver.new_var
19
- b = solver.new_var
20
-
21
- solver << [a, b] << [-a, b] << [a, -b]
22
-
23
- p solver.solve #=> true (satisfiable)
24
-
25
- p solver[a] #=> true
26
- p solver[b] #=> true
27
-
28
- For more examples, see the examples directory in the distribution.
29
-
30
- == Copyright
31
-
32
- ruby-minisat is covered under the MIT License.
33
- This package includes MiniSat in the directory minisat/*, which is also
34
- distributed under the MIT License.
35
-
36
-
37
- MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
38
- ruby-minisat -- Copyright (c) 2007,2010 Yusuke Endoh
39
-
40
- Permission is hereby granted, free of charge, to any person obtaining a copy
41
- of this software and associated documentation files (the "Software"), to deal
42
- in the Software without restriction, including without limitation the rights
43
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44
- copies of the Software, and to permit persons to whom the Software is
45
- furnished to do so, subject to the following conditions:
46
-
47
- The above copyright notice and this permission notice shall be included in
48
- all copies or substantial portions of the Software.
49
-
50
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
56
- THE SOFTWARE.
@@ -1,274 +0,0 @@
1
- /****************************************************************************************[Global.h]
2
- MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5
- associated documentation files (the "Software"), to deal in the Software without restriction,
6
- including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all copies or
11
- substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14
- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17
- OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
- **************************************************************************************************/
19
-
20
- #ifndef Global_h
21
- #define Global_h
22
-
23
- #include <cassert>
24
- #include <cstdio>
25
- #include <cstdlib>
26
- #include <cstring>
27
- #include <climits>
28
- #include <cfloat>
29
- #include <new>
30
-
31
-
32
- //=================================================================================================
33
- // Basic Types & Minor Things:
34
-
35
-
36
- #ifdef _MSC_VER
37
- typedef INT64 int64;
38
- typedef UINT64 uint64;
39
- typedef INT_PTR intp;
40
- typedef UINT_PTR uintp;
41
- #define I64_fmt "I64d"
42
- #else
43
- typedef long long int64;
44
- typedef unsigned long long uint64;
45
- typedef __PTRDIFF_TYPE__ intp;
46
- typedef unsigned __PTRDIFF_TYPE__ uintp;
47
- #define I64_fmt "lld"
48
- #endif
49
- typedef unsigned char uchar;
50
- typedef const char cchar;
51
-
52
-
53
- template<class T> static inline T min(T x, T y) { return (x < y) ? x : y; }
54
- template<class T> static inline T max(T x, T y) { return (x > y) ? x : y; }
55
-
56
- template <bool> struct STATIC_ASSERTION_FAILURE;
57
- template <> struct STATIC_ASSERTION_FAILURE<true>{};
58
- #define TEMPLATE_FAIL STATIC_ASSERTION_FAILURE<false>()
59
-
60
-
61
- //=================================================================================================
62
- // 'malloc()'-style memory allocation -- never returns NULL; aborts instead:
63
-
64
-
65
- template<class T> static inline T* xmalloc(size_t size) {
66
- T* tmp = (T*)malloc(size * sizeof(T));
67
- assert(size == 0 || tmp != NULL);
68
- return tmp; }
69
-
70
- template<class T> static inline T* xrealloc(T* ptr, size_t size) {
71
- T* tmp = (T*)realloc((void*)ptr, size * sizeof(T));
72
- assert(size == 0 || tmp != NULL);
73
- return tmp; }
74
-
75
- template<class T> static inline void xfree(T *ptr) {
76
- if (ptr != NULL) free((void*)ptr); }
77
-
78
-
79
- //=================================================================================================
80
- // Random numbers:
81
-
82
-
83
- // Returns a random float 0 <= x < 1. Seed must never be 0.
84
- static inline double drand(double& seed) {
85
- seed *= 1389796;
86
- int q = (int)(seed / 2147483647);
87
- seed -= (double)q * 2147483647;
88
- return seed / 2147483647; }
89
-
90
- // Returns a random integer 0 <= x < size. Seed must never be 0.
91
- static inline int irand(double& seed, int size) {
92
- return (int)(drand(seed) * size); }
93
-
94
-
95
- //=================================================================================================
96
- // Time and Memory:
97
-
98
-
99
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100
- #ifdef _MSC_VER
101
-
102
- #include <ctime>
103
-
104
- static inline double cpuTime(void) {
105
- return (double)clock() / CLOCKS_PER_SEC; }
106
-
107
- static inline int64 memUsed() {
108
- return 0; }
109
-
110
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111
- #else
112
-
113
- #include <sys/time.h>
114
- #include <sys/resource.h>
115
- #include <unistd.h>
116
-
117
- static inline double cpuTime(void) {
118
- struct rusage ru;
119
- getrusage(RUSAGE_SELF, &ru);
120
- return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000; }
121
-
122
- static inline int memReadStat(int field)
123
- {
124
- char name[256];
125
- pid_t pid = getpid();
126
- sprintf(name, "/proc/%d/statm", pid);
127
- FILE* in = fopen(name, "rb");
128
- if (in == NULL) return 0;
129
- int value;
130
- for (; field >= 0; field--)
131
- fscanf(in, "%d", &value);
132
- fclose(in);
133
- return value;
134
- }
135
-
136
- static inline int64 memUsed() { return (int64)memReadStat(0) * (int64)getpagesize(); }
137
-
138
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
- #endif
140
-
141
-
142
-
143
- //=================================================================================================
144
- // 'vec' -- automatically resizable arrays (via 'push()' method):
145
-
146
-
147
- // NOTE! Don't use this vector on datatypes that cannot be re-located in memory (with realloc)
148
-
149
- template<class T>
150
- class vec {
151
- T* data;
152
- int sz;
153
- int cap;
154
-
155
- void init(int size, const T& pad);
156
- void grow(int min_cap);
157
-
158
- public:
159
- // Types:
160
- typedef int Key;
161
- typedef T Datum;
162
-
163
- // Constructors:
164
- vec(void) : data(NULL) , sz(0) , cap(0) { }
165
- vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); }
166
- vec(int size, const T& pad) : data(NULL) , sz(0) , cap(0) { growTo(size, pad); }
167
- vec(T* array, int size) : data(array), sz(size), cap(size) { } // (takes ownership of array -- will be deallocated with 'xfree()')
168
- ~vec(void) { clear(true); }
169
-
170
- // Ownership of underlying array:
171
- T* release (void) { T* ret = data; data = NULL; sz = 0; cap = 0; return ret; }
172
- operator T* (void) { return data; } // (unsafe but convenient)
173
- operator const T* (void) const { return data; }
174
-
175
- // Size operations:
176
- int size (void) const { return sz; }
177
- void shrink (int nelems) { assert(nelems <= sz); for (int i = 0; i < nelems; i++) sz--, data[sz].~T(); }
178
- void pop (void) { sz--, data[sz].~T(); }
179
- void growTo (int size);
180
- void growTo (int size, const T& pad);
181
- void clear (bool dealloc = false);
182
- void capacity (int size) { grow(size); }
183
-
184
- // Stack interface:
185
- void push (void) { if (sz == cap) grow(sz+1); new (&data[sz]) T() ; sz++; }
186
- void push (const T& elem) { if (sz == cap) grow(sz+1); new (&data[sz]) T(elem); sz++; }
187
- const T& last (void) const { return data[sz-1]; }
188
- T& last (void) { return data[sz-1]; }
189
-
190
- // Vector interface:
191
- const T& operator [] (int index) const { return data[index]; }
192
- T& operator [] (int index) { return data[index]; }
193
-
194
- // Don't allow copying (error prone):
195
- vec<T>& operator = (vec<T>& other) { TEMPLATE_FAIL; }
196
- vec (vec<T>& other) { TEMPLATE_FAIL; }
197
-
198
- // Duplicatation (preferred instead):
199
- void copyTo(vec<T>& copy) const { copy.clear(); copy.growTo(sz); for (int i = 0; i < sz; i++) new (&copy[i]) T(data[i]); }
200
- void moveTo(vec<T>& dest) { dest.clear(true); dest.data = data; dest.sz = sz; dest.cap = cap; data = NULL; sz = 0; cap = 0; }
201
- };
202
-
203
- template<class T>
204
- void vec<T>::grow(int min_cap) {
205
- if (min_cap <= cap) return;
206
- if (cap == 0) cap = (min_cap >= 2) ? min_cap : 2;
207
- else do cap = (cap*3+1) >> 1; while (cap < min_cap);
208
- data = xrealloc(data, cap); }
209
-
210
- template<class T>
211
- void vec<T>::growTo(int size, const T& pad) {
212
- if (sz >= size) return;
213
- grow(size);
214
- for (int i = sz; i < size; i++) new (&data[i]) T(pad);
215
- sz = size; }
216
-
217
- template<class T>
218
- void vec<T>::growTo(int size) {
219
- if (sz >= size) return;
220
- grow(size);
221
- for (int i = sz; i < size; i++) new (&data[i]) T();
222
- sz = size; }
223
-
224
- template<class T>
225
- void vec<T>::clear(bool dealloc) {
226
- if (data != NULL){
227
- for (int i = 0; i < sz; i++) data[i].~T();
228
- sz = 0;
229
- if (dealloc) xfree(data), data = NULL, cap = 0; } }
230
-
231
-
232
- //=================================================================================================
233
- // Lifted booleans:
234
-
235
-
236
- class lbool {
237
- int value;
238
- explicit lbool(int v) : value(v) { }
239
-
240
- public:
241
- lbool() : value(0) { }
242
- lbool(bool x) : value((int)x*2-1) { }
243
- int toInt(void) const { return value; }
244
-
245
- bool operator == (const lbool& other) const { return value == other.value; }
246
- bool operator != (const lbool& other) const { return value != other.value; }
247
- lbool operator ~ (void) const { return lbool(-value); }
248
-
249
- friend int toInt (lbool l);
250
- friend lbool toLbool(int v);
251
- };
252
- inline int toInt (lbool l) { return l.toInt(); }
253
- inline lbool toLbool(int v) { return lbool(v); }
254
-
255
- const lbool l_True = toLbool( 1);
256
- const lbool l_False = toLbool(-1);
257
- const lbool l_Undef = toLbool( 0);
258
-
259
-
260
- //=================================================================================================
261
- // Relation operators -- extend definitions from '==' and '<'
262
-
263
-
264
- #ifndef __SGI_STL_INTERNAL_RELOPS // (be aware of SGI's STL implementation...)
265
- #define __SGI_STL_INTERNAL_RELOPS
266
- template <class T> static inline bool operator != (const T& x, const T& y) { return !(x == y); }
267
- template <class T> static inline bool operator > (const T& x, const T& y) { return y < x; }
268
- template <class T> static inline bool operator <= (const T& x, const T& y) { return !(y < x); }
269
- template <class T> static inline bool operator >= (const T& x, const T& y) { return !(x < y); }
270
- #endif
271
-
272
-
273
- //=================================================================================================
274
- #endif
@@ -1,100 +0,0 @@
1
- /******************************************************************************************[Heap.h]
2
- MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5
- associated documentation files (the "Software"), to deal in the Software without restriction,
6
- including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all copies or
11
- substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14
- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17
- OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
- **************************************************************************************************/
19
-
20
- #ifndef Heap_h
21
- #define Heap_h
22
-
23
-
24
- //=================================================================================================
25
-
26
-
27
- static inline int left (int i) { return i+i; }
28
- static inline int right (int i) { return i+i + 1; }
29
- static inline int parent(int i) { return i >> 1; }
30
-
31
- template<class C>
32
- class Heap {
33
- public:
34
- C comp;
35
- vec<int> heap; // heap of ints
36
- vec<int> indices; // int -> index in heap
37
-
38
- inline void percolateUp(int i)
39
- {
40
- int x = heap[i];
41
- while (parent(i) != 0 && comp(x,heap[parent(i)])){
42
- heap[i] = heap[parent(i)];
43
- indices[heap[i]] = i;
44
- i = parent(i);
45
- }
46
- heap [i] = x;
47
- indices[x] = i;
48
- }
49
-
50
- inline void percolateDown(int i)
51
- {
52
- int x = heap[i];
53
- while (left(i) < heap.size()){
54
- int child = right(i) < heap.size() && comp(heap[right(i)],heap[left(i)]) ? right(i) : left(i);
55
- if (!comp(heap[child],x)) break;
56
- heap[i] = heap[child];
57
- indices[heap[i]] = i;
58
- i = child;
59
- }
60
- heap [i] = x;
61
- indices[x] = i;
62
- }
63
-
64
- bool ok(int n) { return n >= 0 && n < (int)indices.size(); }
65
-
66
- public:
67
- Heap(C c) : comp(c) { heap.push(-1); }
68
-
69
- void setBounds (int size) { assert(size >= 0); indices.growTo(size,0); }
70
- bool inHeap (int n) { assert(ok(n)); return indices[n] != 0; }
71
- void increase (int n) { assert(ok(n)); assert(inHeap(n)); percolateUp(indices[n]); }
72
- bool empty () { return heap.size() == 1; }
73
-
74
- void insert(int n) {
75
- assert(ok(n));
76
- indices[n] = heap.size();
77
- heap.push(n);
78
- percolateUp(indices[n]); }
79
-
80
- int getmin() {
81
- int r = heap[1];
82
- heap[1] = heap.last();
83
- indices[heap[1]] = 1;
84
- indices[r] = 0;
85
- heap.pop();
86
- if (heap.size() > 1)
87
- percolateDown(1);
88
- return r; }
89
-
90
- bool heapProperty() {
91
- return heapProperty(1); }
92
-
93
- bool heapProperty(int i) {
94
- return (size_t)i >= heap.size()
95
- || ((parent(i) == 0 || !comp(heap[i],heap[parent(i)])) && heapProperty(left(i)) && heapProperty(right(i))); }
96
- };
97
-
98
-
99
- //=================================================================================================
100
- #endif
@@ -1,244 +0,0 @@
1
- /******************************************************************************************[Main.C]
2
- MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5
- associated documentation files (the "Software"), to deal in the Software without restriction,
6
- including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all copies or
11
- substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14
- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17
- OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
- **************************************************************************************************/
19
-
20
- #include "Solver.h"
21
- #include <ctime>
22
- #include <unistd.h>
23
- #include <signal.h>
24
- #include <zlib.h>
25
-
26
-
27
- //=================================================================================================
28
- // BCNF Parser:
29
-
30
-
31
- #define CHUNK_LIMIT 1048576
32
-
33
- static void parse_BCNF(cchar* filename, Solver& S)
34
- {
35
- FILE* in = fopen(filename, "rb");
36
- if (in == NULL) fprintf(stderr, "ERROR! Could not open file: %s\n", filename), exit(1);
37
-
38
- char header[16];
39
- fread(header, 1, 16, in);
40
- if (strncmp(header, "BCNF", 4) != 0) fprintf(stderr, "ERROR! Not a BCNF file: %s\n", filename), exit(1);
41
- if (*(int*)(header+4) != 0x01020304) fprintf(stderr, "ERROR! BCNF file in unsupported byte-order: %s\n", filename), exit(1);
42
-
43
- int n_vars = *(int*)(header+ 8);
44
- //int n_clauses = *(int*)(header+12);
45
- int* buf = xmalloc<int>(CHUNK_LIMIT);
46
- int buf_sz;
47
- vec<Lit> c;
48
-
49
- for (int i = 0; i < n_vars; i++) S.newVar();
50
-
51
- for(;;){
52
- int n = fread(&buf_sz, 4, 1, in);
53
- if (n != 1) break;
54
- assert(buf_sz <= CHUNK_LIMIT);
55
- fread(buf, 4, buf_sz, in);
56
-
57
- int* p = buf;
58
- while (*p != -1){
59
- int size = *p++;
60
- c.clear();
61
- c.growTo(size);
62
- for (int i = 0; i < size; i++)
63
- c[i] = toLit(p[i]);
64
- p += size;
65
-
66
- S.addClause(c); // Add clause.
67
- if (!S.okay()){
68
- xfree(buf); fclose(in);
69
- return; }
70
- }
71
- }
72
-
73
- xfree(buf);
74
- fclose(in);
75
- }
76
-
77
-
78
- //=================================================================================================
79
- // DIMACS Parser:
80
-
81
-
82
- class StreamBuffer {
83
- gzFile in;
84
- char buf[CHUNK_LIMIT];
85
- int pos;
86
- int size;
87
-
88
- void assureLookahead() {
89
- if (pos >= size) {
90
- pos = 0;
91
- size = gzread(in, buf, sizeof(buf)); } }
92
-
93
- public:
94
- StreamBuffer(gzFile i) : in(i), pos(0), size(0) {
95
- assureLookahead(); }
96
-
97
- int operator * () { return (pos >= size) ? EOF : buf[pos]; }
98
- void operator ++ () { pos++; assureLookahead(); }
99
- };
100
-
101
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102
-
103
- template<class B>
104
- static void skipWhitespace(B& in) {
105
- while ((*in >= 9 && *in <= 13) || *in == 32)
106
- ++in; }
107
-
108
- template<class B>
109
- static void skipLine(B& in) {
110
- for (;;){
111
- if (*in == EOF) return;
112
- if (*in == '\n') { ++in; return; }
113
- ++in; } }
114
-
115
- template<class B>
116
- static int parseInt(B& in) {
117
- int val = 0;
118
- bool neg = false;
119
- skipWhitespace(in);
120
- if (*in == '-') neg = true, ++in;
121
- else if (*in == '+') ++in;
122
- if (*in < '0' || *in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
123
- while (*in >= '0' && *in <= '9')
124
- val = val*10 + (*in - '0'),
125
- ++in;
126
- return neg ? -val : val; }
127
-
128
- template<class B>
129
- static void readClause(B& in, Solver& S, vec<Lit>& lits) {
130
- int parsed_lit, var;
131
- lits.clear();
132
- for (;;){
133
- parsed_lit = parseInt(in);
134
- if (parsed_lit == 0) break;
135
- var = abs(parsed_lit)-1;
136
- while (var >= S.nVars()) S.newVar();
137
- lits.push( (parsed_lit > 0) ? Lit(var) : ~Lit(var) );
138
- }
139
- }
140
-
141
- template<class B>
142
- static void parse_DIMACS_main(B& in, Solver& S) {
143
- vec<Lit> lits;
144
- for (;;){
145
- skipWhitespace(in);
146
- if (*in == EOF)
147
- break;
148
- else if (*in == 'c' || *in == 'p')
149
- skipLine(in);
150
- else
151
- readClause(in, S, lits),
152
- S.addClause(lits);
153
- }
154
- }
155
-
156
- // Inserts problem into solver.
157
- //
158
- static void parse_DIMACS(gzFile input_stream, Solver& S) {
159
- StreamBuffer in(input_stream);
160
- parse_DIMACS_main(in, S); }
161
-
162
-
163
- //=================================================================================================
164
-
165
-
166
- void printStats(SolverStats& stats)
167
- {
168
- double cpu_time = cpuTime();
169
- int64 mem_used = memUsed();
170
- reportf("restarts : %"I64_fmt"\n", stats.starts);
171
- reportf("conflicts : %-12"I64_fmt" (%.0f /sec)\n", stats.conflicts , stats.conflicts /cpu_time);
172
- reportf("decisions : %-12"I64_fmt" (%.0f /sec)\n", stats.decisions , stats.decisions /cpu_time);
173
- reportf("propagations : %-12"I64_fmt" (%.0f /sec)\n", stats.propagations, stats.propagations/cpu_time);
174
- reportf("conflict literals : %-12"I64_fmt" (%4.2f %% deleted)\n", stats.tot_literals, (stats.max_literals - stats.tot_literals)*100 / (double)stats.max_literals);
175
- if (mem_used != 0) reportf("Memory used : %.2f MB\n", mem_used / 1048576.0);
176
- reportf("CPU time : %g s\n", cpu_time);
177
- }
178
-
179
- Solver* solver;
180
- static void SIGINT_handler(int signum) {
181
- reportf("\n"); reportf("*** INTERRUPTED ***\n");
182
- printStats(solver->stats);
183
- reportf("\n"); reportf("*** INTERRUPTED ***\n");
184
- exit(1); }
185
-
186
-
187
- //=================================================================================================
188
- // Main:
189
-
190
-
191
- int main(int argc, char** argv)
192
- {
193
- Solver S;
194
-
195
- if (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))
196
- reportf("USAGE: %s <input-file> <result-output-file>\n where the input may be either in plain/gzipped DIMACS format or in BCNF.\n", argv[0]),
197
- exit(0);
198
-
199
- if (argc >= 2 && strlen(argv[1]) >= 5 && strcmp(&argv[1][strlen(argv[1])-5], ".bcnf") == 0)
200
- parse_BCNF(argv[1], S);
201
- else{
202
- if (argc == 1)
203
- reportf("Reading from standard input... Use '-h' or '--help' for help.\n");
204
-
205
- gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
206
- if (in == NULL)
207
- fprintf(stderr, "ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]),
208
- exit(1);
209
- parse_DIMACS(in, S);
210
- gzclose(in);
211
- }
212
- FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;
213
-
214
- if (!S.okay()){
215
- if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
216
- reportf("Trivial problem\n");
217
- reportf("UNSATISFIABLE\n");
218
- exit(20);
219
- }
220
-
221
- S.verbosity = 1;
222
- solver = &S;
223
- signal(SIGINT,SIGINT_handler);
224
- signal(SIGHUP,SIGINT_handler);
225
-
226
- S.solve();
227
- printStats(S.stats);
228
- reportf("\n");
229
- reportf(S.okay() ? "SATISFIABLE\n" : "UNSATISFIABLE\n");
230
-
231
- if (res != NULL){
232
- if (S.okay()){
233
- fprintf(res, "SAT\n");
234
- for (int i = 0; i < S.nVars(); i++)
235
- if (S.model[i] != l_Undef)
236
- fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
237
- fprintf(res, " 0\n");
238
- }else
239
- fprintf(res, "UNSAT\n");
240
- fclose(res);
241
- }
242
-
243
- exit(S.okay() ? 10 : 20); // (faster than "return", which will invoke the destructor for 'Solver')
244
- }