Skip to content

Commit

Permalink
Almost fix diagonal searches for von neumann neighbourhood
Browse files Browse the repository at this point in the history
  • Loading branch information
jedlimlx committed May 2, 2024
1 parent cbe7dc3 commit f20fd03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions src/commonMain/kotlin/search/cfind/CFind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,14 @@ class CFind(
0, if (successorLookahead) successorLookaheadDepth else this.lookaheadDepth
)

val additionalDepth: Int = when (indices[0].indexOf(indices[0].min())) {
val rawAdditionalDepth: Int = when (indices[0].indexOf(indices[0].min())) {
0 -> neighbourhood[0].filter { it.y == baseCoordinates[0].y + 1 }.maxOf{ it.x } + 1 - baseCoordinates.last().x
indices[0].size - 1 -> neighbourhood[0].filter { it.y == 0 }.maxOf{ it.x } + 1 - baseCoordinates.last().x
else -> -1
}
val additionalDepthArray = IntArray(spacing) {
((rawAdditionalDepth - 1) + (offsets[(it + 1).mod(spacing)] - offsets[it])) / spacing + 1
}
val maxWidth: Int = widthsByHeight.slice(0 .. this.lookaheadDepth).maxOf { it }

val approximateLookahead = spacing == 1 && lookaheadDepth > 0 && lookaheadIndices[0][0].last() != 0
Expand Down Expand Up @@ -329,7 +332,7 @@ class CFind(
}

val cacheWidths: IntArray = IntArray(height) {
pow(numEquivalentStates, (widthsByHeight[it] - (if (it == 0) 1 else 0) + spacing - 1) / spacing)
pow(numEquivalentStates, widthsByHeight[it] - (if (it == 0) 1 else 0))
}

val successorTable: Array<IntArray> = run {
Expand Down Expand Up @@ -604,7 +607,7 @@ class CFind(
println((bold("Maximum Lookahead Depth: ") + "$maxLookaheadDepth"), verbosity = 1)
println((bold("Successor Lookahead: ") + "$successorLookaheadDepth / $successorLookahead"), verbosity = 1)
println((bold("Approximate Lookahead: ") + "$approximateLookahead"), verbosity = 1)
println((bold("Additional Depth (for lookahead): ") + "$additionalDepth"), verbosity = 1)
println((bold("Additional Depth (for lookahead): ") + "${additionalDepthArray.toList()}"), verbosity = 1)
println((bold("Lookahead Depth: ") + "$lookaheadDepth"), verbosity = 1)
println(
(
Expand Down Expand Up @@ -1406,7 +1409,7 @@ class CFind(
else indices[depth.mod(period)].min()
} else 0
fun approximateLookahead(index: Int, row: Int): Boolean {
val index = index - additionalDepth
val index = index - additionalDepthArray[depth.mod(spacing)]
if (index < 0) return true

val depth = depth + lookaheadDepthDiff
Expand All @@ -1426,7 +1429,6 @@ class CFind(
for ((it, p) in lookaheadNeighbourhood[0]) {
if ((it + coordinate).x >= 0) // TODO consider different backgrounds
key += getDigit(row, pow(numEquivalentStates, (it.x + minX) / spacing), numEquivalentStates) * p
// TODO consider special case for diagonal / oblique ships
}

_lookaheadMemo2!![index] = key
Expand Down Expand Up @@ -1468,13 +1470,6 @@ class CFind(
// Finally checking the boundary condition
if (((lookupTable[encodeKey(coordinate, cells)] shr boundaryState) and 0b1) != 1) {
satisfyBC = false
if (it !in filteredLeftBCs) {
println("$it $coordinate $tempCoordinate")
println("a ${lookup(index).toList()} ${encodeKey(coordinate, cells)} ${rows[tempCoordinate, 0, cells, depth]} ${(lookupTable[encodeKey(coordinate, cells)] shr boundaryState) and 0b1}")
println("b ${lookup(width-1).toList()} " +
"${encodeKey(Coordinate(5, 2), cells)} ${rows[Coordinate(7, 0), 0, cells, depth]} ${(lookup(width-1)[encodeKey(Coordinate(5, 2), cells)] shr rows[Coordinate(7, 0), 0, cells, depth]) and 0b1}")
println(node.predecessor!!.cells)
}
return@forEach
}
} else {
Expand Down Expand Up @@ -1560,8 +1555,8 @@ class CFind(
symmetry != ShipSymmetry.GLIDE ||
(period.mod(2) == 0 && rows.last().phase.mod(period) == 1)
) {
//if (depthToCheck + additionalDepth < node.depth) continue
//else depthToCheck = Int.MAX_VALUE - 1000
if (depthToCheck + additionalDepthArray[depth.mod(spacing)] < node.depth) continue
else depthToCheck = Int.MAX_VALUE - 1000

// Run the approximate lookahead
if (
Expand All @@ -1588,7 +1583,7 @@ class CFind(
!checkBoundaryCondition(node, filteredRightBCs)
) continue

if (checkBoundaryCondition(node, leftBC, offset=Coordinate(width * spacing - 1, 0))) {
if (checkBoundaryCondition(node, filteredLeftBCs, offset=Coordinate(width * spacing - 1, 0))) {
// Running the lookahead
val row = Row(currentRow, node.completeRow, this)
if (lookaheadDepth < this.lookaheadDepth) {
Expand Down Expand Up @@ -1635,6 +1630,10 @@ class CFind(
((stateMask shr i) and 0b1) == 1 &&
(node.depth + 1 == width || !pruneNodes(node, newKey))
) {
// if (node.depth == width - 1 && stateMask != 0) {
// println("${lookup(node.depth).toList()} ${node.cells.mod(cacheWidths[0])} $stateMask")
// }

// Adding the new nodes to the stack
deadend = false
stack.add(
Expand Down
4 changes: 2 additions & 2 deletions src/jvmMain/kotlin/Main.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ actual fun main() {

val search = CFind(
HROT("R2,C2,S9-14,B9-14,NW0010003330130310333000100"), 3, 1, 4, symmetry = ShipSymmetry.ODD,
verbosity = 1, searchStrategy = SearchStrategy.PRIORITY_QUEUE, partialFrequency = 1000,
verbosity = 1, searchStrategy = SearchStrategy.PRIORITY_QUEUE, partialFrequency = 10000,
backupName = "dump", maxQueueSize = 1 shl 22, numThreads = 1, direction = Coordinate(1, 1),
backupFrequency = 600, lookaheadDepth = 0
backupFrequency = 600
)
search.search()

Expand Down

0 comments on commit f20fd03

Please sign in to comment.