0

I'm trying to solve Minesweeper with a SAT Solver.

So far i understand the general logic of the game, how the SAT Solver works and how to implement it.

However i can't wrap my head around, how to translate my boolean formula into a CNF in order to solve it with a SAT Solver.

lets say i have the following 5x5 Board:

2 X X X X
X X X X X
X X X X X
X X X X X
X X X X X

In cell 1,1 there is value 2, which means the surrounding neighbor cells have 2 mines. Overall cell 1,1 has 3 neighbors (cell 1,2; cell 2,1; cell 2,2). I will call the neighbor cells: 1,2 and 3.

That means i have the following boolean possibilities:

cell 1 = Bomb
cell 2 = Bomb
cell 3 = No Bomb
or
cell 1 = Bomb 
cell 2 = No Bomb
cell 3 = Bomb 
or
cell 1 = No Bomb
cell 2 = Bomb 
cell 3 = Bomb 

If (a or b) and (c or not d) is a CNF than the syntax for a SAT Solver is: {{1,2},{3,-4}} where the variables are integers and the boolean false values are negative integers and the true values are positive integers.

However, I don't know how to turn my boolean possibilities of the minesweeper field into a CNF.

My guess for cell 1,1 with 3 neighbors and 2 mines:

(Bomb 1,2 or Bomb 2,1 or no Bomb 2,2) and
(Bomb 1,2 or no Bomb 2,1 or Bomb 2,2) and
(no Bomb 1,2 or Bomb 2,1 or Bomb 2,2)

so my SAT Solver formula according to the syntax would be: {{1,2,-3},{1,-2,3},{-1,2,3}}

Can someone please confirm or correct my SAT Solver formula?

Thank you.


Edit:


After some discussion, i came up with the following pseudo code idea/approach:

# pseudo code for cell_x_y_n of corner type (3 neighbors) with n = 2 bombs


for cell_x_y in minesweeper_board:

 if cell_x_y == corner_cell and cell_value_n == 2:
  
   check with SAT Solver if knowledge_base(cell_x_y_2) ∪ bomb_x_y is satisfiable:

      if satisfiable == True:
         mark cell_x_y as bomb and don't visit anymore
         continue

      if satisfiable == False:
         cell_x_y has no bomb and don't visit anymore
         continue
  • Any remarks of any kind are highly appreciated and helpful.

  • Also once i checked and looped through every cell will this approach solve the game?

Thank you.

