1 /*****************************************************************************************[Main.cc]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007,      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/simp/SimpSolver.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 
main(int argc,char ** argv)54 int main(int argc, char** argv)
55 {
56     try {
57         setUsageHelp("USAGE: %s [options] <input-file> <result-output-file>\n\n  where input may be either in plain or gzipped DIMACS.\n");
58         setX86FPUPrecision();
59 
60         // Extra options:
61         //
62         IntOption    verb   ("MAIN", "verb",   "Verbosity level (0=silent, 1=some, 2=more).", 1, IntRange(0, 2));
63         BoolOption   pre    ("MAIN", "pre",    "Completely turn on/off any preprocessing.", true);
64         BoolOption   solve  ("MAIN", "solve",  "Completely turn on/off solving after preprocessing.", true);
65         StringOption dimacs ("MAIN", "dimacs", "If given, stop after preprocessing and write the result to this file.");
66         IntOption    cpu_lim("MAIN", "cpu-lim","Limit on CPU time allowed in seconds.\n", 0, IntRange(0, INT32_MAX));
67         IntOption    mem_lim("MAIN", "mem-lim","Limit on memory usage in megabytes.\n", 0, IntRange(0, INT32_MAX));
68         BoolOption   strictp("MAIN", "strict", "Validate DIMACS header during parsing.", false);
69 
70         parseOptions(argc, argv, true);
71 
72         SimpSolver  S;
73         double      initial_time = cpuTime();
74 
75         if (!pre) S.eliminate(true);
76 
77         S.verbosity = verb;
78 
79         solver = &S;
80         // Use signal handlers that forcibly quit until the solver will be able to respond to
81         // interrupts:
82         sigTerm(SIGINT_exit);
83 
84         // Try to set resource limits:
85         if (cpu_lim != 0) limitTime(cpu_lim);
86         if (mem_lim != 0) limitMemory(mem_lim);
87 
88         if (argc == 1)
89             printf("Reading from standard input... Use '--help' for help.\n");
90 
91         gzFile in = (argc == 1) ? gzdopen(0, "rb") : gzopen(argv[1], "rb");
92         if (in == NULL)
93             printf("ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]), exit(1);
94 
95         if (S.verbosity > 0){
96             printf("============================[ Problem Statistics ]=============================\n");
97             printf("|                                                                             |\n"); }
98 
99         parse_DIMACS(in, S, (bool)strictp);
100         gzclose(in);
101         FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;
102 
103         if (S.verbosity > 0){
104             printf("|  Number of variables:  %12d                                         |\n", S.nVars());
105             printf("|  Number of clauses:    %12d                                         |\n", S.nClauses()); }
106 
107         double parsed_time = cpuTime();
108         if (S.verbosity > 0)
109             printf("|  Parse time:           %12.2f s                                       |\n", parsed_time - initial_time);
110 
111         // Change to signal-handlers that will only notify the solver and allow it to terminate
112         // voluntarily:
113         sigTerm(SIGINT_interrupt);
114 
115         S.eliminate(true);
116         double simplified_time = cpuTime();
117         if (S.verbosity > 0){
118             printf("|  Simplification time:  %12.2f s                                       |\n", simplified_time - parsed_time);
119             printf("|                                                                             |\n"); }
120 
121         if (!S.okay()){
122             if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
123             if (S.verbosity > 0){
124                 printf("===============================================================================\n");
125                 printf("Solved by simplification\n");
126                 S.printStats();
127                 printf("\n"); }
128             printf("UNSATISFIABLE\n");
129             exit(20);
130         }
131 
132         lbool ret = l_Undef;
133 
134         if (solve){
135             vec<Lit> dummy;
136             ret = S.solveLimited(dummy);
137         }else if (S.verbosity > 0)
138             printf("===============================================================================\n");
139 
140         if (dimacs && ret == l_Undef)
141             S.toDimacs((const char*)dimacs);
142 
143         if (S.verbosity > 0){
144             S.printStats();
145             printf("\n"); }
146         printf(ret == l_True ? "SATISFIABLE\n" : ret == l_False ? "UNSATISFIABLE\n" : "INDETERMINATE\n");
147         if (res != NULL){
148             if (ret == l_True){
149                 fprintf(res, "SAT\n");
150                 for (int i = 0; i < S.nVars(); i++)
151                     if (S.model[i] != l_Undef)
152                         fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
153                 fprintf(res, " 0\n");
154             }else if (ret == l_False)
155                 fprintf(res, "UNSAT\n");
156             else
157                 fprintf(res, "INDET\n");
158             fclose(res);
159         }
160 
161 #ifdef NDEBUG
162         exit(ret == l_True ? 10 : ret == l_False ? 20 : 0);     // (faster than "return", which will invoke the destructor for 'Solver')
163 #else
164         return (ret == l_True ? 10 : ret == l_False ? 20 : 0);
165 #endif
166     } catch (OutOfMemoryException&){
167         printf("===============================================================================\n");
168         printf("INDETERMINATE\n");
169         exit(0);
170     }
171 }
172