Import PicoSAT-913
[cl-satwrap.git] / backends / picosat / picosat.h
blobb97c2483f0519e29c1e9cc62c71445808500287b
1 /****************************************************************************
2 Copyright (c) 2006 - 2009, Armin Biere, Johannes Kepler University.
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to
6 deal in the Software without restriction, including without limitation the
7 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 IN THE SOFTWARE.
21 ****************************************************************************/
23 #ifndef picosat_h_INCLUDED
24 #define picosat_h_INCLUDED
26 /*------------------------------------------------------------------------*/
28 #include <stdlib.h>
29 #include <stdio.h>
31 /*------------------------------------------------------------------------*/
32 /* These are the return values for 'picosat_sat' as for instance
33 * standardized by the output format of the SAT competition.
35 #define PICOSAT_UNKNOWN 0
36 #define PICOSAT_SATISFIABLE 10
37 #define PICOSAT_UNSATISFIABLE 20
39 /*------------------------------------------------------------------------*/
41 const char *picosat_version (void);
42 const char *picosat_config (void);
43 const char *picosat_copyright (void);
45 /*------------------------------------------------------------------------*/
46 /* You can make picosat use an external memory manager instead of the one
47 * provided by LIBC. But then you need to call these three function before
48 * 'picosat_init'. The memory manager functions here all have an additional
49 * first argument which is a pointer to the memory manager, but otherwise
50 * are supposed to work as their LIBC counter parts 'malloc', 'realloc' and
51 * 'free'. As exception the 'resize' and 'delete' function have as third
52 * argument the number of bytes of the block given as second argument.
54 void picosat_set_new (void * mgr, void * (*)(void *, size_t));
55 void picosat_set_resize (void *, void * (*)(void *, void *, size_t, size_t));
56 void picosat_set_delete (void *, void (*)(void *, void *, size_t));
58 /*------------------------------------------------------------------------*/
60 void picosat_init (void); /* constructor */
61 void picosat_reset (void); /* destructor */
63 /*------------------------------------------------------------------------*/
64 /* The following five functions are essentially parameters to 'init', and
65 * thus should be called right after 'picosat_init' before doing anything
66 * else. You should not call any of them after adding a literal.
69 /* Set output file, default is 'stdout'.
71 void picosat_set_output (FILE *);
73 /* Measure all time spent in all calls in the solver. By default only the
74 * time spent in 'picosat_sat' is measured. Enabling this function may for
75 * instance tripple the time needed to add large CNFs, since every call to
76 * 'picosat_add' will trigger a call to 'getrusage'.
78 void picosat_measure_all_calls (void);
80 /* Set the prefix used for printing verbose messages and statistics.
81 * Default is "c ".
83 void picosat_set_prefix (const char *);
85 /* The function 'picosat_set_incremental_rup_file' produces
86 * a proof trace in RUP format on the fly. The resulting RUP file may
87 * contain learned clauses that are not actual in the clausal core.
90 /* Set verbosity level. A verbosity level of 1 and above prints more and
91 * more detailed progress reports on the output file, set by
92 * 'picosat_set_output'. Verbose messages are prefixed with the string set
93 * by 'picosat_set_prefix'.
95 void picosat_set_verbosity (int new_verbosity_level);
97 /* Set default initial phase:
99 * negative = false
101 * posivie = true
103 * 0 = Jeroslow-Wang (default)
105 * After a variable has been assigned the first time, it will always
106 * be assigned the previous value if it is picked as decision variable.
107 * The initial assignment can be choosen with this function.
109 void picosat_set_global_default_phase (int);
111 /* Set next/initial phase of a particular variable if picked as decision
112 * variable. Second argument 'phase' has the following meaning:
114 * negative = next value if picked as decision variable is false
116 * positive = next value if picked as decision variable is true
118 * 0 = use global default phase as next value and
119 * assume 'lit' was never assigned
121 * Again if 'lit' is assigned afterwards through a forced assignment,
122 * then this forced assignment is the next phase if this variable is
123 * used as decision variable.
125 void picosat_set_default_phase_lit (int lit, int phase);
127 /* Allows to print to internal 'out' file from client.
129 void picosat_message (int verbosity_level, const char * fmt, ...);
131 /* Deprecated!
133 #define picosat_enable_verbosity() picosat_set_verbosity (1)
135 /* Set a seed for the random number generator. The random number generator
136 * is currently just used for generating random decisions. In our
137 * experiments having random decisions did not really help on industrial
138 * examples, but was rather helpful to randomize the solver in order to
139 * do proper benchmarking of different internal parameter sets.
141 void picosat_set_seed (unsigned random_number_generator_seed);
143 /* If you ever want to extract cores or proof traces with the current
144 * instance of PicoSAT initialized with 'picosat_init', then make sure to
145 * call 'picosat_enable_trace_generation' right after 'picosat_init'. This
146 * is not necessary if you only use 'picosat_set_incremental_rup_file'.
148 * NOTE, trace generation code is not necessarily included, e.g. if you
149 * configure picosat with full optimzation as './configure -O' or with
150 * './configure --no-trace'. This speeds up the solver slightly. Then you
151 * you do not get any results by trying to generate traces.
153 void picosat_enable_trace_generation (void);
155 /* You can dump proof traces in RUP format incrementally even without
156 * keeping the proof trace in memory. The advantage is a reduction of
157 * memory usage, but the dumped clauses do not necessarily belong to the
158 * clausal core. Beside the file the additional parameters denotes the
159 * maximal number of variables and the number of original clauses.
161 void picosat_set_incremental_rup_file (FILE * file, int m, int n);
163 /*------------------------------------------------------------------------*/
164 /* This function returns the next available unused variable index and
165 * allocates a variable for it even though this variable does not occur as
166 * assumption, nor in a clause or any other constraints. In future calls to
167 * 'picosat_sat', 'picosat_deref' and particularly for 'picosat_changed',
168 * this variable is treated as if it had been used.
170 int picosat_inc_max_var (void);
172 /*------------------------------------------------------------------------*/
173 /* If you know a good estimate on how many variables you are going to use
174 * then calling this function before adding literals will result in less
175 * resizing of the variable table. But this is just a minor optimization.
176 * Beside exactly allocating enough variables it has the same effect as
177 * calling 'picosat_inc_max_var'.
179 void picosat_adjust (int max_idx);
181 /*------------------------------------------------------------------------*/
182 /* Statistics.
184 int picosat_variables (void); /* p cnf <m> n */
185 int picosat_added_original_clauses (void); /* p cnf m <n> */
186 size_t picosat_max_bytes_allocated (void);
187 double picosat_time_stamp (void); /* ... in process */
188 void picosat_stats (void); /* > output file */
190 /* The time spent in the library or in 'picosat_sat'. The former is only
191 * returned if, right after initialization 'picosat_measure_all_calls'
192 * is called.
194 double picosat_seconds (void);
196 /*------------------------------------------------------------------------*/
197 /* Add a literal of the next clause. A zero terminates the clause. The
198 * solver is incremental. Adding a new literal will reset the previous
199 * assignment.
201 void picosat_add (int lit);
203 /* Print the CNF to the given file in DIMACS format.
205 void picosat_print (FILE *);
207 /* You can add arbitrary many assumptions before the next 'picosat_sat'.
208 * This is similar to the using assumptions in MiniSAT, except that you do
209 * not have to collect all your assumptions yourself. In PicoSAT you can
210 * add one after the other before the next call to 'picosat_sat'.
212 * These assumptions can be seen as adding unit clauses with those
213 * assumptions as literals. However these assumption clauses are only valid
214 * for exactly the next call to 'picosat_sat'. And will be removed
215 * afterwards, e.g. in future calls to 'picosat_sat' after the next one they
216 * are not assumed, unless they are assumed again trough 'picosat_assume'.
218 * More precisely, assumptions actually remain valid even after the next
219 * call to 'picosat_sat' returns. Valid means they remain 'assumed' until a
220 * call to 'picosat_add', 'picosat_assume', or another 'picosat_sat,
221 * following the first 'picosat_sat'. They need to stay valid for
222 * 'picosat_failed_assumption' to return correct values.
224 * Example:
226 * picosat_assume (1); // assume unit clause '1 0'
227 * picosat_assume (-2); // additionally assume clause '-2 0'
228 * res = picosat_sat (1000); // assumes 1 and -2 to hold
229 * // 1000 decisions max.
231 * if (res == PICOSAT_UNSATISFIABLE)
233 * if (picosat_failed_assumption (1))
234 * // unit clause '1 0' was necessary to derive UNSAT
236 * if (picosat_failed_assumption (-2))
237 * // unit clause '-2 0' was necessary to derive UNSAT
239 * // at least one but also both could be necessary
241 * picosat_assume (17); // previous assumptions are removed
242 * // now assume unit clause '17 0' for
243 * // the next call to 'picosat_sat'
245 * // adding a new clause, actually the first literal of
246 * // a clause would also make the assumptions used in the previous
247 * // call to 'picosat_sat' invalid.
249 * // The first two assumptions above are not assumed anymore. Only
250 * // the assumptions, since the last call to 'picosat_sat' returned
251 * // are assumed, e.g. the unit clause '17 0'.
253 * res = picosat_sat (-1);
255 * else if (res == PICOSAT_SATISFIABLE)
257 * // now the assignment is valid and we can call 'picosat_deref'
259 * assert (picosat_deref (1) == 1));
260 * assert (picosat_deref (-2) == 1));
262 * val = picosat_deref (15);
264 * // previous two assumptions are still valid
266 * // would become invalid if 'picosat_add' or 'picosat_assume' is
267 * // called here, but we immediately call 'picosat_sat'. Now when
268 * // entering 'picosat_sat' the solver nows that the previous call
269 * // returned SAT and it can savely reset the previous assumptions
271 * res = picosat_sat (-1);
273 * else
275 * assert (res == PICOSAT_UNKNOWN);
277 * // assumptions valid, but assignment invalid
278 * // except for top level assigned literals which
279 * // necessarily need to have this value if the formula is SAT
281 * // as above the solver nows that the previous call returned UNKWOWN
282 * // and will before doing anything else reset assumptions
284 * picosat_sat (-1);
287 void picosat_assume (int lit);
289 /*------------------------------------------------------------------------*/
290 /* This is an experimental feature for handling 'all different constraints'
291 * (ADC). Currently only one global ADC can be handled. The bit-width of
292 * all the bit-vectors entered in this ADC (stored in 'all different
293 * objects' or ADOs) has to be identical.
295 * TODO: also handle top level assigned literals here.
297 void picosat_add_ado_lit (int);
299 /*------------------------------------------------------------------------*/
300 /* Call the main SAT routine. A negative decision limit sets no limit on
301 * the number of decisions. The return values are as above, e.g.
302 * 'PICOSAT_UNSATISFIABLE', 'PICOSAT_SATISFIABLE', or 'PICOSAT_UNKNOWN'.
304 int picosat_sat (int decision_limit);
306 /* After 'picosat_sat' was called and returned 'PICOSAT_SATISFIABLE', then
307 * the satisfying assignment can be obtained by 'dereferencing' literals.
308 * The value of the literal is return as '1' for 'true', '-1' for 'false'
309 * and '0' for an unknown value.
311 int picosat_deref (int lit);
313 /* Same as before but just returns true resp. false if the literals is
314 * forced to this assignment at the top level. This function does not
315 * require that 'picosat_sat' was called and also does not internally reset
316 * incremental usage.
318 int picosat_deref_toplevel (int lit);
320 /* Returns non zero if the CNF is unsatisfiable because an empty clause was
321 * added or derived.
323 int picosat_inconsistent (void);
325 /* Returns non zero if the literal is a failed assumption, which is defined
326 * as an assumption used to derive unsatisfiability. This is as accurate as
327 * generating core literals, but still of course is an overapproximation of
328 * the set of assumptions really necessary. The technique does not need
329 * clausal core generation nor tracing to be enabled and thus can be much
330 * more effectice. The function can only called as long the current
331 * assumptions are valid. See 'picosat_assume' for more details.
333 int picosat_failed_assumption (int lit);
335 /*------------------------------------------------------------------------*/
336 /* Assume that a previous call to 'picosat_sat' in incremental usage,
337 * returned 'SATISFIABLE'. Then a couple of clauses and optionally new
338 * variables were added (a new variable is a variable that has an index
339 * larger then the maximum variable added so far). The next call to
340 * 'picosat_sat' also returns 'SATISFIABLE'. If this function
341 * 'picosat_changed' returns '0', then the assignment to the old variables
342 * did not change. Otherwise it may have changed. The return value to
343 * this function is only valid until new clauses are added through
344 * 'picosat_add', an assumption is made through 'picosat_assume', or again
345 * 'picosat_sat' is called. This is the same assumption as for
346 * 'picosat_deref'.
348 * TODO currently this function may also return a non zero value even if the
349 * old assignment did not change, because it only checks whether the
350 * assignment of at least one old variable was flipped at least once during
351 * the search. In principle it should be possible to be exact in the other
352 * direcetion as well by using a counter of variables that have an odd
353 * number of flips. But this is not implemented yet.
355 int picosat_changed (void);
357 /*------------------------------------------------------------------------*/
358 /* The following five functions internally extract the variable and clausal
359 * core and thus require trace generation to be enabled with
360 * 'picosat_enable_trace_generation' right after calling 'picosat_init'.
362 * TODO: using these functions in incremental mode with failed assumptions
363 * has only been tested for 'picosat_corelit' thoroughly. The others may
364 * only work in non-incremental mode or without using 'picosat_assume'.
367 /* This function gives access to the variable core, which is made up of the
368 * variables that were resolved in deriving the empty clauses.
370 int picosat_corelit (int lit);
372 /* Write the clauses that were used in deriving the empty clause to a file
373 * in DIMACS format.
375 void picosat_write_clausal_core (FILE * core_file);
377 /* Write a proof trace in TraceCheck format to a file.
379 void picosat_write_compact_trace (FILE * trace_file);
380 void picosat_write_extended_trace (FILE * trace_file);
382 /* Write a RUP trace to a file. This trace file contains only the learned
383 * core clauses while this is not necessarily the case for the RUP file
384 * obtained with 'picosat_set_incremental_rup_file'.
386 void picosat_write_rup_trace (FILE * trace_file);
388 /*------------------------------------------------------------------------*/
389 /* Keeping the proof trace around is not necessary if an over-approximation
390 * of the core is enough. A literal is 'used' if it was involved in a
391 * resolution to derive a learned clause. The core literals are necessarily
392 * a subset of the 'used' literals.
395 int picosat_usedlit (int lit);
396 /*------------------------------------------------------------------------*/
397 #endif