Skip to content

Commit 86d889a

Browse files
committed
Updated Kosat, now you can only specify clauses
1 parent 7e6d365 commit 86d889a

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/commonMain/kotlin/org/kosat/CDCL.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ fun solveCnf(cnf: CnfRequest): List<Int>? {
1616
return CDCL(clauses.map { Clause(it) }.toMutableList(), cnf.vars).solve()
1717
}
1818

19+
fun solveCnf(clauses: List<List<Lit>>): List<Int> = Kosat(clauses, 0).apply { solve() }.getModel()
20+
1921
enum class SolverType {
2022
INCREMENTAL, NON_INCREMENTAL;
2123
}
@@ -34,6 +36,7 @@ class CDCL(private val solverType: SolverType = SolverType.INCREMENTAL) {
3436
val learnts = mutableListOf<Clause>()
3537

3638
var numberOfVariables: Int = 0
39+
private set
3740

3841
// for each variable contains current assignment, clause it came from and decision level when it happened
3942
val vars: MutableList<VarState> = MutableList(numberOfVariables) { VarState(VarValue.UNDEFINED, null, -1) }
@@ -79,7 +82,9 @@ class CDCL(private val solverType: SolverType = SolverType.INCREMENTAL) {
7982

8083
// get value of literal
8184
fun getValue(lit: Lit): VarValue {
82-
if (vars[variable(lit)].value == VarValue.UNDEFINED) return VarValue.UNDEFINED
85+
if (vars[variable(lit)].value == VarValue.UNDEFINED)
86+
return VarValue.UNDEFINED
87+
8388
return if (lit % 2 == 1)
8489
!vars[variable(lit)].value
8590
else
@@ -144,7 +149,7 @@ class CDCL(private val solverType: SolverType = SolverType.INCREMENTAL) {
144149

145150
// add not mentioned variables from new clause
146151
val maxVar = clause.maxOfOrNull { variable(it) } ?: 0
147-
while (numberOfVariables < maxVar) {
152+
while (numberOfVariables <= maxVar) {
148153
addVariable()
149154
}
150155

src/commonMain/kotlin/org/kosat/DPLL.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.collections.immutable.persistentListOf
55
import kotlin.math.abs
66

77
// DPLL
8+
@Deprecated("use solveCnf instead", replaceWith = ReplaceWith("solveCnf(clauses)"))
89
fun solveCnfDPLL(cnf: CnfRequest): List<Int>? {
910
val clauses = ArrayList(cnf.clauses.map { ArrayList(it.lits.toList()) })
1011
return dpll(clauses)?.sortedBy { abs(it) }

src/commonMain/kotlin/org/kosat/Kosat.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ interface Solver {
5959
fun getModel(): List<Lit>
6060
}
6161

62-
class Kosat(clauses: MutableList<MutableList<Lit>>, vars: Int = 0) : Solver {
62+
class Kosat(clauses: List<List<Lit>>, vars: Int = 0) : Solver {
6363
override val numberOfVariables get() = solver.numberOfVariables
6464
override val numberOfClauses get() = solver.constraints.size + solver.learnts.size
6565

6666
private var model: List<Lit>? = null
67-
private val solver = CDCL(clauses.map { Clause(it) }.toMutableList(), vars)
67+
private val solver = CDCL(clauses.map { Clause(it.toMutableList()) }.toMutableList(), vars)
6868

6969
override fun addVariable(): Int {
7070
solver.addVariable()
@@ -98,3 +98,4 @@ class Kosat(clauses: MutableList<MutableList<Lit>>, vars: Int = 0) : Solver {
9898
return model?.get(abs(lit) - 1) == lit
9999
}
100100
}
101+

src/jvmTest/kotlin/TestSolver.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import org.junit.jupiter.api.Test
2+
import org.junit.jupiter.api.TestInstance
3+
import org.kosat.Kosat
4+
import org.kosat.solveCnf
5+
6+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
7+
internal class TestSolver {
8+
@Test
9+
fun `test example 1`() {
10+
11+
val solver = Kosat(listOf(
12+
listOf(1, 2, -3), listOf(-1), listOf(-3)
13+
))
14+
15+
println(solver.solve())
16+
println(solver.getModel().toString())
17+
}
18+
19+
@Test
20+
fun `test example 2`() {
21+
22+
val solution = solveCnf(listOf(
23+
listOf(1, 2, -3, -4), listOf(-1), listOf(-3), listOf(3, -4)
24+
))
25+
26+
println(solution)
27+
}
28+
}

0 commit comments

Comments
 (0)