edgemodel now uses intarray, it's faster!

This commit is contained in:
Joachim 2018-08-28 19:24:47 +02:00
parent 1351935159
commit e679acbcec
5 changed files with 73 additions and 50 deletions

View File

@ -30,7 +30,8 @@ open abstract class CrossSolver {
/**
* Let's look for symmetries.
*
* base times: 72s, 29s, 61s
* - no changes: base times: 72s, 29s, 61s
* - changed list<int> in edgemodel to intarray: 51s, 21s, 35s
*/
fun main(args: Array<String>) {
@ -78,7 +79,7 @@ fun main(args: Array<String>) {
println("Scramble: ${moves.map { decodeMove(it) }}")
val usedModel = EdgeModel(moves)
val usedModel = EdgeModel.withMoves(moves)
println(usedModel)
// val baseSolve = CrossSolverBase().solveCrossesTimed(usedModel)

View File

@ -45,7 +45,7 @@ class CrossSolverIterator : CrossSolver() {
}
fun main(args: Array<String>) {
val start = EdgeModel().doMoves(randomMoves(20))
val start = EdgeModel.solved().doMoves(randomMoves(20))
val solver = CrossSolverIterator()
val solveCrossesTimed = solver.solveCrossesTimed(start)
solveCrossesTimed.forEach { t, u ->

View File

@ -26,50 +26,15 @@ package be.nielandt
class EdgeModel {
val model: Array<Int>
lateinit var model: IntArray
private val F_indices = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2)
private val B_indices = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5)
private val L_indices = intArrayOf(3, 23, 9, 19, 12, 13, 14, 15)
private val U_indices = intArrayOf(16, 17, 18, 19, 0, 12, 8, 4)
private val D_indices = intArrayOf(2, 6, 10, 14, 20, 21, 22, 23)
private val R_indices = intArrayOf(4, 5, 6, 7, 1, 17, 11, 21)
/**
* Create an edgemodel through the companion object.
*/
private constructor()
constructor() {
// do a sanity check
val entries = mutableListOf(F_indices, B_indices, L_indices, U_indices, D_indices, R_indices).flatMap { it.asList() }.groupBy { it }.entries
// println("entries = ${entries}")
if (entries.any {
it.value.size != 2
}) {
throw RuntimeException("each index should occur exactly twice in the arrays")
}
model = arrayOf(
GREEN, GREEN, GREEN, GREEN,
RED, RED, RED, RED,
BLUE, BLUE, BLUE, BLUE,
ORANGE, ORANGE, ORANGE, ORANGE,
WHITE, WHITE, WHITE, WHITE,
YELLOW, YELLOW, YELLOW, YELLOW
)
}
constructor(randomMoves: Int) {
val edgeModel = EdgeModel()
val r: IntArray = randomMoves(randomMoves)
val doMoves = edgeModel.doMoves(r.toList())
this.model = doMoves.model
}
constructor(model: Array<Int>) {
this.model = model
}
constructor(moves: List<Int>) {
val edgeModel = EdgeModel()
val newModel = edgeModel.doMoves(moves)
this.model = newModel.model
fun copyOf(model: EdgeModel): EdgeModel {
return EdgeModel.withModel(model.model)
}
/**
@ -146,7 +111,7 @@ class EdgeModel {
D_ -> prime(D_indices)
D2 -> double(D_indices)
}
return EdgeModel(copyOf)
return EdgeModel.withModel(copyOf)
}
/**
@ -221,5 +186,62 @@ class EdgeModel {
}
}
}
companion object {
private val F_indices = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2)
private val B_indices = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5)
private val L_indices = intArrayOf(3, 23, 9, 19, 12, 13, 14, 15)
private val U_indices = intArrayOf(16, 17, 18, 19, 0, 12, 8, 4)
private val D_indices = intArrayOf(2, 6, 10, 14, 20, 21, 22, 23)
private val R_indices = intArrayOf(4, 5, 6, 7, 1, 17, 11, 21)
fun solved(): EdgeModel {
val edgeModel = EdgeModel()
// do a sanity check
val entries = mutableListOf(F_indices, B_indices, L_indices, U_indices, D_indices, R_indices).flatMap { it.asList() }.groupBy { it }.entries
// println("entries = ${entries}")
if (entries.any {
it.value.size != 2
}) {
throw RuntimeException("each index should occur exactly twice in the arrays")
}
val model = intArrayOf(
GREEN, GREEN, GREEN, GREEN,
RED, RED, RED, RED,
BLUE, BLUE, BLUE, BLUE,
ORANGE, ORANGE, ORANGE, ORANGE,
WHITE, WHITE, WHITE, WHITE,
YELLOW, YELLOW, YELLOW, YELLOW
)
edgeModel.model = model
return edgeModel
}
fun withRandomMoves(randomMoves: Int): EdgeModel {
val edgeModel = EdgeModel()
val r: IntArray = randomMoves(randomMoves)
val doMoves = edgeModel.doMoves(r.toList())
edgeModel.model = doMoves.model
return edgeModel
}
fun withModel(model: IntArray): EdgeModel {
val edgeModel = EdgeModel()
edgeModel.model = model
return edgeModel
}
/**
* Pass a list of moves and do them on a solved edgemodel.
*/
fun withMoves(moves: IntArray): EdgeModel {
val edgeModel = EdgeModel.solved()
val newModel = edgeModel.doMoves(moves)
edgeModel.model = newModel.model
return edgeModel
}
}
}

View File

@ -58,8 +58,8 @@ fun classOf(move: Int): Int {
return (move%6) / 2
}
fun parseMoves(s: String): List<Int> {
return s.split(" ", ",", ";").filter { it?.length > 0 }.map { parseMove(it) }.toList()
fun parseMoves(s: String): IntArray {
return s.split(" ", ",", ";").filter { it?.length > 0 }.map { parseMove(it) }.toIntArray()
}
fun parseMove(s: String): Int {

View File

@ -7,7 +7,7 @@ class EdgeModelTest {
@Test
fun testSingleMove() {
val edgeModel = EdgeModel()
val edgeModel = EdgeModel.solved()
(0..5).forEach { color ->
assertTrue(edgeModel.crossSolved(color))
}
@ -15,7 +15,7 @@ class EdgeModelTest {
@Test
fun testSingleMoves() {
val edgeModel = EdgeModel()
val edgeModel = EdgeModel.solved()
println(edgeModel)
val final = edgeModel.doMove(F)
println(final)