Digital problem solving, respectively using BFS and heuristic search implementation.
BFS: solves the optimal solution for the specified 3*3 puzzle (8-digital problem).
1,iscompleted records the completion status of the solution;
2,closelist record all the visited nodes;
3,searchednodesnum records the number of nodes accessed;
4,solutionpath records the solution path.
Public boolean bfsearch () throws IOException {// write the search process d:// bfsearchdialog.txtstring filepath = "BFSearchDialog.txt"; Printwriter pw = new printwriter (New filewriter (filePath)); // ****************************** Jigsawnode beginnode = getbeginjnode (); openlist.add (Beginnode); isCompleted = false; // Judging the end conditions of the wide search while (!openlist.isempty ()) { // determine if the target status is reached jigsawnode root = openlist.firstelement (); currentjnode = root; if (Getcurrentjnode (). Equals (Getendjnode ())) { iscompleted = true; break; } // accesses the first node of the Openlist and joins adjacent inaccessible nodes (not found in closelist) openlist Vector<JigsawNode> neighbornodes = new Vector<JigsawNode> (); Neighbornodes = findfollowjnodes (Root); openlist.addall (neighbornodes); openlist.removeelementat (0 ); Closelist.add (Root); searchednodesnum++;} if ( iscompleted) { calsolutionpath (); } // *********************** This.printresult (PW);p w.close (); System.out.println ("record into " + filepath); return iscompleted;}
Heuristic Search:
A search failed because the number of access nodes is greater than 30,000.
After the function is finished: IsCompleted records the completion status of the solution;
Closelist records all the nodes that have been visited;
Searchednodesnum records the number of nodes visited;
Solutionpath Records understand the path.
Public boolean asearch () throws IOException{// write the search process asearchdialog.txtstring filepath = "ASearchDialog.txt"; Printwriter pw = new printwriter (New filewriter (filePath));// The number of access nodes greater than 25,000 is considered a search failure int maxnodesnum = 25000; // the adjacency node to hold a node vector< Jigsawnode> followjnodes = new vector<jigsawnode> (); // Reset solve complete marked as falseiscompleted = false; (1) Put the starting node in Openlist this.sortedinsertopenlist (This.beginjnode);// (2) If Openlist is empty, or the number of access nodes is greater than maxnodesnum, the search fails, the problem is not solved, otherwise the loop until the solution succeeds while (This.openList.isEmpty () != true & & searchednodesnum <= maxnodesnum) {// (2-1) access the first node of Openlist N, Set to the current node currentjnode// if Currentjnode is the target node, the search succeeds, the completion token iscompleted to True, the solution path is computed, Exit. This.currentjnode = this.opeNlist.elementat (0);if (this.currentJNode.equals (this.endjnode)) {iscompleted = true; This.calsolutionpath (); break;} (2-2) Remove node n from Openlist and put it in closelist to access the node this.openList.removeElementAt (0); this.closeList.addElement (This.currentjnode);searchednodesnum++;// records and displays the search process pw.println ("Searching ... Number of searched nodes: " + this.closelist.size () + " Current state: " + this.currentjnode.tostring ()); SYSTEM.OUT.PRINTLN ("Searching ... Number of searched nodes: " + this.closelist.size () + " Current state: " + this.currentjnode.tostring ());// (2-3) Look for all nodes that are adjacent to Currentjnode and have not been accessed, They are valued at a price from small to large sort insert openlist in Followjnodes = this.findfollowjnodes (this.currentjnode);while (! Followjnodes.isempty ()) {this.sortedinsertopenlist (Followjnodes.elementat (0)); Followjnodes.removeelementat (0);}} This.printresult (PW);// record search results Pw.close(); // Close output file System.out.println ("record into " + filepath);return iscompleted;} /* calculates and modifies the cost estimate for the state node JNode: F (n) =s (n). * s (n) represents incorrect number of subsequent nodes * @param jNode - nodes to calculate cost estimates This function changes the Estimatedvalue property value of the node. */ private void estimatevalue (Jigsawnode jnode) { int fn = 0;// incorrect number of subsequent nodes int s = 0; // All misplaced digital distance from the correct location and int errordis = 0; // all Misplaced digital number int errornum = 0; int index; Index subscript for // spaces int spaceindex = 0; // array of this node int[] nodestate = jnode.getnodesstate (); int dimension = jigsawnode.getdimension (); for (index =1 ; index<dimension* dimension; index++) {if (nodestate[index]+1!=noDESTATE[INDEX+1]) {s++; }} for (index =1 ; index<dimension* dimension; index++) { if (index != nodestate[spaceindex] && nodestate[index] != index) { errornum++; // solving Manhattan Distance // Current coordinates int cx, cy, dx,dy; cx = (index - 1) / dimension; cy = (index -1 ) % dimension; dx = (nodestate[index] - 1) / dimension; dy = (nodestate[index] -1) % dimension; errordis = errordis+math.abs (DX-CX) + math.abs (dy-cy); } } fn = 2*s + errordis + errornum;jnode.setestimatedvalue (FN);}
BFS and A * algorithm solve the N-Digital problem separately