Python: various algorithms to solve the eight queens problem


1 The Eight Queens Problem

Question: There is an 8-by-8 chessboard. Now you need to put eight queens on the chessboard. It is satisfied: for each queen, there is no other queen in the row, column, or two diagonals where it is.
Insert picture description here


2 Solution

6 blog posts, a total of 11 solutions:

(1) Brute force cracking ;

(2) Genetic algorithm ;

(3) Hill climbing method / random restart hill climbing method / hill climbing method with side shift allowed ;

(4) Random walk ;

(5)DFS/BFS/UCS

(6) GBF/A* algorithm .


3 performance comparison

The various methods are evaluated as follows:

(1) Brute force cracking found all 92 solutions, and the running time of the main program is not very long, basically within 5s.

(2) I am not satisfied with the genetic algorithm code. The main reason for the poor performance may be that the mutation operation is not selected well, followed by the poor crossover operation.

(3) Hill-climbing method/random restart hill-climbing method/hill climbing method that allows lateral shifting code, the hill-climbing method often falls into the local optimum and cannot find the solution; the random restart hill-climbing method is more rascal, if it doesn’t work, just start again, so most of them can be found Solution; The hill climbing method that allows lateral shifting can greatly increase the probability of success of the hill climbing method to solve the eight queens problem, and the solution sequence can be obtained basically every time it runs.

(4) The random walk code sometimes fails, because the algorithm randomly places the queen at any position in the current column. If there is no queen in the first few columns of the placed queen, then the next column position is selected, so During the process, it may reach the state of [no suitable place to put], and have to declare a failure. You can only re-explore by re-running the program. It may take a few more runs to get the solution.

(5) Among the DFS/BFS/UCS codes, DFS and UCS have higher efficiency and performance in solving the eight queens problem, and BFS is the worst.

(6) In the GBF/A* algorithm code, compared to the GBF algorithm, the A* algorithm code has less running time. After all, the A* algorithm is a global optimization and uses more useful information.


4 summary

Brute force cracking and random walk code are completely self-thinking, and I am proud of it; although I am not satisfied with the performance of the genetic algorithm code, I think a lot in the process of writing the code, and I am very happy to exercise my logical thinking. ; The code of the 6 blog posts is similar, but the details are different. Even in the wording of the comments, I have carefully considered them.

In order to solve the problem of the eight queens, various algorithms were used to carry out "crazy bombing". I have gained a lot from it, so keep on working hard!


END

Guess you like

Origin blog.csdn.net/qq_40061206/article/details/112055813