A * algorithm to achieve 8 or 15-number problem (C # Implementation)

Source: Internet
Author: User
Tags addchild exit empty sort
Algorithm | problem-, problem description

8 or 15 number problem is the same problem, it is a randomly arranged 8 or 15 numbers moving in a founder lattice to reach a specified target state. Because there is only one empty lattice, only the pieces near the space can be moved.



Second, the algorithm description



F algorithm Selection



This problem needs to be exhaustive for all possible paths, but as the height of the tree increases, the increase in the number of child nodes is increasing, so it is not realistic to make all the sub nodes exhaustive. In contrast to the current state and target State, an evaluation function is used to evaluate the current state of good or bad. And here we choose a * algorithm to solve, A * algorithm is a best priority algorithm. F ' (n) = G ' (n) + h ' (n), F ' (n) is the valuation function, G ' (n) is the shortest path value from the starting point to the end point, where the height of the tree is represented, and H ' (n) is the value of the most open circuit of N to the target, and the principle is to compare the current generated state with the value of the evaluated function of the previous state Select the smallest evaluation function to proceed to the next step. Here I choose the heuristic value of H ' (n) for the minimum number of steps that each grid takes to reach its target location.



F Algorithm Description



A few points to note:

1. Use Openarr to represent the initial node list (to be extended, this is a dynamic array)

2. Closedarr Save a node that has already been visited

3. The algorithm first needs to give the initial state, because the randomly generated state is not necessarily able to go to the target State, so here to go back from the standard state to create a disturbed random state, so as to ensure that there is a solution.



Algorithm implementation:



1. Openarr Place the initial node

2. If Openarr is the empty set, exit and give a signal of failure

3. N takes the first node of the Openarr and deletes the node n from the Openarr

4. If n is the target node, exit and give a success signal

5. Otherwise, will produce n child nodes, and N of each child node n ' Do the following operation:

1 if n ' is not in Openarr and Closedarr table, put n ' into Openarr table

2 Otherwise, if the evaluation value is computed in the Openarr table, and if the value of the evaluation function in the table is smaller than the values in the tables, the evaluation value in the Update table is the current value.

3 Otherwise, if the evaluation value is computed in the Closedarr table, and if the value of the evaluation function in the table is smaller than the estimate functions in the tables, the evaluation value in the table is updated to the current value, and the node is deleted from the table, and the node is added to the Openarr table.

6. Add N nodes to Closedarr

7. Sort the Openarr (from small to large according to the evaluation function) and go to 2.



Third, program design



The algorithm is implemented using the C # language. The program is mainly based on the breadth-first algorithm described above to implement the algorithm. There are four classes in the program:

Stepnode class, used to describe the properties and methods of each node generated

Heuristic_15num_search class, algorithm implementation class

Form1 class, is the interface design of the class.

This provides a solution for 8-and 15-number problems respectively. and shows the various state transitions that have been experienced.

The following is a brief introduction to the program implementation of several core algorithms.

The following methods of Stepnode are mainly to control the movement of the left and right of the lattice

0 Digital up-shift

private void MoveUp (Position p)

{

if (p.x>=1)

{

Stepnode node = new Stepnode ();

to (This,node);



Position P1 = new Position (P.X-1,P.Y);

AddChild (NODE,P,P1);

}

}



0 Digit Move Down

private void MoveDown (Position p)

{

if (p.x<=text_v.length-2)

{

Stepnode node = new Stepnode ();

to (This,node);



Position P1 = new Position (P.X+1,P.Y);

AddChild (NODE,P,P1);

}

}



0 Number left Shift

private void MoveLeft (Position p)

{

if (p.y>=1)

{

Stepnode node = new Stepnode ();

to (This,node);



Position P1 = new Position (p.x,p.y-1);

AddChild (NODE,P,P1);

}

}



0 digit Right Shift

private void MoveRight (Position p)

{

if (p.y<=text_v.length-2)

{

Stepnode node = new Stepnode ();

to (This,node);



Position P1 = new Position (p.x,p.y+1);

AddChild (NODE,P,P1);

}

}



Compute the value of a node's heuristic function

private void Computegeuristicgeneval ()

{

int geneval = this. Height;



int g = 0; Heuristic factor the minimum number of steps required to reach a standard position for each data



for (int i=0;i<text_v.length;i++)

{

for (int j=0;j<text_v[0]. length;j++)

{

Position P1 = GetPosition (Text_v[i][j]);

Position P2 = GetPosition (Aim[i,j]);

int xd = p1.x > p2.x? p1.x-p2.x:p2.x-p1.x;

int yd = P1.Y > P2.Y? P1.Y-P2.Y:P2.Y-P1.Y;

G + + XD + yd;

}

}

Geneval = g;



This. Heuristic_gene = Geneval;

}



Heuristic_15num_search class

Core algorithm Implementation part

Loop Search Match

private void Loopsearch (Stepnode node)

{

while (openarr.count>0)

{

node = (stepnode) openarr[0];



Openarr.remove (node);



Stop the search if it is the target node

if (node. Isaim ())

{

SetPath (node);

Return

}

Else

{

produces child nodes.

Node. Createchildren ();



Check for each child node

foreach (Stepnode I in node. Children)

{

If you are not in the open and close table.

if (contain (closedarr,i) ==-1 && contain (openarr,i) ==-1)

{

Openarr.add (i);

}



If you are in the Open table

else if (contain (openarr,i)!=-1)

{

Stepnode n = (stepnode) openarr[contain (openarr,i)];



Replace the estimate in the table if I's estimate is less than the estimate in the Open table

if (I.heuristic_gene<n.heuristic_gene)

{

N.heuristic_gene = I.heuristic_gene;

}

}



If you are in close.

Else

{

Stepnode n = (stepnode) closedarr[contain (closedarr,i)];



Replace the estimate in the table if I's estimate is less than the estimate in the closed table

if (I.heuristic_gene<n.heuristic_gene)

{

N.heuristic_gene = I.heuristic_gene;

Closedarr.removeat (contain (openarr,i));

Openarr.add (n);

}

}

}

The node joins the closed table to indicate that it has been accessed.

Closedarr.add (node);



To sort a node

Openarr.sort (New Mycomparer ());

}

}

Theoretically, that's not going to happen.

Path = "Not Found @!";

Return

}



Iv. results of the test



1) 8 Number problem search path is shown below



Generate is used to randomly generate an initial state (46) that represents a random initial state that has been generated from a random 46-step view from a standard state. Here 46 is a random number, because the larger the number of the farther away from the target, the more complex the search so that the random number is controlled between 0-100. 3 for 3 of the founder that is 8 number of questions, 4 means 4 of the founder that represents 15 number problem.







2) 15 number problem search path is shown below






From the above results, as a result of the use of heuristic search process, so greatly accelerated the search speed and the shortest path possible to reach the target State, as a result of the 8-number problem is relatively simple so faster search speed, and 15 number of problems increased complexity, When the number of randomly generated close to 100 when the search time slows down rapidly, so the algorithm is still to be improved, mainly because with the depth of the Openarr and Closearr table data rapidly expanding, so you can consider the Openarr table of the number of States to choose to exclude some, For example, each time the data in the table in the Openarr can be sorted to remove the last few of the worst states, so as to increase the speed to some extent but also reduce the probability of finding the optimal solution but this reduction is very small, because the exclusion is already the worst case.












Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.