ok, let's try and improve the skip solution

This commit is contained in:
Joachim 2018-08-25 10:48:39 +02:00
parent af89c48970
commit 60b2b3bb1a
6 changed files with 54 additions and 23 deletions

View File

@ -5,29 +5,25 @@ import kotlin.math.min
/**
* Counter for X digits of a given base.
*/
class Counter(size: Int, val base: Int = 10) {
open class Counter(size: Int, val base: Int = 10) {
/**
* Empty counter, all 0 values for each digit.
*/
private var counter: Array<Int> = Array(size) { 0 }
protected var counter: Array<Int> = Array(size) { 0 }
/**
* The last (highest significance) index that overflowed and has been changed in the counter. Could be null, if it never overflowed.
* Start with saying that everything changed.
*/
private var lastModifiedIndex: Int = 0
fun getLastModifiedIndex(): Int {
return this.lastModifiedIndex
}
var lastModifiedIndex: Int = 0
/**
* Increase the counter.
*
* @return true if the increase happened, false if we hit the ceiling.
*/
fun increase(): Boolean {
open fun increase(): Boolean {
if (atMax()) {
return false
}

View File

@ -0,0 +1,41 @@
package be.nielandt
import kotlin.math.min
/**
* Counter for X digits of a given base. Skips situations where faces are the same.
*/
class CounterSkipSameFaces(size: Int, base: Int = 10): Counter(size, base) {
/**
* Increase the counter.
*
* @return true if the increase happened, false if we hit the ceiling.
*/
override fun increase(): Boolean {
var lmi = lastModifiedIndex
var last = super.increase()
lmi = min(lastModifiedIndex, lmi)
// are we having an invalid situation? this would be two consecutive moves on the same face
while (containsConsecutiveSameFaceMoves() && !atMax()) {
last = super.increase()
lmi = min(lastModifiedIndex, lmi)
}
// we have to set the lastmodified index to the lowest point that it got to... otherwise we might be skipping some cases
this.lastModifiedIndex = lmi
return last
}
/**
* Are there two moves in the current counter / chain that act on the same face? This would be F+F2 for example.
*/
private fun containsConsecutiveSameFaceMoves(): Boolean {
for (i in 1 until this.counter.size) {
val current = Move.values()[this.counter[i]]
val previous = Move.values()[this.counter[i - 1]]
if (current sameFace previous)
return true
}
return false
}
}

View File

@ -67,17 +67,20 @@ fun main(args: Array<String>) {
// scramble random
val moves = Move.random(20)
val moves = Move.random(10)
println("Scramble: $moves")
val scrambledModel = EdgeModel(moves)
println(scrambledModel)
val baseSolve = CrossSolverBase().solveCrossesTimed(scrambledModel)
CrossSolver.printResults(baseSolve)
// val baseSolve = CrossSolverBase().solveCrossesTimed(scrambledModel)
// CrossSolver.printResults(baseSolve)
val upgradedSolve = CrossSolverUpgraded().solveCrossesTimed(scrambledModel)
CrossSolver.printResults(upgradedSolve)
val upgradedSolveSkip = CrossSolverUpgradedSkip().solveCrossesTimed(scrambledModel)
CrossSolver.printResults(upgradedSolveSkip)
// val allCrossMoveCountUpgradedSkip = allCrossMoveCountUpgradedSkip(scrambledModel)
// allCrossMoveCountUpgradedSkip.forEach { color, moves ->
// println("skip upgrade cross for color: ${color} in ${moves.size}: ${moves.joinToString(" ")}")

View File

@ -1,16 +1,12 @@
package be.nielandt
import java.time.Instant
/**
* This solver avoids redoing edgemodel manipulations. Should be equivalent to X nested for loops.
*/
class CrossSolverUpgraded : CrossSolver() {
override fun solveCrosses(edgeModel: EdgeModel): Map<Int, List<Move>> {
val start = Instant.now()
val moveCounts = mutableMapOf<Int, List<Move>>()
for (moveCount in 1..8) {
println("all cross move count upgrade doing $moveCount")
// build a counter of moveCount big

View File

@ -7,16 +7,12 @@ class CrossSolverUpgradedSkip : CrossSolver() {
override fun solveCrosses(edgeModel: EdgeModel): Map<Int, List<Move>> {
val moveCounts = mutableMapOf<Int, List<Move>>()
for (moveCount in 1..8) {
println("all cross move count upgrade doing $moveCount")
// build a counter of moveCount big
val counter = Counter(moveCount, Move.values().size)
// TODO skip the counter
val counter = CounterSkipSameFaces(moveCount, Move.values().size)
val edgeModelFactory = EdgeModelFactory(edgeModel, counter)
println("moveCounts = ${moveCounts}")
while (edgeModelFactory.hasNext()) {
// get the next model, using the internal counter which simply iterates over possible combinations of moves
val next = edgeModelFactory.getNext()
@ -34,7 +30,6 @@ class CrossSolverUpgradedSkip : CrossSolver() {
}
// break if we have found hem all
if (moveCounts.keys.size == 6) {
// println("counter.skipInvalidCount = ${counter.skipInvalidCount}")
return moveCounts
}
}

View File

@ -28,12 +28,12 @@ class EdgeModelFactory(val original: EdgeModel, val counter: Counter) {
fun getNext(): EdgeModel {
// the counter was increased, hooray
val lastOverflowIndex = counter.getLastModifiedIndex()
val lastOverflowIndex = counter.lastModifiedIndex
// we only need to redo everything starting from the lastoverflowindex
// these are our moves, but we can salvage everything up to lastoverflowindex
val moves = Move.combo(counter)
// we have a history to work with... only redo what's necessary
for (i in counter.getLastModifiedIndex() until counter.size()) {
for (i in counter.lastModifiedIndex until counter.size()) {
var start: EdgeModel = if (i == 0)
original
else