From b1274e01d864395ea9dbf99476435d7514063f42 Mon Sep 17 00:00:00 2001 From: Lim Li Xin Jed <42632089+jedlimlx@users.noreply.github.com> Date: Fri, 10 May 2024 10:35:46 +0000 Subject: [PATCH] Fix searches for gcd(k, p) > 1 --- src/commonMain/kotlin/search/cfind/CFind.kt | 30 ++++++++++++--------- src/commonMain/kotlin/search/cfind/Row.kt | 3 ++- src/jvmMain/kotlin/Main.jvm.kt | 10 +++---- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/commonMain/kotlin/search/cfind/CFind.kt b/src/commonMain/kotlin/search/cfind/CFind.kt index 27efbd9..8b4b2b5 100644 --- a/src/commonMain/kotlin/search/cfind/CFind.kt +++ b/src/commonMain/kotlin/search/cfind/CFind.kt @@ -58,7 +58,6 @@ class CFind( val partialFileFrequency = if (partialFileFrequency > 0) partialFileFrequency else partialFrequency // TODO Handle alternating stuff properly / don't support alternating neighbourhoods - // TODO Fix photon searches var minDeepeningIncrement = if (minDeepeningIncrement == -1) { if (searchStrategy == SearchStrategy.HYBRID_BFS) _period else _period * 10 // shallow tree with deep leafs :) @@ -95,6 +94,9 @@ class CFind( } this[count.mod(period)] = period * k - count + + val temp = this.toList() + temp.forEachIndexed { index, it -> this[(index + 1).mod(period)] = it } } val fwdOff = run { val array = IntArray(period) { -1 } @@ -115,7 +117,7 @@ class CFind( } val offsets = IntArray(this.period * this.k * (if (symmetry == ShipSymmetry.GLIDE && direction == Coordinate(1, 1)) 2 else 1) - ) { -1 }.apply { + ) { 0 }.apply { if (symmetry != ShipSymmetry.GLIDE || direction != Coordinate(1, 1)) { for (i in 0 .. 1 @@ -269,14 +271,15 @@ class CFind( if (pos < indices[0].size - 1) lst.last()[phase].map { it - temp }.toIntArray() else { - val fwdOffset = fwdOff[(phase - temp).mod(period)] + val fwdOffset = fwdOff[phase.mod(period)] lst.last()[phase].map { - if (it - temp != 0) it - temp + backOff[phase] - fwdOffset + if (it - temp != 0) it - (lst.last()[phase][0] - fwdOffset) else 0 }.toIntArray() } } ) + println(lst.last().map { it.toList() }.toList()) } lst.subList(1, lst.size) @@ -315,7 +318,7 @@ class CFind( val lookaheadDepth = minOf(lookaheadDepth, maxLookaheadDepth) val successorLookaheadDepth = tempIndices.map { it[0].last() }.indexOf(0) + 1 - val successorLookahead = successorLookaheadDepth == this.lookaheadDepth + 1 && lookaheadDepth > 0 + val successorLookahead = (successorLookaheadDepth == this.lookaheadDepth + 1 || tempIndices[0][1].last() == 0) && lookaheadDepth > 0 val lookaheadIndices = if (this.lookaheadDepth == 0) listOf() else tempIndices.subList( 0, if (successorLookahead) successorLookaheadDepth else this.lookaheadDepth @@ -1349,7 +1352,7 @@ class CFind( if (coordinate !in neighbourhoodWithoutBg) { neighbourhoodWithoutBg[coordinate] = mainNeighbourhood.filter { (it, _) -> if (symmetry == ShipSymmetry.ASYMMETRIC || symmetry == ShipSymmetry.GLIDE) - (it + coordinate).x in 0 ..< (width * spacing) + 0 <= (it + coordinate).x && (it + coordinate).x < (width * spacing) else (it + coordinate).x >= 0 }.map { (it, p) -> Pair(it + coordinate, p) }.toList() } @@ -1522,9 +1525,9 @@ class CFind( val approximateLookaheadRows: List val lookaheadDepthDiff = if (lookaheadDepth < this.lookaheadDepth) { approximateLookaheadRows = lookaheadRows[0] - if (lookaheadDepth >= 1) { + if (lookaheadDepth >= 1) lookaheadIndices[lookaheadDepth - 1][depth.mod(period)].filter { it > 0 }.min() - } else minIndices[depth.mod(period)] + else minIndices[depth.mod(period)] } else { approximateLookaheadRows = listOf() 0 @@ -1545,12 +1548,15 @@ class CFind( _lookaheadMemo[index] = key } else key = _lookaheadMemo[index] - + + //println(lookaheadNeighbourhood[0].toList()) for ((it, p) in lookaheadNeighbourhood[0]) { + //println("$it ${pow(numEquivalentStates, (it.x + minX) / spacing)}") if ((it + coordinate).x >= 0) // TODO consider different backgrounds key += getDigit( row, - pow(numEquivalentStates, (it.x + minX) / spacing), numEquivalentStates + pow(numEquivalentStates, (it.x + minX) / spacing), + numEquivalentStates ) * p } @@ -1720,8 +1726,8 @@ class CFind( val row = Row(currentRow, node.completeRow!!, this) if (lookaheadDepth < this.lookaheadDepth) { val newRows = lookaheadRows.mapIndexed { index, rows -> - val temp = lookaheadIndices[lookaheadDepth][depth.mod(period)].min() // TODO may not be legit - val tempIndices = lookaheadIndices[index + lookaheadDepth][depth.mod(period)] + val temp = lookaheadIndices[lookaheadDepth][(depth + 2).mod(period)].min() // TODO may not be legit + val tempIndices = lookaheadIndices[index + lookaheadDepth][(depth + 2).mod(period)] rows.mapIndexed { index, value -> if (tempIndices[index] == temp) row else value } } diff --git a/src/commonMain/kotlin/search/cfind/Row.kt b/src/commonMain/kotlin/search/cfind/Row.kt index da849d8..86b6055 100644 --- a/src/commonMain/kotlin/search/cfind/Row.kt +++ b/src/commonMain/kotlin/search/cfind/Row.kt @@ -2,6 +2,7 @@ package search.cfind import simulation.Coordinate import simulation.DenseGrid +import simulation.SparseGrid import simulation.Grid class Row( @@ -115,7 +116,7 @@ class Row( fun isEmpty(): Boolean = hash == 0 fun toGrid(period: Int, symmetry: ShipSymmetry): Grid { - val grid = DenseGrid() + val grid = SparseGrid() var temp: Row? var predecessor = this diff --git a/src/jvmMain/kotlin/Main.jvm.kt b/src/jvmMain/kotlin/Main.jvm.kt index bbbdd2b..0668f07 100644 --- a/src/jvmMain/kotlin/Main.jvm.kt +++ b/src/jvmMain/kotlin/Main.jvm.kt @@ -122,12 +122,10 @@ actual fun main() { // search.search() // B2-ei3cjkr4cektyz5-cnr6-ik78/S01e2-ae3cnqry4cqrtwyz5-ain6ekn7e - val search = CFind( - 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 + val search = CFind( + HROT("R2,C2,S2,B3,NN"), 4, 2, 5, ShipSymmetry.EVEN, + verbosity = 1, searchStrategy = SearchStrategy.PRIORITY_QUEUE, //numShips = 1, + lookaheadDepth = 1, //numShips = 1, //, direction = Coordinate(1, 1) ) search.search()