项目作者: togatoga

项目描述 :
A simple CDCL(Conflict-Driven-Clause-Learning) SAT solver in Rust.
高级语言: Rust
项目地址: git://github.com/togatoga/screwsat.git
创建时间: 2019-04-14T23:36:21Z
项目社区:https://github.com/togatoga/screwsat

开源协议:Other

下载


screwsat

Crates.io

A simple CDCL(Conflict-Driven-Clause-Learning) SAT Solver in Rust.
I wrote it very simple to help people(including me) understand the inside of SAT Solver.

I have implemented the core SAT Solver algorithms and techniques in screwsat.

Algorithms and Techniques

  • CDCL(Conflict-Driven-Clause-Learning)
  • Back Jump
  • Two-Literal-Watching
  • VSIDS

The performance of screwsat isn’t as good as other modern sat solvers.
But you can grasp some important points of SAT Solver from screwsat(I hope).

screwsat is written in only one file and std libraries. You can use it for competitive programming problems.

Accepted by screwsat

Testing

You need to pull all SAT problems under the cnf directory that are stored by git-lfs to run cargo test.

  1. % git lfs pull
  2. % cargo test -- --nocapture

How to use

screwsat can be used as a library and a command-line tool.

Command

Install

  1. % cargo install --locked screwsat

Usage(cmd)

  1. % screwsat --help
  2. USAGE: screwsat [options] <input-file> [output-file]
  3. % cat examples/sat.cnf
  4. c Here is a comment.
  5. c SATISFIABLE
  6. p cnf 5 3
  7. 1 -5 4 0
  8. -1 5 3 4 0
  9. -3 -4 0
  10. % screwsat examples/sat.cnf
  11. s SATISFIABLE
  12. -1 -2 -3 -4 -5 0
  13. % screwsat cnf/unsat/unsat.cnf
  14. s UNSATISFIABLE
  15. % screwsat examples/sat.cnf sat_result.txt
  16. % cat sat_result.txt
  17. SAT
  18. -1 -2 -3 -4 -5 0

Library

You need to add screwsat to Cargo.toml.

  1. screwsat="*"

OR

Copy src/lib.rs and Paste it. (Competitive Programming Style)

Usage(lib)

  1. use std::vec;
  2. use screwsat::solver::*;
  3. use screwsat::util;
  4. fn main() {
  5. {
  6. // Create a default Solver struct
  7. let mut solver = Solver::default();
  8. // A problem is (x1 v ¬x5 v x4) ∧ (¬x1 v x5 v x3 v x4) ∧ (x3 v x4)
  9. let clauses = vec![
  10. // (x1 v ¬x5 v x4)
  11. vec![Lit::from(1), Lit::from(-5), Lit::from(4)],
  12. // (¬x1 v x5 v x3 v x4)
  13. vec![Lit::from(-1), Lit::from(5), Lit::from(3), Lit::from(4)],
  14. // (x3 v x4)
  15. vec![Lit::from(3), Lit::from(4)],
  16. ];
  17. // Add clauses to solver
  18. clauses
  19. .into_iter()
  20. .for_each(|clause| solver.add_clause(&clause));
  21. let status = solver.solve(None);
  22. // Sat: A problem is SATISFIABLE.
  23. println!("{:?}", status);
  24. // print the assignments satisfy a given problem.
  25. // x1 = false x2 = false x3 = false x4 = true x5 = false
  26. solver.models.iter().enumerate().for_each(|(var, assign)| {
  27. let b = match assign {
  28. LitBool::True => true,
  29. _ => false,
  30. };
  31. print!("x{} = {} ", var + 1, b);
  32. });
  33. println!("");
  34. }
  35. {
  36. // Parse a DIMACS CNF file
  37. // c
  38. // c This is a sample input file.
  39. // c (unsatisfiable)
  40. // c
  41. // p cnf 3 5
  42. // 1 -2 3 0
  43. // -1 2 0
  44. // -2 -3 0
  45. // 1 2 -3 0
  46. // 1 3 0
  47. // -1 -2 3 0
  48. let input = std::fs::File::open("example/unsat.cnf").unwrap();
  49. let cnf = util::parse_cnf(input).unwrap();
  50. // 3
  51. let variable_num = cnf.var_num.unwrap();
  52. // 5
  53. //let clause_num = cnf.cla_num.unwrap();
  54. let clauses = cnf.clauses;
  55. // Create a new Solver struct
  56. let mut solver = Solver::new(variable_num, &clauses);
  57. let status = solver.solve(None);
  58. // Unsat: A problem is UNSATISFIABLE
  59. println!("{:?}", status);
  60. }
  61. {
  62. // Set the time limitation
  63. // You might want to set the time limitation for very hard problem
  64. let input = std::fs::File::open("example/hard.cnf").unwrap();
  65. let cnf = util::parse_cnf(input).unwrap();
  66. let mut solver = Solver::default();
  67. let clauses = cnf.clauses;
  68. clauses
  69. .into_iter()
  70. .for_each(|clause| solver.add_clause(&clause));
  71. // 5 sec
  72. let status = solver.solve(Some(std::time::Duration::from_secs(5)));
  73. // Indeterminate
  74. println!("{:?}", status);
  75. }
  76. }

Appreciation

This code is really inspired by his good simple code not522’s SAT Solver

Contribution

Contributions and feedbacks are welcome. (e.g., fix typo and tedious code and my English, report bugs/issues, GIVE ME GITHUB STARS)