Java implementation of a * search algorithm the board Sir Travel Problem BFS

Source: Internet
Author: User
Tags gety

A knight moves on a chessboard both squares up, down, left, or right followed by one square in one of the both directions PE Rpendicular to the first part of the "move" (i.e., the move is l-shaped). Suppose the knight is on a unbounded board at Square (0,0) and we wish to move it to square (x, y) in the smallest number of moves. (for example, the to move from (0,0) to () requires the moves. The knight can move to board locations with negative coordinates.)

1.Explain How to decide whether the required number of moves are even or odd without constructing a solution.
2.Design an admissible heuristic function for estimating the minimum number of moves required; It should is as accurate as you can make it. Prove rigorously that your heuristic is admissible.

3. Implement * and use it to solve the problem using your heuristic. Create Scatter plots showing (a) the number of nodes expanded as a function of solution length, and (b) Computation Ti Me as a function of solution length for a set of randomly generated problem instances.

Thinking Analysis: This is an AI work problem, mainly to investigate the implementation of a * algorithm. The heuristic function used for the * implementation given below is One-third of the Manhattan distance from the generated node to the destination, as the jazz goes in a Japanese style with a maximum jump distance of no more than 3, which is an acceptable heuristic. The main implementation is BFS, which needs to be implemented by queue.

Implement code

Import Java.util.comparator;import java.util.priorityqueue;/** * Find The minimum number of moves of Knight from * (0,0) to (x, y) * @author Liu Yang & Wei Hong * @mail [email protected] & [email protected] */public class Ches Sboardsearchastar {static int[][] actioncosts = {{2, 1}, {2,-1}, {-2, 1}, {-2,-1}, {1, 2}, {1,-2}, {-1, 2}} ;P rivate node startnode;private node Goalnode;int max_x = 1999;int max_y = 1999;int min_x = -2000;int MIN_Y = -2000;int VI Sitedflagoffset = 2000; x + visitedflagoffset = index of x in visitedflagstatic int generatednodecount;static int expandednodecount;private enu M flagtype{init, visited;} private static flagtype[][] Visitedflag;public int getvisitedflagoffset () {return visitedflagoffset;} public void Setvisitedflagoffset (int visitedflagoffset) {this.visitedflagoffset = Visitedflagoffset;} Public Node Getstartnode () {return startnode;} public void Setstartnode (Node startnode) {this.startnode = Startnode;} Public Node GetgoalnodE () {return goalnode;} public void Setgoalnode (Node goalnode) {this.goalnode = Goalnode;} Return-1 when there is no solution or illegal inputpublic int Astarsearch () {if (startnode.getx () = = Goalnode.getx () &amp ;& startnode.gety () = = Goalnode.gety ()) {return 0;} Since the startnode is a fixed, only need to check Goalnodeif (! Goalnode.getx () >= min_x && goalnode.getx () <= max_x) | | ! (Goalnode.gety () >= min_y && goalnode.gety () <= max_y)) {System.err.println ("illegal input!"); return-1;} comparator<node> Comparator = new comparator<node> () {@Overridepublic int Compare (node n1, node N2) {//TODO A Uto-generated Method Stubif (N1.getfvalue (Goalnode) > N2.getfvalue (goalnode)) {return 1;} else if (N1.getfvalue ( Goalnode) = = N2.getfvalue (Goalnode)) {return 0;} return-1;}}; priorityqueue<node> pqueue = new Priorityqueue<node> (2, comparator);//init Visitedflagvisitedflag = new flagtype[max_x-min_x + 1][max_y-min_y + 1];for (int i = 0; I < VISitedflag.length; i++) {for (int j = 0; J < Visitedflag[i].length; J + +) {Visitedflag[i][j] = Flagtype.init;}} Generatednodecount = 1;expandednodecount = 0; Node CurrentNode = new Node (startnode), STARTNODE.SETG (0); Startnode.setstepcount (0); Startnode.setparentnode (null); Pqueue.add (Startnode); Visitedflag[startnode.getx () + visitedflagoffset][startnode.gety () + visitedFlagOffset] = Flagtype.visited;while (pqueue.size ()! = 0) {pqueue.poll (); Expandednodecount++;visitedflag[currentnode.getx () + Visitedflagoffset][currentnode.gety () + Visitedflagoffset] = flagtype.visited;for (int i = 0; i < actioncosts.length; I + +) {node Childnode = new node (currentnode.getx () + actioncosts[i][0], currentnode.gety () + actioncosts[i][1]); Childnode.getx () >= min_x) && (Childnode.getx () <= max_x) && (childnode.gety () >= min_y) &AMP;&A mp (Childnode.gety () <= max_y) &&visitedflag[childnode.getx () + visitedflagoffset][childnode.gety () + Visitedflagoffset] = = flagtype.init) {geNeratednodecount++;childnode.setparentnode (CurrentNode); CHILDNODE.SETG (CURRENTNODE.GETG () + 1); Childnode.setstepcount (Currentnode.getstepcount () + 1);p Queue.add (Childnode); Visitedflag[childnode.getx () + Visitedflagoffset][childnode.gety () + visitedflagoffset] = flagtype.visited;}} CurrentNode = Pqueue.peek (); if (currentnode.getx () = = Goalnode.getx () && currentnode.gety () = = Goalnode.gety ()) {return currentnode.stepcount;}} System.out.println ("There is no solution for the input!"); return-1;}  /** * @param for testing */public static void Main (string[] args) {//TODO auto-generated method Stubchessboardsearchastar Chesssearch = new Chessboardsearchastar (), Chesssearch.setstartnode (new node (0,0)), Chesssearch.setgoalnode (New node ( //tracking the computation timelong startTime = System.currenttimemillis (); System.out.println ("The minimum number of moves of Knight is" + Chesssearch.astarsearch ()); Long endTime = System.currentt Imemillis (); System.out.println ("The Computation Time is" + (Endtime-starttime) + "MS"); System.out.println ("Expandednodecount:" + expandednodecount + "+" Generatednodecount: "+ Generatednodecount);} Static class Node {node parentnode;int x, y;//coordinatesint g;//current costint Stepcount;//count of Steppublic Node (in t i, Int j) {//TODO auto-generated constructor stubthis.x = I;this.y = j;} Public node (node Startnode) {//TODO auto-generated constructor stubthis.x = Startnode.x;this.y = Startnode.y;} Public Node () {//TODO auto-generated constructor stub}public int Getstepcount () {return stepcount;} public void Setstepcount (int stepcount) {this.stepcount = Stepcount;} Public Node Getparentnode () {return parentnode;} public void Setparentnode (Node parentnode) {this.parentnode = parentnode;} public int GetX () {return x;} public void SetX (int x) {this.x = x;} public int GetY () {return y;} public void sety (int y) {this.y = y;} public int Getg () {return g;} public void Setg (int g) {this.g = g;} public int Getfvalue (Node goalnode) {return g + GethvaluE (Goalnode);} public int Gethvalue (Node goalnode) {return (Math.Abs (x-goalnode.x) + Math.Abs (Y-GOALNODE.Y))/3;}}}



Java implementation of a * search algorithm the board Sir Travel Problem BFS

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.