Skip to content

Commit

Permalink
add larger radius example to unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Jan 14, 2025
1 parent 0ac85e6 commit 54a5ea0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/FSA.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,29 @@ open class FSA constructor(open val Q: TSA, open val init: Set<Σᐩ>, open val
// println(dp.joinToString("\n") { it.joinToString(" ") { it.joinToString("", "[", "]") { if (it) "1" else "0"} } })

val startIdx = cfg.bindex[START_SYMBOL]
var fresh = true
while (fresh) {
fresh = false
for (p in 0 until levFSA.numStates)
for (q in (p + 1) until levFSA.numStates)
for ((w, /*->*/ x, z) in cfg.tripleIntProds)
if (!dp[p][q][w])
for (r in levFSA.allPairs[p to q] ?: emptySet<Int>())
if (dp[p][r][x] && dp[r][q][z]) {
// println("Found: ${levFSA.stateLst[p]} ${levFSA.stateLst[q]} / ${cfg.bindex[w]} -> ${cfg.bindex[x]} ${cfg.bindex[z]}")
if (p == 0 && w == startIdx && q in levFSA.finalIdxs) return true
dp[p][q][w] = true
fresh = true
break
}

// For pairs (p,q) in topological order by (rank(q) - rank(p)):
for (dist in 1..levFSA.numStates-1) {
for (iP in 0 until levFSA.numStates - dist) {
val p = iP
val q = iP + dist
// For each A -> B C
for ((A, B, C) in cfg.tripleIntProds) {
if (!dp[p][q][A]) {
// Check possible midpoints r in [p+1, q-1]
// or in general, r in levFSA.allPairs[p->q]
for (r in (levFSA.allPairs[p to q] ?: emptySet())) {
if (dp[p][r][B] && dp[r][q][C]) {
if (p == 0 && A == startIdx && q in levFSA.finalIdxs) return true
dp[p][q][A] = true
// We don't need fresh = true, because once we pass this step,
// we won't come back to (p,q) in a later sweep
break
}
}
}
}
}
}

// println("AFTER (sum=${dp.sumOf { it.sumOf { it.sumOf { if(it) 1.0 else 0.0 } } }})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,14 @@ class BarHillelTest {
measureTimedValue { FSA.nonemptyLevInt(str, Grammars.dyck, led - 1) }
.also { println("${it.value} / ${it.duration}") }
.also { assertFalse(it.value) }

val pythonCode = "NAME = [ ( STRING , STRING ) , ( STRING , STRING ) , ( STRING , STRING ) , ( STRING , NAME ) , , ( NAME , NAME ) , , ( NAME , STRING ) , , ( NAME , NAME ) ]"
measureTimedValue { FSA.nonemptyLevInt(pythonCode, vanillaS2PCFG, 4) }
.also { println("${it.value} / ${it.duration}") }
.also { assertTrue(it.value) }

measureTimedValue { FSA.nonemptyLevInt(pythonCode, vanillaS2PCFG, 3) }
.also { println("${it.value} / ${it.duration}") }
.also { assertFalse(it.value) }
}
}

0 comments on commit 54a5ea0

Please sign in to comment.