| 1 | [](https://opensource.org/licenses/MIT) |
| 2 | [](https://ci.appveyor.com/project/msoos/cryptominisat) |
| 3 |  |
| 4 | |
| 5 | |
| 6 | CryptoMiniSat SAT solver |
| 7 | =========================================== |
| 8 | |
| 9 | This system provides CryptoMiniSat, an advanced incremental SAT solver. The system has 3 |
| 10 | interfaces: command-line, C++ library and python. The command-line interface |
| 11 | takes a [cnf](http://en.wikipedia.org/wiki/Conjunctive_normal_form) as an |
| 12 | input in the [DIMACS](http://www.satcompetition.org/2009/format-benchmarks2009.html) |
| 13 | format with the extension of XOR clauses. The C++ and python interface mimics this and also |
| 14 | allows for incremental use: assumptions and multiple `solve` calls. |
| 15 | A C compatible wrapper is also provided. |
| 16 | |
| 17 | When citing, always reference our [SAT 2009 conference paper](https://link.springer.com/chapter/10.1007%2F978-3-642-02777-2_24), bibtex record is [here](http://dblp.uni-trier.de/rec/bibtex/conf/sat/SoosNC09). |
| 18 | |
| 19 | License |
| 20 | ----- |
| 21 | |
| 22 | Everything that is needed to build by default is MIT licensed. If you specifically instruct the system it can build with Bliss, which are both GPL. However, by default CryptoMiniSat will not build with these. |
| 23 | |
| 24 | |
| 25 | Compiling in Linux |
| 26 | ----- |
| 27 | |
| 28 | To build and install, issue: |
| 29 | |
| 30 | ``` |
| 31 | sudo apt-get install build-essential cmake |
| 32 | # not required but very useful |
| 33 | sudo apt-get install zlib1g-dev |
| 34 | tar xzvf cryptominisat-version.tar.gz |
| 35 | cd cryptominisat-version |
| 36 | mkdir build && cd build |
| 37 | cmake .. |
| 38 | make |
| 39 | sudo make install |
| 40 | sudo ldconfig |
| 41 | ``` |
| 42 | |
| 43 | Compiling in Mac OSX |
| 44 | ----- |
| 45 | |
| 46 | First, you must get Homebrew from https://brew.sh/ then: |
| 47 | |
| 48 | ``` |
| 49 | brew install cmake |
| 50 | tar xzvf cryptominisat-version.tar.gz |
| 51 | cd cryptominisat-version |
| 52 | mkdir build && cd build |
| 53 | cmake .. |
| 54 | make |
| 55 | sudo make install |
| 56 | ``` |
| 57 | |
| 58 | Compiling in Windows |
| 59 | ----- |
| 60 | |
| 61 | ``` |
| 62 | C:\> [ download and unzip cryptominisat-version.zip ] |
| 63 | C:\> cd cryptominisat |
| 64 | C:\cryptominisat> mkdir build |
| 65 | C:\cryptominisat> cd build |
| 66 | C:\cryptominisat\build> cmake -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=Release -DSTATICCOMPILE=ON .. |
| 67 | C:\cryptominisat\build> cmake --build --config Release . |
| 68 | ``` |
| 69 | |
| 70 | You now have the static binary under `C:\cryptominisat\build\Release\cryptominisat5.exe` |
| 71 | |
| 72 | Command-line usage |
| 73 | ----- |
| 74 | |
| 75 | Let's take the file: |
| 76 | ``` |
| 77 | p cnf 3 3 |
| 78 | 1 0 |
| 79 | -2 0 |
| 80 | -1 2 3 0 |
| 81 | ``` |
| 82 | |
| 83 | The file has 3 variables and 3 clauses, this is reflected in the header |
| 84 | `p cnf 3 3` which gives the number of variables as the first number and the number of clauses as the second. |
| 85 | Every clause is ended by '0'. The clauses say: 1 must be True, 2 |
| 86 | must be False, and either 1 has to be False, 2 has to be True or 3 has to be |
| 87 | True. The only solution to this problem is: |
| 88 | ``` |
| 89 | cryptominisat5 --verb 0 file.cnf |
| 90 | s SATISFIABLE |
| 91 | v 1 -2 3 0 |
| 92 | ``` |
| 93 | |
| 94 | Which means, that setting variable 1 True, variable 2 False and variable 3 True satisfies the set of constraints (clauses) in the CNF. If the file had contained: |
| 95 | ``` |
| 96 | p cnf 3 4 |
| 97 | 1 0 |
| 98 | -2 0 |
| 99 | -3 0 |
| 100 | -1 2 3 0 |
| 101 | ``` |
| 102 | |
| 103 | Then there is no solution and the solver returns `s UNSATISFIABLE`. |
| 104 | |
| 105 | Incremental Python Usage |
| 106 | ----- |
| 107 | The python module works with both Python 3. Just execute: |
| 108 | |
| 109 | ``` |
| 110 | pip3 install pycryptosat |
| 111 | ``` |
| 112 | |
| 113 | You can then use it in incremental mode as: |
| 114 | |
| 115 | ``` |
| 116 | >>> from pycryptosat import Solver |
| 117 | >>> s = Solver() |
| 118 | >>> s.add_clause([1]) |
| 119 | >>> s.add_clause([-2]) |
| 120 | >>> s.add_clause([-1, 2, 3]) |
| 121 | >>> sat, solution = s.solve() |
| 122 | >>> print sat |
| 123 | True |
| 124 | >>> print solution |
| 125 | (None, True, False, True) |
| 126 | >>> sat, solution = s.solve([-3]) |
| 127 | >> print sat |
| 128 | False |
| 129 | >>> sat, solution = s.solve() |
| 130 | >>> print sat |
| 131 | True |
| 132 | >>> s.add_clause([-3]) |
| 133 | >>> sat, solution = s.solve() |
| 134 | >>> print sat |
| 135 | False |
| 136 | ``` |
| 137 | |
| 138 | We can also try to assume any variable values for a single solver run: |
| 139 | ``` |
| 140 | >>> sat, solution = s.solve([-3]) |
| 141 | >>> print sat |
| 142 | False |
| 143 | >>> print solution |
| 144 | None |
| 145 | >>> sat, solution = s.solve() |
| 146 | >>> print sat |
| 147 | True |
| 148 | >>> print solution |
| 149 | (None, True, False, True) |
| 150 | ``` |
| 151 | If you want to build the python module, you can do this: |
| 152 | |
| 153 | ``` |
| 154 | sudo apt-get install build-essential |
| 155 | sudo apt-get install python3-setuptools python3-dev |
| 156 | git clone https://github.com/msoos/cryptominisat |
| 157 | python -m build |
| 158 | pip install dist/pycryptosat-*.whl |
| 159 | ``` |
| 160 | |
| 161 | Incremental Library Usage |
| 162 | ----- |
| 163 | The library uses a variable numbering scheme that starts from 0. Since 0 cannot |
| 164 | be negated, the class `Lit` is used as: `Lit(variable_number, is_negated)`. As |
| 165 | such, the 1st CNF above would become: |
| 166 | |
| 167 | ``` |
| 168 | #include <cryptominisat5/cryptominisat.h> |
| 169 | #include <assert.h> |
| 170 | #include <vector> |
| 171 | using std::vector; |
| 172 | using namespace CMSat; |
| 173 | |
| 174 | int main() |
| 175 | { |
| 176 | SATSolver solver; |
| 177 | vector<Lit> clause; |
| 178 | |
| 179 | //Let's use 4 threads |
| 180 | solver.set_num_threads(4); |
| 181 | |
| 182 | //We need 3 variables. They will be: 0,1,2 |
| 183 | //Variable numbers are always trivially increasing |
| 184 | solver.new_vars(3); |
| 185 | |
| 186 | //add "1 0" |
| 187 | clause.push_back(Lit(0, false)); |
| 188 | solver.add_clause(clause); |
| 189 | |
| 190 | //add "-2 0" |
| 191 | clause.clear(); |
| 192 | clause.push_back(Lit(1, true)); |
| 193 | solver.add_clause(clause); |
| 194 | |
| 195 | //add "-1 2 3 0" |
| 196 | clause.clear(); |
| 197 | clause.push_back(Lit(0, true)); |
| 198 | clause.push_back(Lit(1, false)); |
| 199 | clause.push_back(Lit(2, false)); |
| 200 | solver.add_clause(clause); |
| 201 | |
| 202 | lbool ret = solver.solve(); |
| 203 | assert(ret == l_True); |
| 204 | std::cout |
| 205 | << "Solution is: " |
| 206 | << solver.get_model()[0] |
| 207 | << ", " << solver.get_model()[1] |
| 208 | << ", " << solver.get_model()[2] |
| 209 | << std::endl; |
| 210 | |
| 211 | //assumes 3 = FALSE, no solutions left |
| 212 | vector<Lit> assumptions; |
| 213 | assumptions.push_back(Lit(2, true)); |
| 214 | ret = solver.solve(&assumptions); |
| 215 | assert(ret == l_False); |
| 216 | |
| 217 | //without assumptions we still have a solution |
| 218 | ret = solver.solve(); |
| 219 | assert(ret == l_True); |
| 220 | |
| 221 | //add "-3 0" |
| 222 | //No solutions left, UNSATISFIABLE returned |
| 223 | clause.clear(); |
| 224 | clause.push_back(Lit(2, true)); |
| 225 | solver.add_clause(clause); |
| 226 | ret = solver.solve(); |
| 227 | assert(ret == l_False); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | ``` |
| 232 | |
| 233 | The library usage also allows for assumptions. We can add these lines just |
| 234 | before the `return 0;` above: |
| 235 | ``` |
| 236 | vector<Lit> assumptions; |
| 237 | assumptions.push_back(Lit(2, true)); |
| 238 | lbool ret = solver.solve(&assumptions); |
| 239 | assert(ret == l_False); |
| 240 | |
| 241 | lbool ret = solver.solve(); |
| 242 | assert(ret == l_True); |
| 243 | ``` |
| 244 | |
| 245 | Since we assume that variable 2 must be false, there is no solution. However, |
| 246 | if we solve again, without the assumption, we get back the original solution. |
| 247 | Assumptions allow us to assume certain literal values for a _specific run_ but |
| 248 | not all runs -- for all runs, we can simply add these assumptions as 1-long |
| 249 | clauses. |
| 250 | |
| 251 | Multiple solutions |
| 252 | ----- |
| 253 | |
| 254 | To find multiple solutions to your problem, just run the solver in a loop |
| 255 | and ban the previous solution found: |
| 256 | |
| 257 | ``` |
| 258 | while(true) { |
| 259 | lbool ret = solver->solve(); |
| 260 | if (ret != l_True) { |
| 261 | assert(ret == l_False); |
| 262 | //All solutions found. |
| 263 | exit(0); |
| 264 | } |
| 265 | |
| 266 | //Use solution here. print it, for example. |
| 267 | |
| 268 | //Banning found solution |
| 269 | vector<Lit> ban_solution; |
| 270 | for (uint32_t var = 0; var < solver->nVars(); var++) { |
| 271 | if (solver->get_model()[var] != l_Undef) { |
| 272 | ban_solution.push_back( |
| 273 | Lit(var, (solver->get_model()[var] == l_True)? true : false)); |
| 274 | } |
| 275 | } |
| 276 | solver->add_clause(ban_solution); |
| 277 | } |
| 278 | ``` |
| 279 | |
| 280 | The above loop will run as long as there are solutions. It is __highly__ |
| 281 | suggested to __only__ add into the new clause(`bad_solutions` above) the |
| 282 | variables that are "important" or "main" to your problem. Variables that were |
| 283 | only used to translate the original problem into CNF should not be added. |
| 284 | This way, you will not get spurious solutions that don't differ in the main, |
| 285 | important variables. |
| 286 | |
| 287 | Rust bindings |
| 288 | ----- |
| 289 | |
| 290 | To build the Rust bindings: |
| 291 | |
| 292 | ``` |
| 293 | git clone https://github.com/msoos/cryptominisat-rs/ |
| 294 | cd cryptominisat-rs |
| 295 | cargo build --release |
| 296 | cargo test |
| 297 | ``` |
| 298 | |
| 299 | You can use it as per the [README](https://github.com/msoos/cryptominisat-rs/blob/master/README.markdown) in that repository. To include CryptoMiniSat in your Rust project, add the dependency to your `Cargo.toml` file: |
| 300 | |
| 301 | ``` |
| 302 | cryptominisat = { git = "https://github.com/msoos/cryptominisat-rs", branch= "master" } |
| 303 | ``` |
| 304 | |
| 305 | You can see an example project using CryptoMiniSat in Rust [here](https://github.com/msoos/caqe/). |
| 306 | |
| 307 | Preprocessing |
| 308 | ----- |
| 309 | If you wish to use CryptoMiniSat as a preprocessor, we encourage you |
| 310 | to try out our model counting preprocessing framework |
| 311 | (Arjun)[https://www.github.com/meelgroup/arjun]. Arjun was conceived to |
| 312 | minimize input formulas for model counting, but it can also be used for |
| 313 | non-model-counting purposes. However, it cannot produce a solution to the |
| 314 | original CNF given a solution to the simplified CNF. This current limitation |
| 315 | will eventually be lifted for Arjun. |
| 316 | |
| 317 | Please see the README of Arjun for more details. The basic usage of Arjun is: |
| 318 | `./arjun --renumber 0 original.cnf simplified.cnf` |
| 319 | |
| 320 | Gauss-Jordan elimination |
| 321 | ----- |
| 322 | Since CryptoMiniSat 5.8, Gauss-Jordan elimination is compiled into the solver by default. However, it will turn off automatically in case the solver observes GJ not to perform too well. To use Gaussian elimination, provide a CNF with xors in it (either in CNF or XOR+CNF form) and either run with default setup, or, tune it to your heart's desire: |
| 323 | |
| 324 | ``` |
| 325 | Gauss options: |
| 326 | --iterreduce arg (=1) Reduce iteratively the matrix that is updated.We |
| 327 | effectively are moving the start to the last |
| 328 | column updated |
| 329 | --maxmatrixrows arg (=3000) Set maximum no. of rows for gaussian matrix. Too |
| 330 | large matrixes should be discarded for reasons of |
| 331 | efficiency |
| 332 | --autodisablegauss arg (=1) Automatically disable gauss when performing badly |
| 333 | --minmatrixrows arg (=5) Set minimum no. of rows for gaussian matrix. |
| 334 | Normally, too small matrixes are discarded for |
| 335 | reasons of efficiency |
| 336 | --savematrix arg (=2) Save matrix every Nth decision level |
| 337 | --maxnummatrixes arg (=3) Maximum number of matrixes to treat. |
| 338 | ``` |
| 339 | |
| 340 | In particular, you may want to set `--autodisablegauss 0` in case you are sure it'll help. |
| 341 | |
| 342 | Testing |
| 343 | ----- |
| 344 | For testing you will need the GIT checkout and build as per: |
| 345 | |
| 346 | ``` |
| 347 | sudo apt-get install build-essential cmake git |
| 348 | sudo apt-get install zlib1g-dev libboost-program-options-dev libsqlite3-dev |
| 349 | sudo apt-get install git python3-pip python3-setuptools python3-dev |
| 350 | sudo pip3 install --upgrade pip |
| 351 | sudo pip3 install lit |
| 352 | git clone https://github.com/msoos/cryptominisat.git |
| 353 | cd cryptominisat |
| 354 | git submodule update --init |
| 355 | mkdir build && cd build |
| 356 | cmake -DENABLE_TESTING=ON .. |
| 357 | make -j4 |
| 358 | make test |
| 359 | sudo make install |
| 360 | sudo ldconfig |
| 361 | ``` |
| 362 | |
| 363 | Fuzzing |
| 364 | ----- |
| 365 | Build for test as per above, then: |
| 366 | |
| 367 | ``` |
| 368 | cd ../cryptominisat/scripts/fuzz/ |
| 369 | ./fuzz_test.py |
| 370 | ``` |
| 371 | |
| 372 | |
| 373 | CrystalBall |
| 374 | ----- |
| 375 | |
| 376 | Build and use instructions below. Please see the [associated blog post](https://www.msoos.org/2019/06/crystalball-sat-solving-data-gathering-and-machine-learning/) for more information. |
| 377 | |
| 378 | ``` |
| 379 | # prerequisites on a modern Debian/Ubuntu installation |
| 380 | sudo apt-get install build-essential cmake git |
| 381 | sudo apt-get install zlib1g-dev libsqlite3-dev |
| 382 | sudo apt-get install libboost-program-options-dev libboost-serialization-dev |
| 383 | sudo apt-get install python3-pip |
| 384 | sudo pip3 install sklearn pandas numpy lit matplotlib |
| 385 | |
| 386 | # build and install Louvain Communities |
| 387 | git clone https://github.com/meelgroup/louvain-community |
| 388 | cd louvain-community |
| 389 | mkdir build && cd build |
| 390 | cmake .. |
| 391 | make -j10 |
| 392 | sudo make install |
| 393 | cd ../.. |
| 394 | |
| 395 | # build and install XGBoost |
| 396 | git clone https://github.com/dmlc/xgboost |
| 397 | cd xgboost |
| 398 | mkdir build && cd build |
| 399 | cmake .. |
| 400 | make -j10 |
| 401 | sudo make install |
| 402 | cd ../.. |
| 403 | |
| 404 | # build and install LightGBM |
| 405 | git clone https://github.com/microsoft/LightGBM |
| 406 | cd LightGBM |
| 407 | mkdir build && cd build |
| 408 | cmake .. |
| 409 | make -j10 |
| 410 | sudo make install |
| 411 | cd ../.. |
| 412 | |
| 413 | # getting the code |
| 414 | git clone https://github.com/msoos/cryptominisat |
| 415 | cd cryptominisat |
| 416 | git checkout crystalball |
| 417 | git submodule update --init |
| 418 | mkdir build && cd build |
| 419 | ln -s ../scripts/crystal/* . |
| 420 | ln -s ../scripts/build_scripts/* . |
| 421 | |
| 422 | # Let's get an unsatisfiable CNF |
| 423 | wget https://www.msoos.org/largefiles/goldb-heqc-i10mul.cnf.gz |
| 424 | gunzip goldb-heqc-i10mul.cnf.gz |
| 425 | |
| 426 | # Gather the data, denormalize, label, |
| 427 | # create the classifier, generate C++, |
| 428 | # and build the final SAT solver |
| 429 | ./ballofcrystal.sh goldb-heqc-i10mul.cnf |
| 430 | [...compilations and the full data pipeline...] |
| 431 | |
| 432 | # let's use our newly built tool |
| 433 | ./cryptominisat5 goldb-heqc-i10mul.cnf |
| 434 | [ ... ] |
| 435 | s UNSATISFIABLE |
| 436 | |
| 437 | # Let's look at the data |
| 438 | cd goldb-heqc-i10mul.cnf-dir |
| 439 | sqlite3 mydata.db |
| 440 | sqlite> select count() from sum_cl_use; |
| 441 | 94507 |
| 442 | ``` |
| 443 | |
| 444 | Configuring a build for a minimal binary&library |
| 445 | ----- |
| 446 | The following configures the system to build a bare minimal binary&library. It needs a compiler, but nothing much else: |
| 447 | |
| 448 | ``` |
| 449 | cmake -DONLY_SIMPLE=ON -DNOZLIB=ON -DSTATS=OFF -DENABLE_TESTING=OFF . |
| 450 | ``` |
| 451 | |
| 452 | CMake Arguments |
| 453 | ----- |
| 454 | The following arguments to cmake configure the generated build artifacts. To use, specify options prior to running make in a clean subdirectory: `cmake <options> ..` |
| 455 | |
| 456 | - `-DSTATICCOMPILE=<ON/OFF>` -- statically linked library and binary. You must build&link `BreakID` with the same `DSTATICCOMPILE=<ON/OFF>` setting as well. You can get the BreakID library from [our GitHub repository](https://github.com/meelgroup/breakid) |
| 457 | - `-DSTATS=<ON/OFF>` -- advanced statistics (slower). Needs [louvain communities](https://github.com/meelgroup/louvain-community) installed. |
| 458 | - `-DENABLE_TESTING=<ON/OFF>` -- test suite support |
| 459 | - `-DMIT=<ON/OFF>` -- MIT licensed components only |
| 460 | - `-DNOMPI=<ON/OFF>` -- without MPI support |
| 461 | - `-DNOZLIB=<ON/OFF>` -- no gzip DIMACS input support |
| 462 | - `-DONLY_SIMPLE=<ON/OFF>` -- only the simple binary is built |
| 463 | - `-DLARGEMEM=<ON/OFF>` -- more memory available for clauses (but slower on most problems) |
| 464 | - `-DIPASIR=<ON/OFF>` -- Build `libipasircryptominisat.so` for [IPASIR](https://www.cs.utexas.edu/users/moore/acl2/manuals/current/manual/index-seo.php/IPASIR____IPASIR) interface support |
| 465 | |
| 466 | Getting learnt clauses |
| 467 | ----- |
| 468 | As an experimental feature, you can get the learnt clauses from the system with the following code, where `lits` is filled with learnt clauses every time `get_next_small_clause` is called. The example below will eventually return all clauses of size 4 or less. You can call `end_getting_small_clauses` at any time. |
| 469 | |
| 470 | ``` |
| 471 | SATSolver s; |
| 472 | //fill the solver, run solve, etc. |
| 473 | |
| 474 | //Get all clauses of size 4 or less |
| 475 | |
| 476 | s->start_getting_small_clauses(4); |
| 477 | |
| 478 | vector<Lit> lits; |
| 479 | bool ret = true; |
| 480 | while (ret) { |
| 481 | bool ret = s->get_next_small_clause(lits); |
| 482 | if (ret) { |
| 483 | //deal with clause in "lits" |
| 484 | add_to_my_db(lits); |
| 485 | } |
| 486 | } |
| 487 | s->end_getting_small_clauses(); |
| 488 | ``` |
| 489 | |
| 490 | C usage |
| 491 | ----- |
| 492 | See src/cryptominisat_c.h.in for details. This is an experimental feature. |