Skip to content

Commit

Permalink
test non-emptiness latency for nontrivial intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 8, 2024
1 parent d8128fe commit 2514260
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object Grammars {
S -> X | Y | Z
""".parseCFG().noNonterminalStubs

val dyck = """S -> ( ) | ( S ) | S S""".parseCFG().noEpsilonOrNonterminalStubs
val deadSimple = """S -> ( ) | ( S )""".parseCFG().noEpsilonOrNonterminalStubs
val dsNorm = """
START -> START START
Expand Down
12 changes: 7 additions & 5 deletions src/jvmMain/kotlin/ai/hypergraph/kaliningraph/automata/JFSA.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ data class FSATrajectory(val traj: List<Σᐩ?>, val lastState: BState, val scor

fun BAutomaton.min(): BAutomaton = minimize(this)

fun PTree.toDFA(minimize: Boolean = false) =
fun PTree.toDFA(
minimize: Boolean = false,
unitRule: (String) -> dk.brics.automaton.Automaton = {
BAutomaton.makeChar(Random(it.hashCode()).nextInt().toChar())
}
) =
measureTimedValue {
BAutomaton.setMinimization(MINIMIZE_BRZOZOWSKI)
var i = 0
Expand All @@ -68,10 +73,7 @@ fun PTree.toDFA(minimize: Boolean = false) =
else if (i++ % 13 == 0) a.concatenate(b).min() else a.concatenate(b) },
either = { a, b -> if (a == null) b else if (b == null) a
else if (j++ % 13 == 0) a.union(b).min() else a.union(b) },
unit = { a ->
if ("ε" in a.root) null
else BAutomaton.makeChar(Random(a.root.hashCode()).nextInt().toChar())
}
unit = { a -> if ("ε" in a.root) null else unitRule(a.root) }
)
}.also { println("Took ${it.duration} to build FSA") }.value
?.also { println("Original automata had ${it
Expand Down
24 changes: 24 additions & 0 deletions src/jvmTest/kotlin/ai/hypergraph/kaliningraph/automata/WFSATest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import net.jhoogland.jautomata.*
import net.jhoogland.jautomata.operations.Concatenation
import net.jhoogland.jautomata.semirings.RealSemiring
import java.io.File
import kotlin.system.measureTimeMillis
import kotlin.test.*
import kotlin.time.measureTimedValue

Expand Down Expand Up @@ -72,6 +73,29 @@ class WFSATest {
println(BAutomaton.minimize(ag.also { it.determinize() }).toDot())
}

/*
./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.automata.WFSATest.testIntersectionNonemptiness"
*/
@Test
fun testIntersectionNonemptiness() {
val ab = BAutomaton.makeString("(").concatenate(BAutomaton.makeString(")"))
val aa = BAutomaton.makeString("(").concatenate(BAutomaton.makeString("("))
val ac = BAutomaton.makeString("(")
val az = RegExp("(\\(+(\\(+\\)+){3,15}\\)+)+").toAutomaton()

val ds = Grammars.dyck.startPTree(List(20) { "_" })!!
.toDFA(false) { BAutomaton.makeString(it) }!!

val a = ab.union(aa).union(ac)
val ag = a.repeat(19, 25).union(az)
println("SA:" + ag.getShortestExample(true))
println("SD:" + ds.getShortestExample(true))
println("SS:" + ds.intersection(ag).getShortestExample(true))

measureTimeMillis { println(!ds.intersection(ag).isEmpty) }
.also { println("Time: $it ms") }
}

/*
./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.automata.WFSATest.testPTreeVsWFSA"
*/
Expand Down

0 comments on commit 2514260

Please sign in to comment.