Skip to content

Commit

Permalink
Revert PRIORITY_QUEUE search method to a more BFS-like search
Browse files Browse the repository at this point in the history
  • Loading branch information
jedlimlx committed Jun 22, 2024
1 parent 7077a3b commit 08c1233
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/commonMain/kotlin/search/cfind/CFind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CFind(
val partialFrequency: Int = 1000,
val backupFrequency: Int = 60*15,
val backupName: String = "dump",
transpositionTableSize: Int = 1 shl 25,
val transpositionTableSize: Int = Int.MAX_VALUE,
val maxTimePerRound: Int = 5*60,
val numThreads: Int = 8,
val stdin: Boolean = false,
Expand Down Expand Up @@ -471,9 +471,13 @@ class CFind(
while (count in skippedRows) count++
neighbourhood[0].filter { it.y == baseCoordinates[0].y + count }.maxOf{ it.x } + 1 - baseCoordinates.last().x
}
indices[0].size - 1 -> neighbourhood[0].filter { it.y == 0 }.maxOf{ it.x } + 1 - baseCoordinates.last().x
indices[0].size - 1 -> {
if (neighbourhood[0].count { it.y == 0 } == 0) 1 - baseCoordinates.last().x
else 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
}
Expand All @@ -489,7 +493,7 @@ class CFind(
while (count in skippedRows) count++
mainNeighbourhood.filter { (it, _) -> it.y == -centralHeight + count }
}
val minX = lookaheadNeighbourhood.minOf { (it, _) -> it.x }
val minX = if (lookaheadNeighbourhood.isNotEmpty()) lookaheadNeighbourhood.minOf { (it, _) -> it.x } else 0

// Building lookup tables
val numEquivalentStates: Int = rule.equivalentStates.distinct().size
Expand Down Expand Up @@ -1403,14 +1407,15 @@ class CFind(
fun checkEquivalentState(row: Row): Boolean {
val rows = row.getAllPredecessors((height - 1) * period)

val useReverseHash = isotropic && (symmetry == ShipSymmetry.GLIDE || symmetry == ShipSymmetry.ASYMMETRIC)

val hash = rows.map { it.hashCode() }.hashCode()
val reverseHash = rows.map { it.reverseHashCode() }.hashCode()
val reverseHash = if (useReverseHash) rows.map { it.reverseHashCode() }.hashCode() else 0
fun addState() {
val temp = rows.map { it.hash.toUShort() }.toUShortArray()
equivalentStates[hash] = temp
if (isotropic && (symmetry == ShipSymmetry.GLIDE || symmetry == ShipSymmetry.ASYMMETRIC)) {
if (useReverseHash)
equivalentStates[reverseHash] = rows.map { it.reverseHash.toUShort() }.toUShortArray()
}
}

if (hash in equivalentStates.keys) {
Expand All @@ -1427,11 +1432,7 @@ class CFind(
addState()
return false
}
} else if (
isotropic &&
(symmetry == ShipSymmetry.GLIDE || symmetry == ShipSymmetry.ASYMMETRIC) &&
reverseHash in equivalentStates.keys
) {
} else if (useReverseHash && reverseHash in equivalentStates.keys) {
var equivalent = true
val state = equivalentStates[reverseHash]!!
for (i in state.indices) {
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/search/cfind/Row.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class Row(

override fun hashCode() = hash + 293 * depth.mod(depthPeriod)
fun reverseHashCode() = reverseHash + 293 * depth.mod(depthPeriod)
override fun compareTo(other: Row): Int = -this.depth + other.depth
override fun compareTo(other: Row): Int = this.depth - other.depth

override fun equals(other: Any?): Boolean {
if (this === other) return true
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 @@ -210,9 +210,9 @@ actual fun main() {
//INTGenerations("023456/0123456/3"), 3, 1, 7, ShipSymmetry.ODD,

val search = CFind(
HROT("R2,C0,S3-4,6,B0-7,N+"), 4, 1, 6, ShipSymmetry.ODD,
HROT("B02/S2V"), 4, 1, 13, ShipSymmetry.ODD,
verbosity = 1, searchStrategy = SearchStrategy.PRIORITY_QUEUE,
transpositionTableSize = 1 shl 31, direction = Coordinate(1, 1)
transpositionTableSize = 1 shl 23, direction = Coordinate(1, 1)
)
search.search()
}

0 comments on commit 08c1233

Please sign in to comment.