Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nqueens #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added nqueens_matrix_example.ods
Binary file not shown.
7 changes: 7 additions & 0 deletions src/main/scala-2.12/dlx/SparseMatrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ final class SparseMatrix(val matrix: Array[Array[Boolean]]) {
break
}
}

val e = Array.ofDim[Boolean](matrix.length)
for (i <- matrix.indices) {
e(i) = matrix(i)(j)
}

throw new IllegalArgumentException(s"column j=$j is only zeros -> ${e.mkString(", ")}")
}
}

Expand Down
119 changes: 119 additions & 0 deletions src/main/scala-2.12/dlx/transform/NQueensProblem.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package dlx.transform

/**
* N=8 generalizing later
* CheckBoard representation matrix 8*8 of booleans true=queen is present, false otherwise
* SparseMatrix is 64*42
*
* constraints: N Ranks, N Files, 2N-2 Diagonal, 2N-2 RDiagonal
*
* @todo generalize N
*/
object NQueensProblem {
private[this] val N = 8
private[this] val m = N * N
private[this] val n = 6 * (N - 1)

def rankColumnIndexBy(rowIndex: Int): Int = {
require(rowIndex < m && rowIndex >= 0)
rowIndex / N
}

def fileColumnIndexBy(rowIndex: Int): Int = {
require(rowIndex < m && rowIndex >= 0)
N + rowIndex % N
}

def buildDiagABy(array: Array[Array[Boolean]], diagIndex: Int): Unit = {
require(diagIndex >= 0 && diagIndex < 2*N-3)
val offset = N*2

if (diagIndex < N - 1) {
// 0..6 (A1..A7)
for(i <- 0 until (N-diagIndex)) {
array(diagIndex + i*(N+1))(offset + diagIndex) = true
}
} else {
// 7..12 (A9..A14)
val ioffset = diagIndex - N + 1
for(i <- (N-1) until ioffset by -1) {
array(i*(N+1)-ioffset-1)(offset + diagIndex) = true
}
}
}

def buildDiagBBy(array: Array[Array[Boolean]], diagIndex: Int): Unit = {
require(diagIndex >= 0 && diagIndex < 2*N-3)
val offset = (N * 4) - 3

if(diagIndex < N - 1) {
// 0..6 (B1..B7)
for(i <- N until diagIndex by -1) {
array(i*(N-1)+diagIndex)(offset + diagIndex) = true
}
} else {
//7..12 (B9..B14)
val ioffset = diagIndex - N + 1
for (i <- (array.length-N-1) until 0 by -(N-1)) {
array(i - ioffset)(offset + diagIndex) = true
}
}
}

def buildArray(): Array[Array[Boolean]] = {
val array = new Array[Array[Boolean]](m)

for (i <- array.indices) {
val a = Array.fill[Boolean](n)(false)
a(rankColumnIndexBy(i)) = true
a(fileColumnIndexBy(i)) = true
array(i) = a
}

// build Diagonals A, B
for(i <- 0 until 2 * N - 3) {
buildDiagABy(array, i)
buildDiagBBy(array, i)
}

array
}

def convert(checkBoard: Array[Array[Boolean]]): Array[Array[Boolean]] = {
buildArray()
// todo: filling the checkboard

}

def unconvert(sparseMatrix: Array[Array[Boolean]]): Array[Array[Boolean]] = {
val checkBoard = Array.ofDim[Boolean](N, N)

for {
i <- checkBoard.indices
j <- checkBoard(i).indices
} {
val rowValue = i*N + j
val rankValue = i % N
val fileValue = N + j
//val diagAValue = 0
//val diagBValue = 0
if (sparseMatrix(rowValue)(rankValue)
&& sparseMatrix(rowValue)(fileValue)
//&& sparseMatrix(rowValue)(diagAValue)
//&& sparseMatrix(rowValue)(diagBValue)
) {
checkBoard(i)(j) = true
} else {
checkBoard(i)(j) = false
}
}

checkBoard
}

def unconvert(solution: Array[Array[Int]]): Array[Array[Boolean]] = {
val checkBoard = Array.ofDim[Boolean](N, N)
//todo
checkBoard
}
}
10 changes: 5 additions & 5 deletions src/main/scala-2.12/dlx/transform/SudokuProblem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ object SudokuProblem {
j <- grid(i).indices
} { for (value <- 1 to 9) {
val v = value.toByte
val rowValues = row(i, j, v)
if (sparseMatrix(rowValues)(colCel(i, j)) &&
sparseMatrix(rowValues)(colRow(i, v)) &&
sparseMatrix(rowValues)(colCol(j, v)) &&
sparseMatrix(rowValues)(colBox(i, j, v))
val rowValue = row(i, j, v)
if (sparseMatrix(rowValue)(colCel(i, j)) &&
sparseMatrix(rowValue)(colRow(i, v)) &&
sparseMatrix(rowValue)(colCol(j, v)) &&
sparseMatrix(rowValue)(colBox(i, j, v))
) {
grid(i)(j) = (v + grid(i)(j)).toByte
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/scala/NQueensSolverSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import dlx.DLX
import dlx.transform.{NQueensProblem, Solution1}
import org.scalatest.{FlatSpec, Matchers}

class NQueensSolverSpec extends FlatSpec with Matchers {
"Solution 1" should "solved correctly" in {
val dlx = new DLX(NQueensProblem.convert(Solution1.checkBoard))
val sols = dlx.solve()
sols should have length 1
val sol = sols.head
sol should have size 8
for (s <- sol) {
s should have length 4
for (i <- 0 until 4) {
val ss = s.sorted
//ss(i) shuld be ()
}
}

NQueensProblem.unconvert(sol) should be (Solution1.checkboardSol)
}
}
1 change: 1 addition & 0 deletions src/test/scala/SudokuSolverSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class SudokuSolverSpec extends FlatSpec with Matchers {
ss(i) should (be >= 81*i and be < 81*(i+1))
}
}

SudokuProblem.unconvert(sol) should be (exp)
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/dlx/SparseMatrixSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SparseMatrixSpec extends FlatSpec with Checkers {
}
}

"dlx.SparseMatrix" should "throw an error when 1 columns is compose only by zeros" in {
"dlx.SparseMatrix" should "throw an error when 1 column is compose only by zeros" in {
intercept[IllegalArgumentException] {
new SparseMatrix(Array(
Array[Boolean](false, false, false),
Expand Down
80 changes: 80 additions & 0 deletions src/test/scala/dlx/transform/NQueensProblemSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package dlx.transform

import org.scalatest.{FlatSpec, Matchers}

/**
* [[https://en.wikipedia.org/wiki/Eight_queens_puzzle#Solutions]]
*/
object Solution1 {
val checkboardSol = Array(
// A B C D E F G H
Array[Boolean](false, false, false, true, false, false, false, false),
Array[Boolean](false, false, false, false, false, false, true, false),
Array[Boolean](false, false, true, false, false, false, false, false),
Array[Boolean](false, false, false, false, false, false, false, true),
Array[Boolean](false, true, false, false, false, false, false, false),
Array[Boolean](false, false, false, false, true, false, false, false),
Array[Boolean](true, false, false, false, false, false, false, false),
Array[Boolean](false, false, false, false, false, true, false, false)
)

val checkBoard = Array(
// A B C D E F G H
Array[Boolean](false, false, false, true, false, false, false, false),
Array[Boolean](false, false, false, false, false, false, true, false),
Array[Boolean](false, false, true, false, false, false, false, false),
Array[Boolean](false, false, false, false, false, false, false, true),
Array[Boolean](false, true, false, false, false, false, false, false),
Array[Boolean](false, false, false, false, true, false, false, false),
Array[Boolean](true, false, false, false, false, false, false, false),
Array[Boolean](false, false, false, false, false, false, false, false),
)

}

class NQueensProblemSpec extends FlatSpec with Matchers {

sealed trait Empty8QueenProblem {
val checkBoard = new Array[Array[Boolean]](8)
for (i <- checkBoard.indices) {
checkBoard(i) = Array.fill[Boolean](8)(false)
}

val sparseMatrix: Array[Array[Boolean]] = NQueensProblem.convert(checkBoard)
}

"NQueens empty problem" should "be converted properly" in new Empty8QueenProblem {
sparseMatrix should have length 64
for(i <- sparseMatrix.indices) {
sparseMatrix(i) should have length 42
// ranks
for(j <- 0 until 8) {
i/8 === j should be (sparseMatrix(i)(j))
}
// Files
for(j <- 0 until 8) {
sparseMatrix(i)(8 + j) should be (j == i%8)
}
}

// check the ones for each row
for (i <- sparseMatrix.indices) {
val sum = sparseMatrix(i).foldLeft(0)((acc, x) => if(x) acc+1 else acc)
withClue(s"row i=$i sum: ") {
if (i === sparseMatrix.indices.start || i === sparseMatrix.indices.end) {
sum should be(3)
} else {
sum should be(4)
}
}
}

NQueensProblem.unconvert(sparseMatrix) should be (checkBoard)
}

// "NQueens problem 1" should "be converted back properly" in {
// NQueensProblem.unconvert(
// NQueensProblem.convert(Solution1.checkBoard)
// ) should be (Solution1.checkBoard)
// }
}
2 changes: 1 addition & 1 deletion src/test/scala/dlx/transform/SudokuProblemSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ final class SudokuProblemSpec extends FlatSpec with Matchers {
234 -> (8, 1),
242 -> (8, 9)
)
for((e, (j, v)) <- gen) SudokuProblem.colCol(j, v.toByte)
for((e, (j, v)) <- gen) SudokuProblem.colCol(j, v.toByte) should be (e)
}

"colBox helper" should "return valid values" in {
Expand Down