Cross-backend CDCL SAT solver with DRAT-checkable UNSAT proofs, incremental assumptions and weighted MaxSAT optimisation.
| 能力 | 说明 |
|---|---|
| CDCL 求解 | 两文字监听、1-UIP 学习子句、VSIDS 活动度、Luby 序列重启、子句库归约、相位保存 |
| 增量求解 | solve(assumptions) 可反复调用,带 assumption 的 UNSAT 不污染后续求解状态 |
| UNSAT core | solve_core(assumptions) 在 UNSAT 时返回一个本身不可满足的假设子集,可用来解释冲突,也给 MaxSAT 提供下界 |
| 冲突预算 | solve_with_budget 在给定冲突数后返回 Unknown,回退到 0 层,可继续求解 |
| UNSAT 证明 | 每个学习子句与最终空子句写入 DRAT 风格日志,可交给第三方检查器复算 |
| 独立检查器 | 项目自带 check_drat / verify_proof(RUP)与 check_drat_rat / verify_proof_rat(RUP + RAT),不信任求解器 |
| 加权 MaxSAT | WCNF 解析 + 松弛变量 + Sinz 顺序计数编码 + 二分求最优成本 |
| MaxSAT 最优性 | 下界侧 maxsat_bound_proof 导出「代价 k 不可达」的 DRAT 证明;上界侧 maxsat_solution 给出代价恰为 opt 的赋值,maxsat_model_cost 按 WCNF 原文复算;maxsat-verify 两端一起验 |
| 预处理 | Formula::simplify 去掉子句内重复文字、删除重言子句,并统计被删条数 |
| 四后端 | 求解与校验不手写 FFI、不依赖外部求解器,preferred_target = "wasm" 下与 native 同结果;命令行的 @path 读盘用官方 moonbitlang/x/fs |
moon add 2227057182/moonbit-satgit clone https://github.com/2227057182/moonbit-sat.git
cd moonbit-sat
moon check --deny-warn && moon testmkdir sat-demo && cd sat-demo
printf 'name = "tmp/sat-demo"\nversion = "0.1.0"\n' > moon.mod
moon update
moon add 2227057182/moonbit-satimport {
"2227057182/moonbit-sat" @moonbit_sat
}
pkgtype(kind: "executable")fn main {
// (x1 ∨ x2) ∧ ¬x1 ∧ ¬x2:两条软子句必有一条被违约,最优成本是 1
let w = @moonbit_sat.parse_wcnf("p wcnf 2 3 10\n10 1 2 0\n1 -1 0\n1 -2 0\n")
match @moonbit_sat.maxsat_model(w, 512) {
Some((model, cost)) => {
println("cost=\{cost}") // cost=1
println("verified=\{@moonbit_sat.verify_maxsat_solution(w, model, cost)}") // verified=true
}
None => println("hard clauses unsatisfiable")
}
}moon run . # cost=1 / verified=true// (a ∨ b) ∧ ¬a ⇒ a=false, b=true
let f = @moonbit_sat.parse_dimacs("p cnf 2 2\n1 2 0\n-1 0\n")
let s = @moonbit_sat.Solver::new(f)
let proof = s.enable_proof()
if s.solve([]) {
println(s.model()) // [false, true]
} else {
// 证明:学习子句 + 空子句,DRAT 文本
println(proof.text())
}
println(s.stats_string()) // decisions=… propagations=… conflicts=… learnts=…let f = @moonbit_sat.parse_dimacs("p cnf 2 1\n1 2 0\n")
let s = @moonbit_sat.Solver::new(f)
assert_true(s.solve([]))
assert_false(s.solve([@moonbit_sat.lit_new(0, true), @moonbit_sat.lit_new(1, true)]))
assert_true(s.solve([@moonbit_sat.lit_new(0, true)])) // 上一次 UNSAT 不影响这一次let f = @moonbit_sat.parse_dimacs(cnf_text)
let s = @moonbit_sat.Solver::new(f)
let p = s.enable_proof()
assert_false(s.solve([]))
assert_true(@moonbit_sat.verify_proof(f, p.text()))p wcnf 2 3 10
10 1 2 0 # 硬子句:a ∨ b
1 -1 0 # 软子句:¬a,权 1
1 -2 0 # 软子句:¬b,权 1let w = @moonbit_sat.parse_wcnf(wcnf_text)
let opt = @moonbit_sat.maxsat_solve(w, 512) // 总软权重上限 512
println(opt) // 1let opt = @moonbit_sat.maxsat_oll(w) // 无总权重上限
let best = @moonbit_sat.maxsat_oll_solution(w) // (赋值, 代价)?
let bounded = @moonbit_sat.maxsat_oll_result(w, 50) // 带轮预算,不收敛时带回界moon run cmd/main -- solve "p cnf 3 2;1 2 0;-1 0" # SAT + 模型 + 统计
moon run cmd/main -- proof "p cnf 6 9;1 2 0;..." # UNSAT + DRAT 证明
moon run cmd/main -- verify "p cnf 2 3;1 0;2 0;-1 -2 0" "-1 0;0" # 独立校验(RUP)
moon run cmd/main -- verify-rat "p cnf 2 3;1 0;2 0;-1 -2 0" "-1 0;0" # 同一证明,RUP + RAT 规则
moon run cmd/main -- maxsat "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0"
moon run cmd/main -- maxsat-cnf "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" 0 # 打印「代价 ≤0」编码后的 CNF,交给外部检查器
moon run cmd/main -- verify-maxsat "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" 0 "$(cat proof)" # 重建编码后校验
moon run cmd/main -- maxsat-verify "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" # 上界赋值 + 下界证明一起验
moon run cmd/main -- maxsat-solution "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" # 最优赋值 + 复算代价
moon run cmd/main -- maxsat-oll "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" # 核心引导,总软权重无上限
moon run cmd/main -- maxsat-oll-budget "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" 50 # 同上,加轮预算:不收敛时打印上下界
moon run cmd/main -- verify-solution "p wcnf 2 3 10;10 1 2 0;1 -1 0;1 -2 0" 1 "v 1 -2 0" # 只核对导出的赋值
moon run cmd/main -- proof @examples/pigeonhole-3-2.cnf > p32.drat # 从文件读公式,证明写进文件
moon run cmd/main -- verify @examples/pigeonhole-3-2.cnf @p32.drat # 再从文件验回来,24 MB 的证明只能这样传| AdUhTkJm/sat-solver | yf666888/moonsatkit | moonbit-sat | |
|---|---|---|---|
| 核心算法 | CDCL | DPLL | CDCL(两监听 + 1-UIP + VSIDS + 重启 + 归约) |
| 后端 | 仅 native | 四后端 | 四后端 |
| UNSAT 证明 | 无 | 无 | DRAT 风格日志 + 自带独立检查器(RUP 与 RUP+RAT 两种模式) |
| 加权优化 | 无 | 无 | 加权 partial MaxSAT(Sinz 编码 + 二分;核心引导无权重上限) |
| 增量假设 | 不明确 | 有(solve_with_assumptions) | 有,且 assumption 冲突不污染状态 |
| UNSAT core | 无 | 无 | 有(solve_core,返回的 core 可单独回代复验) |
moon check --deny-warn # 0 warnings
moon test # 90 passedmoon run cmd/main -- bench| 变量 | 子句 | 结论 | 决策 | 传播 | 冲突 | 删除字面 |
|---|---|---|---|---|---|---|
| 20 | 86 | SAT | 13 | 59 | 7 | 3 |
| 30 | 129 | UNSAT | 9 | 91 | 10 | 2 |
| 40 | 172 | SAT | 24 | 209 | 15 | 11 |
| 50 | 215 | SAT | 99 | 1008 | 76 | 44 |
| 60 | 258 | SAT | 108 | 1469 | 83 | 65 |
moon run cmd/main -- bench-verify 9| k | 变量 | 证明行数 | 全量扫描 | 两文字监听 | 扁平池(0.6.7) |
|---|---|---|---|---|---|
| 7 | 42 | 670 | 0.04s | 0.02s | 0.02s |
| 8 | 56 | 3942 | 0.66s | 0.16s | 0.14s |
| 9 | 72 | 40273 | 32.56s | 6.00s | 5.30s |
| 10 | 90 | 295345 | — | 435s | 366s |
moon check --deny-warn
moon test
moon run cmd/main -- solve "p cnf 2 1;1 2 0"type Lit = Intpub struct Clause {
lits : Array[Int]
learnt : Bool
activity : Double
lbd : Int
watch0 : Int
watch1 : Int
dead : Bool
}pub enum SolveOutcome {
Satisfiable
Unsatisfiable
Unknown
}pub struct Solver {
num_vars : Int
clauses : Array[Clause]
watches : Array[Array[Int]]
assigns : Array[Int]
levels : Array[Int]
reasons : Array[Int]
trail : Array[Int]
trail_lim : Array[Int]
qhead : Int
activity : Array[Double]
var_inc : Double
heap : Array[Int]
heap_pos : Array[Int]
phase : Array[Bool]
ok : Bool
decisions : Int
propagations : Int
conflicts : Int
minimised : Int
restarts : Int
learnts : Array[Int]
max_learnts : Int
conflicts_until_restart : Int
proof : Proof?
last_core : Array[Int]
}fn Solver::solve_with_budget(self : Solver, assumptions : Array[Int], max_conflicts : Int) -> SolveOutcomeInstall
Download zipCross-backend CDCL SAT solver with DRAT-checkable UNSAT proofs, incremental assumptions and weighted MaxSAT optimisation.