Skip to content

Commit

Permalink
Slightly improvre speed by including some boundary conditions inside …
Browse files Browse the repository at this point in the history
…appproximate lookahead
  • Loading branch information
jedlimlx committed May 9, 2024
1 parent de8d42d commit 4d1b37c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
35 changes: 22 additions & 13 deletions src/commonMain/kotlin/search/cfind/CFind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CFind(
count += this[count.mod(period)]
}

this[count.mod(period)] = (backoffPeriod - count).fmod(backoffPeriod)
this[count.mod(period)] = period * k - count
}
val fwdOff = run {
val array = IntArray(period) { -1 }
Expand Down Expand Up @@ -336,7 +336,7 @@ class CFind(
val maxWidth: Int = widthsByHeight.slice(0 .. this.lookaheadDepth).maxOf { it }

// TODO fix approximate lookahead for spacing != 1
val approximateLookahead = spacing == 1 && false && lookaheadDepth > 0 && lookaheadIndices[0][0].last() != 0
val approximateLookahead = spacing == 1 && lookaheadDepth > 0 && lookaheadIndices[0][0].last() != 0

// Computing neighbourhoods to be memorised for lookahead
val memorisedlookaheadNeighbourhood: List<List<Pair<Coordinate, Int>>> = lookaheadIndices.indices.map {
Expand Down Expand Up @@ -420,7 +420,7 @@ class CFind(
}
}

val combinedSuccessorArray: BooleanArray = run {
val combinedSuccessorArray: IntArray = run {
if (verbosity >= 0 && !stdin) {
t.cursor.move {
up(1)
Expand All @@ -431,19 +431,28 @@ class CFind(
}
println("Generating approximate lookahead lookup table...", verbosity = 0)

BooleanArray(
IntArray(
pow(rule.numStates, neighbourhood[0].size - baseCoordinates.size + 2)
) {

var output = false
for (i in successorTable[it].indices) {
if (successorTable[it][i] != 0) {
output = true
break
var num = 0
for (j in 0..reversedBaseCoordinate.size) {
var output = false
for (i in successorTable[it].indices) {
if (
(
j == reversedBaseCoordinate.size ||
i and ((2 shl (reversedBaseCoordinate.size - j - 1)) - 1) shl j == 0
) && successorTable[it][i] != 0
) {
output = true
break
}
}

if (output) num += 1 shl j
}

output
num
}
}

Expand Down Expand Up @@ -1513,7 +1522,7 @@ class CFind(
val approximateLookaheadRows: List<Row?>
val lookaheadDepthDiff = if (lookaheadDepth < this.lookaheadDepth) {
approximateLookaheadRows = lookaheadRows[0]
if (lookaheadDepth - 1 >= 0) {
if (lookaheadDepth >= 1) {
lookaheadIndices[lookaheadDepth - 1][depth.mod(period)].filter { it > 0 }.min()
} else minIndices[depth.mod(period)]
} else {
Expand Down Expand Up @@ -1553,7 +1562,7 @@ class CFind(
power *= rule.numStates

key += approximateLookaheadRows[coordinate, 1, null, depth] * power
return combinedSuccessorArray[key]
return combinedSuccessorArray[key] and (1 shl minOf(index, reversedBaseCoordinate.size)) != 0
}

// Checks boundary conditions
Expand Down
8 changes: 5 additions & 3 deletions src/jvmMain/kotlin/Main.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ actual fun main() {

// B2-ei3cjkr4cektyz5-cnr6-ik78/S01e2-ae3cnqry4cqrtwyz5-ain6ekn7e
val search = CFind(
HROT("R2,C2,S2-3,B3,N@891891"), 4, 3, 10, ShipSymmetry.GLIDE,
verbosity = 1, searchStrategy = SearchStrategy.PRIORITY_QUEUE, direction = Coordinate(1, 1),
lookaheadDepth = 1//, isotropic = false
HROT("R3,C2,S6-9,B7-8,NN"), 2, 1, 8, ShipSymmetry.EVEN,
verbosity = 1, searchStrategy = SearchStrategy.HYBRID_BFS,
numShips = 1
//direction = Coordinate(1, 1),
//lookaheadDepth = 1, //, isotropic = false
)
search.search()

Expand Down

0 comments on commit 4d1b37c

Please sign in to comment.