Skip to content

Commit

Permalink
sort valiant actually faster than seqvaliant for small alphabets
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 2, 2023
1 parent 0ce0fb8 commit 76591ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun CFG.sortwiseAlgebra(metric: ChoiceMetric): Ring<Sort> =
times = { x, y -> join(x, y, metric) },
)

const val MAX_CAPACITY = 100
var MAX_SORT_CAPACITY = 1000
// X ⊗ Z := { w | <x, z> ∈ X × Z, (w -> xz) ∈ P }
// Greedily selects candidate string fragments according to ChoiceMetric
fun CFG.join(X: Sort, Z: Sort, metric: ChoiceMetric = { it.weight }): Sort =
Expand All @@ -66,7 +66,7 @@ fun CFG.join(X: Sort, Z: Sort, metric: ChoiceMetric = { it.weight }): Sort =
val list = acc ?: mutableListOf()
val idx = list.binarySearch(choice, Choice.comparator)
if (idx < 0) list.add(-idx - 1, choice) // Only if not already present
list.apply { if (MAX_CAPACITY < size) removeLast() }
list.apply { if (MAX_SORT_CAPACITY < size) removeLast() }
}.mapValues { it.value.toSet() }

fun union(l: Sort, r: Sort): Sort =
Expand Down Expand Up @@ -94,7 +94,7 @@ data class Choice(val tokens: List<Σᐩ>, val weight: Float): Comparable<Choice
override fun compareTo(other: Choice): Int = comparator.compare(this, other)

operator fun plus(other: Choice) =
Choice(tokens + other.tokens, weight + other.weight)
Choice(sanitized + other.sanitized, weight + other.weight)

val sanitized by lazy { tokens.filter { "ε" !in it } }
val asString by lazy { sanitized.joinToString(" ") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class SetValiantTest {
val leaves = tree.contents()
assertEquals(expr, leaves)

val holExpr = "_ _ _ _ _ _ _ _ _ _"
val holExpr = "_ _ _ _"

measureTime {
val solutions = ocamlCFG.solve(holExpr, levMetric("( false curry )"))
Expand Down Expand Up @@ -864,19 +864,19 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma
println("Grammar size: ${bhcfg.size}")
println("Solutions:")

val template = "_ _ _ _ _ _ _"
measureTime {
template.tokenizeByWhitespace()
.solve(bhcfg, fillers = bhcfg.terminals)
.map { it.replace("ε", "").tokenizeByWhitespace().joinToString(" ") }
.distinct()
.onEach { println(it) }
.toList().also { println("Found ${it.size} solutions.") }
}.also { println("Brute force solver took: ${it.inWholeMilliseconds}ms") }
val template = List(53) { "_" }.joinToString(" ")
// measureTime {
// template.tokenizeByWhitespace()
// .solve(bhcfg, fillers = bhcfg.terminals)
// .map { it.replace("ε", "").tokenizeByWhitespace().joinToString(" ") }
// .distinct()
// .onEach { println(it) }
// .toList().also { println("Found ${it.size} solutions.") }
// }.also { println("Brute force solver took: ${it.inWholeMilliseconds}ms") }

measureTime {
// bhcfg.solve(template, {it.weight})
bhcfg.solveSeq(template)
bhcfg.solve(template, {it.weight})
// bhcfg.solveSeq(template)
.onEach { assertTrue { it in bhcfg.language }; println(it) }
.toList().also { println("Found ${it.size} solutions.") }
}.also { println("Sequential solver took: ${it.inWholeMilliseconds}ms") }
Expand Down

0 comments on commit 76591ae

Please sign in to comment.