Heuristic function for applying A* sudoku

只谈情不闲聊 提交于 2019-12-13 20:13:39

问题


I need a good heuristic function for A star for sudoku solving. The sudoku grid is 4X4 and by definition the legal operation from each state is to insert a new number to the next free cell (the order is left to right and up to down).

for example, this is the input grid:

and we should now fill the cell (1,2).

All the nodes are different grids that represents different states. The branching factor is 4, so we have 4 possibilities for the next cell:1, 2, 3 or 4, i.e. 4 children for each node.

How can I define heuristic function on the nodes for applying A* on the grid?

All I can think of is:

if the new number that was inserted to the current state's grid is illigal (= appears more than once in the same row, column or box) so h(n)= infinity.

else, h(n)= [number of empty remain cells].

I think that my solution is not correct because there is no difference in the heuristic value between two nodes in the same level that are legal.


回答1:


One heuristic function would be to pick the cell that has the maximum number of constraints on it.

E.g. in your example, you could pick (2,3) => since it has only one possibility to fit (max # of constraints). After you've made a "bet" (placed a number), you could continue with the same strategy => place S[2,3] = 2 => pick S[2,2] and so on.

By constraints, I mean how many options do you have per cell, given the constraints in Sudoku rules.

h[cell] = 1/(#options per box + #options per row + #options per column)

This is a very simple strategy though (one look-ahead option), but more complex strategies would be to combine this strategy into higher-order logic => basically:

h2[cell] = h[cell-left]+h[cell-right]+...;




回答2:


As noted in one comment, A* is not appropriate for a game like Sudoku, as, considering the Sudoku rules, you have just one possible solution. A* (with open list) allows to find the optimal solution, given an admissible heuristic, which tells the estimated cost to the goal state and is less than or equal to the actual cost. There is no point on having illegal actions. They can be discarded by defining proper actions, given a state.

The only search formulation that could make sense for Sudoku with A* is embedding the cost of finding the solution, and thus the heuristic would represent the estimated cost of the search. In such a case the order in which cells are filled in matter.

Usually, though, some other search techniques are used for Sudoku, such as depth first search. The idea is to increase the efficiency of the search, by properly pruning the search graph and by selecting wisely the next cell to work on. One possible way to select the next cell is given in the other answer.

However, this does not to seem to be the case for your problem with the extra constraint on how to choose the next cell to handle as given by your assignment. If you have to stick to A*, the heuristic you defined is correct, as it is admissible. As you noticed, the heuristic value would be the same for all of the nodes. This would result in basically a breadth first search, and would be very inefficient.

For more information about informed search and heuristics, refer to Chapter 3 of Artificial Intelligence: a modern approach, by Russell and Norvig.



来源:https://stackoverflow.com/questions/36037919/heuristic-function-for-applying-a-sudoku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!