3

I am looking at the 8 queens puzzle. I used the below R code, which is directly from the R lpsolve documentation. The parameter num.bin.solution is set equal to 3. In R documentation it says that num.bin.solns stands for a numeric indicator of number of solutions returned. In that case how can I see 3 possible solutions? I used command chessing$solution, but the output is not easy to understand. Also is there a way to return all possible solutions?

chess.obj <- rep (1, 64)
q8 <- make.q8 ()
chess.dir <- rep (c("=", "<"), c(16, 26))#first 16 cosntraints are for row and columns, remaining constraints are for diagonals
chess.rhs <- rep (1, 42)
chessing=lp ('max', chess.obj, , chess.dir, chess.rhs, dense.const = q8,
    all.bin=TRUE, num.bin.solns=3)
chessing$solution

Update: I got my main question answered. But still wondering if there is any efficient way to get all possible solutions.

josliber
  • 43,891
  • 12
  • 98
  • 133
user2543622
  • 5,760
  • 25
  • 91
  • 159

1 Answers1

3

The solutions are encoded in chessing$solution. Each block of 64 integer values is one optimal solution, and the last value (-1) should be ignored. You can extract your solutions with:

res <- split(chessing$solution[1:(3*64)], rep(1:3, each=64))
res
# $`1`
#  [1] 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
# [52] 0 0 0 0 0 0 0 0 0 0 1 0 0
# 
# $`2`
#  [1] 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
# [52] 0 0 0 1 0 0 0 0 0 1 0 0 0
# 
# $`3`
#  [1] 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0
# [52] 0 0 0 0 0 0 0 0 0 1 0 0 0

You can now access the individual solutions with res[[1]], res[[2]], and res[[3]].

josliber
  • 43,891
  • 12
  • 98
  • 133
  • is it possible to get all possible solutions. In case of 64 square block chess board, there are 92 possible solutions. If we dont know how many solutions are availabel, how could we tell lpsolve to return all possible solutions? – user2543622 Apr 10 '14 at 18:27
  • @user2543622 I passed `num.bin.solns=1000` and it returned saying it had found 92 solutions. So it seems like you can do this just by setting `num.bin.solns` to a large value. – josliber Apr 10 '14 at 19:11
  • I ran with num.bin.solns=1000, but didnt get any message :( where did you get the message that said "found 92 solutions"? – user2543622 Apr 10 '14 at 19:30