1 /*****************************************************************************************[Main.cc]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20 
21 #include <errno.h>
22 #include <zlib.h>
23 
24 #include "minisat/utils/System.h"
25 #include "minisat/utils/ParseUtils.h"
26 #include "minisat/utils/Options.h"
27 #include "minisat/core/Dimacs.h"
28 #include "minisat/core/Solver.h"
29 
30 using namespace Minisat;
31 
32 //=================================================================================================
33 
34 
35 static Solver* solver;
36 // Terminate by notifying the solver and back out gracefully. This is mainly to have a test-case
37 // for this feature of the Solver as it may take longer than an immediate call to '_exit()'.
SIGINT_interrupt(int)38 static void SIGINT_interrupt(int) { solver->interrupt(); }
39 
40 // Note that '_exit()' rather than 'exit()' has to be used. The reason is that 'exit()' calls
41 // destructors and may cause deadlocks if a malloc/free function happens to be running (these
42 // functions are guarded by locks for multithreaded use).
SIGINT_exit(int)43 static void SIGINT_exit(int) {
44     printf("\n"); printf("*** INTERRUPTED ***\n");
45     if (solver->verbosity > 0){
46         solver->printStats();
47         printf("\n"); printf("*** INTERRUPTED ***\n"); }
48     _exit(1); }
49 
50 
51 //=================================================================================================
52 // Main:
53 
54 
main(int argc,char ** argv)55 int main(int argc, char** argv)
56 {
57     try {
58         setUsageHelp("USAGE: %s [options] <input-file> <result-output-file>\n\n  where input may be either in plain or gzipped DIMACS.\n");
59         setX86FPUPrecision();
60 
61         // Extra options:
62         //
63         IntOption    verb   ("MAIN", "verb",   "Verbosity level (0=silent, 1=some, 2=more).", 1, IntRange(0, 2));
64         IntOption    cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", 0, IntRange(0, INT32_MAX));
65         IntOption    mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", 0, IntRange(0, INT32_MAX));
66         BoolOption   strictp("MAIN", "strict", "Validate DIMACS header during parsing.", false);
67 
68         parseOptions(argc, argv, true);
69 
70         Solver S;
71         double initial_time = cpuTime();
72 
73         S.verbosity = verb;
74 
75         solver = &S;
76         // Use signal handlers that forcibly quit until the solver will be able to respond to
77         // interrupts:
78         sigTerm(SIGINT_exit);
79 
80         // Try to set resource limits:
81         if (cpu_lim != 0) limitTime(cpu_lim);
82         if (mem_lim != 0) limitMemory(mem_lim);
83 
84         if (argc == 1)
85             printf("Reading from standard input... Use '--help' for help.\n");
86 
87         gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
88         if (in == NULL)
89             printf("ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]), exit(1);
90 
91         if (S.verbosity > 0){
92             printf("============================[ Problem Statistics ]=============================\n");
93             printf("|                                                                             |\n"); }
94 
95         parse_DIMACS(in, S, (bool)strictp);
96         gzclose(in);
97         FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;
98 
99         if (S.verbosity > 0){
100             printf("|  Number of variables:  %12d                                         |\n", S.nVars());
101             printf("|  Number of clauses:    %12d                                         |\n", S.nClauses()); }
102 
103         double parsed_time = cpuTime();
104         if (S.verbosity > 0){
105             printf("|  Parse time:           %12.2f s                                       |\n", parsed_time - initial_time);
106             printf("|                                                                             |\n"); }
107 
108         // Change to signal-handlers that will only notify the solver and allow it to terminate
109         // voluntarily:
110         sigTerm(SIGINT_interrupt);
111 
112         if (!S.simplify()){
113             if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
114             if (S.verbosity > 0){
115                 printf("===============================================================================\n");
116                 printf("Solved by unit propagation\n");
117                 S.printStats();
118                 printf("\n"); }
119             printf("UNSATISFIABLE\n");
120             exit(20);
121         }
122 
123         vec<Lit> dummy;
124         lbool ret = S.solveLimited(dummy);
125         if (S.verbosity > 0){
126             S.printStats();
127             printf("\n"); }
128         printf(ret == l_True ? "SATISFIABLE\n" : ret == l_False ? "UNSATISFIABLE\n" : "INDETERMINATE\n");
129         if (res != NULL){
130             if (ret == l_True){
131                 fprintf(res, "SAT\n");
132                 for (int i = 0; i < S.nVars(); i++)
133                     if (S.model[i] != l_Undef)
134                         fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
135                 fprintf(res, " 0\n");
136             }else if (ret == l_False)
137                 fprintf(res, "UNSAT\n");
138             else
139                 fprintf(res, "INDET\n");
140             fclose(res);
141         }
142 
143 #ifdef NDEBUG
144         exit(ret == l_True ? 10 : ret == l_False ? 20 : 0);     // (faster than "return", which will invoke the destructor for 'Solver')
145 #else
146         return (ret == l_True ? 10 : ret == l_False ? 20 : 0);
147 #endif
148     } catch (OutOfMemoryException&){
149         printf("===============================================================================\n");
150         printf("INDETERMINATE\n");
151         exit(0);
152     }
153 }
154