philuix
  • 109
  • 1
  • 7
  • 1
    An SMT solver would be a better choice for this, since you can directly talk/reason-about numbers. (With a pure SAT solver, you'd have to encode them; much more cumbersome.) – alias Nov 14 '21 at 18:42
  • sorry, i had to edit my question. First, thanks for the answer. I will take this into account when implementing it, but my problem is of another kind. – philuix Nov 14 '21 at 18:49
  • This sort of encoding will be difficult with SAT. Instead use an SMT solver. If there's a bomb, you can represent it with `-1`. If empty, it'll be `0`. Otherwise, that number will be the #of neighbors that have bombs. Then solving is equivalent to checking if a given board is "meaningful," i.e., if all the numbers satisfy the rules of the game. Then, the solver will give you the full-board, starting with partial information you provide. – alias Nov 14 '21 at 18:56
  • No need for a SMT solver. Without being familiar with minesweeper rules (e.g. what can a cell contain: bomb, number 0 - 4?), this doesn't look to hard to formulate in SAT. Some remarks: 1) it's very important to base your formulation on a binary-encoded form -> Each cell is in exactly one of N states and each of those states is encoded by a boolean variable. See any SAT + sudoku tutorial for something similar. 2) Your latest code-block inverted what you actually want: you want to express either variant A or B or C. This means: A disjunction of conjunctions! – sascha Nov 14 '21 at 19:05
  • 3) You are missing the channeling / linking of the state and variants. A or B or C (our disjunction of conjunctions) is great, but those MUST only be enforced if x 1 1 is actually 2 (meaning: some boolean is true). You need an outer implication. Meaning: `x_1_1_2 (there is a 2 in 1, 1) -> Or(And(), And(), And())`. 4) These remarks follow your modelling approach. There are others like the SMT-based where your right-hand side is not a disjunction of conjuctions (possible patterns) but a cardinality-constraints. That's a bit easier with a library like pysat (assuming the cards can be reified). – sascha Nov 14 '21 at 19:06
  • @sascha thanks! But my last code block is a conjunction of disjunctions alias the CNF - isn't it (?). Also that is one of the problems I have. Translating the boolean possibilities into the right form (dnf or cnf). – philuix Nov 14 '21 at 19:14
  • SAT solvers only take cnf (never dnf) and i said that yours look wrong. You want a sound model and not some unsound model which just happens to be cnf. **Additional remark:** do you have a sound idea of what exactly to do with your model? A SAT-solver in it's core form just searches for some solution. That is fine with some incomplete sudoku board and you obtain a full one. But in your case, that game just works differently. It's incremental / online.It seems you want incremental domain-reduction (to select one of the non-proven to be wrong moves).I'm not sure if you are ready on this part yet – sascha Nov 14 '21 at 19:15
  • 1
    There's a worked out example of how to do this with SAT and SMT in Section 3.9 of https://sat-smt.codes/SAT_SMT_by_example.pdf You'll notice that that SMT solution is a lot easier to read/understand/modify/maintain etc. – alias Nov 14 '21 at 19:17
  • @sascha Thanks! So far I only know how to get the information from the cells, create boolean possibilities and a formula. Now, I'm working on figuring out how to go on from there.. Also once I have my model I'm not sure what to do with it. I think I have to create another boolean value which marks a bomb, so I can test the model. So maybe when I have the model with clauses and test it with the "bomb" value I can predict whether there is a bomb in the cell and then move on and mark this cell as bomb cell.. but I'm just getting started on this and I appreciate every tip! – philuix Nov 14 '21 at 19:38
  • Without spending too much thoughts on it, you probably want something called *shaving* or *singleton arc consistency*. As preparation: build a full model once without any observations (don't hard-code your observation of 2 @ 1,1 yet). Then each time one of n decisions is needed, *assume* (that's a incremental SAT-solving notion) this decision (and your *fixed* past ones) and check if it's feasible or falsified. This testing of all potential decisions is the *shaving* part. SAT-solvers are pretty good at this kind of incremental use-cases. – sascha Nov 14 '21 at 19:42
  • @sascha Thank you very much! Helps a lot. In order to get the modeling right, can you please run me through my cell 1,1 step. In my question I wrote a formula with some clauses, and (as expected) you said it is not sound enough. Could you please, explain how I get a sound model from let's say cell 1,1 with 3 neighbours and 2 mines. I want to make sure to get this right. I know how to move on from there.. – philuix Nov 14 '21 at 19:51
  • They gray-formatted block above basically contains all my opinions on what to encode. There is only the cnf conversion missing where you 1) either hard-code it 2) implement your own (restricted) boolean-algebra lib or 3) use sympy's tools to do this for you. I admit, that there is some learning curve. – sascha Nov 14 '21 at 19:55
  • @sascha thanks sascha! The missing link was indeed the implication. That was my missing puzzle. I will continue from there, thanks a lot! – philuix Nov 15 '21 at 08:51
  • @sascha one more question: After i modeled everything and i have a link for every cell state and it's variants, how do i move on from there? I.e. once i have the model of cell 1,1 with 2 mines, how do i chose the next cell? Sorry for the "dumb" questions, but as you can see, I'm getting started on this topic and want to understand it in depth. – philuix Nov 15 '21 at 09:12
  • 1
    I guess you want to model the *full* problem including rules for ALL of your cells. This applies to both modelling variants. (It's important to see, that these discussions here talk about 2 very different modelling aproaches: 1) (your idea and what i tackled) a a-priori valid pattern based approach 2) cardinality-based model ("if 2 here, the amount of true-vars in this set is 2") ). You got one big model with alll cell rules (despite having observed only one "2" yet). Because of this it's imporant to have these implications / reification: only enforce things consisten to current knowledge. – sascha Nov 15 '21 at 09:28
  • @sascha Thank you! After reading a little bit more, I edited my question and came up with the following approach (see question). Any remarks from you in regard to it, would be very helpful. – philuix Nov 15 '21 at 11:00

0 Answers0