Skip to content

Commit

Permalink
Fix minor bug and atttempt to fix the broken oblique searches
Browse files Browse the repository at this point in the history
  • Loading branch information
jedlimlx committed May 7, 2024
1 parent a7b0782 commit 6848751
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
41 changes: 27 additions & 14 deletions src/commonMain/kotlin/search/cfind/CFind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class CFind(
) { -1 }.apply {
if (symmetry != ShipSymmetry.GLIDE || direction != Coordinate(1, 1)) {
for (i in 0 ..<k) {
var count = i
var count = (i*period).mod(k) // TODO fix for gcd(k, p) > 1
while (count < period * k) {
this[count] = tempOffsets[i.mod(tempOffsets.size)]
count += backOff[count.mod(period)]
Expand Down Expand Up @@ -349,7 +349,7 @@ class CFind(
listOf()
}
}.toList()
val minX = if (lookaheadNeighbourhood.isNotEmpty()) lookaheadNeighbourhood[0].minOf { (it, _) -> it.x } else 0
val minX = if (lookaheadNeighbourhood.isNotEmpty() && lookaheadNeighbourhood[0].isNotEmpty()) lookaheadNeighbourhood[0].minOf { (it, _) -> it.x } else 0

// Building lookup tables
val numEquivalentStates: Int = rule.equivalentStates.distinct().size
Expand Down Expand Up @@ -619,6 +619,10 @@ class CFind(
t.cursor.hide(showOnExit = true)
}

println(bcNeighbourhood)
println(inverseBcNeighbourhood)
println(memorisedBCsMap)

// TODO Error handling of invalid inputs, e.g. invalid symmetries
// Print a message that indicates the search is beginning
println(bold("Beginning search for width ${green("$width")} " +
Expand Down Expand Up @@ -1381,7 +1385,7 @@ class CFind(
key += rows[coordinate, 0, row, depth] * power
power *= rule.numStates

key += rows[coordinate, 1, row, depth] * power
key += rows[coordinate, 1, row, depth, currentRow] * power
}
}

Expand Down Expand Up @@ -1557,10 +1561,10 @@ class CFind(
val index: Int
val tempCoordinate = coordinate + lastBaseCoordinate
if (spacing != 1) {
val temp = tempCoordinate.x - offsets[(depth - tempCoordinate.y * period).mod(offsets.size)]
if (temp.mod(spacing) != 0) return@forEach
val temp = coordinate.x - offsets[(depth - coordinate.y * period).mod(offsets.size)]
//if (temp.mod(spacing) != 0) return@forEach

index = temp / spacing
index = tempCoordinate.x / spacing
} else index = tempCoordinate.x

// Getting the boundary state
Expand All @@ -1581,6 +1585,7 @@ class CFind(

satisfyBC = bcMemo[it]?.get(
inverseBcNeighbourhood[memorisedBCsMap[it]!!].mapIndexed { index, it ->
//println("$coordinate ${it + offset} ${offsets.toList()}")
rule.equivalentStates[
rows[it + offset, 0, cells, depth]
] * pow(numEquivalentStates, index)
Expand Down Expand Up @@ -1770,7 +1775,13 @@ class CFind(
else return Coordinate(coordinate.x * spacing + offsets[depth.mod(offsets.size)], coordinate.y)
}

private operator fun List<Row?>.get(coordinate: Coordinate, generation: Int, currentRow: IntArray? = null, depth: Int = 0): Int {
private operator fun List<Row?>.get(
coordinate: Coordinate,
generation: Int,
currentRow: IntArray? = null,
depth: Int = 0,
mostRecentRow: Row? = null
): Int {
if (coordinate.x < 0) return 0 // TODO allow different backgrounds
if (coordinate.x >= width * spacing) {
return when (symmetry) {
Expand All @@ -1783,7 +1794,7 @@ class CFind(

if (coordinate.y == 0 && currentRow != null) {
if (spacing != 1 && coordinate.x.mod(spacing) != offsets[depth.mod(offsets.size)]) {
println("crap $depth")
println("crap $depth $coordinate ${coordinate.x.mod(spacing)} ${offsets[depth.mod(offsets.size)]}")
return 0
}
return currentRow[coordinate.x / spacing]
Expand All @@ -1792,17 +1803,19 @@ class CFind(
if (generation == 0) {
if (indexToRowMap[coordinate.y] != -1) {
this[indexToRowMap[coordinate.y] - 1]?.get(coordinate.x) ?: -1
} else {
} else { // TODO optimise this
this[0]?.getPredecessor((coordinate.y - 1) * period)?.get(coordinate.x) ?: -1
}
} else if (coordinate.y == centralHeight && generation == 1) {
} else if (generation == 1) {
// TODO optimise this
val row = if (coordinate.y == centralHeight) this.last()!!
else mostRecentRow!!.getPredecessor(coordinate.y * period - backOff[depth.mod(period)] - 1)!!

if (
symmetry != ShipSymmetry.GLIDE ||
(period.mod(2) == 0 && this.last()!!.phase.mod(period) == 0)
) this.last()!![coordinate.x]
else this.last()!![width * spacing - coordinate.x - 1]
}
else -1 // means that the cell state is not known
) row[coordinate.x] else row[width * spacing - coordinate.x - 1]
} else -1 // means that the cell state is not known
} else -1
}

Expand Down
8 changes: 4 additions & 4 deletions src/commonMain/kotlin/search/cfind/Row.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class Row(
}

operator fun get(index: Int): Int {
// if (search!!.spacing != 1 && (index - offset).mod(search!!.spacing) != 0) {
// println("crap $depth $index $offset")
// return 0
// }
if (search!!.spacing != 1 && (index - offset).mod(search!!.spacing) != 0) {
//println("crap $depth $index $offset")
return 0
}
if (search!!.spacing == 1) return cells[index]
else return cells[index / search!!.spacing]
}
Expand Down
9 changes: 6 additions & 3 deletions src/jvmMain/kotlin/Main.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import kotlinx.io.writeString
import rules.hrot.HROT
import rules.hrot.HROTGenerations
import rules.nontotalistic.rules.INT
import rules.nontotalistic.rules.INTGenerations
import rules.ruleloader.builders.ruletable
Expand Down Expand Up @@ -120,9 +121,11 @@ actual fun main() {
// )
// search.search()

val search = CFind(
HROT("R2,C2,S6-9,B7-8,NM"), 1, 1, 9, ShipSymmetry.EVEN,
verbosity = 1, searchStrategy = SearchStrategy.HYBRID_BFS
//B2-ei3cjkr4cektyz5-cnr6-ik78/S01e2-ae3cnqry4cqrtwyz5-ain6ekn7e
val search = CFind(
HROTGenerations("23/3/3"), 2, 1, 10, ShipSymmetry.ODD,
verbosity = 1, searchStrategy = SearchStrategy.PRIORITY_QUEUE, //direction = Coordinate(1, 2),
//lookaheadDepth = 0, isotropic = false
)
search.search()

Expand Down

0 comments on commit 6848751

Please sign in to comment.