Advertisement
Guest User

Pathfinder

a guest
Oct 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.93 KB | None | 0 0
  1. // pathfinding;
  2.  
  3. public class Cell {
  4.    
  5.     private int value;
  6.     private int row,col;
  7.     public float  globalG=0.0f;
  8.     public Cell() {
  9.         direction=null;
  10.     }
  11.  
  12.     public int getRow() {
  13.         return row;
  14.     }
  15.  
  16.     public void setRow(int row) {
  17.         this.row = row;
  18.     }
  19.  
  20.     public int getCol() {
  21.         return col;
  22.     }
  23.  
  24.     public void setCol(int col) {
  25.         this.col = col;
  26.     }
  27.    
  28.     public void setValue(int value) {
  29.         this.value=value;
  30.     }
  31.    
  32.     public int getValue() {
  33.         return value;
  34.     }
  35.    
  36.     }
  37.    
  38.    ///the Class function
  39.  
  40.  
  41. import java.util.ArrayList;
  42. import java.io.PrintStream;;
  43. public class main {
  44.  
  45.     static PrintStream out=System.out;
  46.  
  47.     static class Node{
  48.        
  49.         // holds the current array position data on the graph
  50.         Cell cell;
  51.         // gets the next adjacent node
  52.         Node next;
  53.         // gets the parent node of the current node
  54.         Node parent;
  55.         // gets all the possible adjacent Nodes from the current node
  56.         ArrayList< Node> Neigbours;
  57.         public Node() {
  58.             next= null;
  59.             cell= null;
  60.             parent=null;
  61.             Neigbours= null;
  62.         }
  63.         // first heuristic test
  64.          float distance(Node a ,Node b) {
  65.            
  66.             return (float)Math.sqrt(((a.cell.getRow()-b.cell.getRow())*
  67.                     (a.cell.getRow()-b.cell.getRow())+
  68.                     (a.cell.getCol()-b.cell.getCol())*
  69.                     (a.cell.getCol()-b.cell.getCol())));
  70.         }
  71.          
  72.          //second heuristics test
  73.         float distance2(Node a,Node b) {   
  74.              
  75.          float x=(a.cell.getRow())-(b.cell.getRow());
  76.          float y=(a.cell.getCol())-(b.cell.getCol());
  77.          float cost=(float)(Math.pow(x, 2)+Math.pow(y, 2));
  78.          
  79.           return cost;
  80.          
  81.          }
  82.        
  83.          // get first prediction
  84.         public float heuristic(Node a,Node b) {
  85.             return distance(a,b);
  86.         }
  87.        
  88.         // get second prediction
  89.         public float heuristic2(Node a,Node b) {
  90.             return distance2(a,b);
  91.         }
  92.     }
  93.    
  94.     static Node map_Ref[][];
  95.     //start point where the robot is located
  96.     static Node start;
  97.     // the end destination where the robot is to arrive
  98.     static Node end;
  99.     // our map where by 0 represents a walkable path
  100.     //    1 represents a wall(non- walkable path)
  101.     // 2 represents the starting location of the robots position
  102.     //4 represents  the destination
  103.     // 5 represents an enemy
  104.     static int obstacles[]= {1,5};
  105.     //map
  106.     static int map[][]= {{2,0,1,1,1,1,1},
  107.                          {1,0,1,0,1,0,1},
  108.                          {1,0,0,0,0,0,1},
  109.                          {1,1,1,1,0,0,1},
  110.                          {1,0,0,0,0,0,1},
  111.                          {1,0,1,1,1,0,1},
  112.                          {1,0,0,4,0,0,1}};
  113.     //add map as a linked list
  114.     private static Node addNode(Node Head,Node ptr) {
  115.         map_Ref= new Node[map.length][map.length];
  116.         for(int i=0;i<map.length;i++) {
  117.             for(int j=0;j<map.length;j++) {
  118.                
  119.         ptr= new Node();
  120.         ptr.cell= new Cell();
  121.         // gets the value from the map array
  122.         ptr.cell.setValue(map[i][j]);
  123.         // gets the column position
  124.         ptr.cell.setCol(j);
  125.         // gets the row position
  126.         ptr.cell.setRow(i);
  127.         // set the next node pointer with the current node
  128.         ptr.next=Head;
  129.         Head=ptr;
  130.         // our reference to the actual map
  131.          map_Ref[i][j]= Head;
  132.          
  133.          // initialize the start and end values from our map
  134.          if(map[i][j]==2) {
  135.              start=ptr;
  136.          }
  137.          if(map[i][j]==4) {
  138.              end=ptr;
  139.          }
  140.            
  141.             }}
  142.      return ptr;   
  143.     }
  144.    
  145.     // function to convert a linked list to an array of Nodes
  146.     private static void getPaths(Node Head){
  147.          //holds the values of the tested nodes in the linked list
  148.          ArrayList <Node> closedList=new ArrayList<Node>();
  149.          // holds the values of the non- tested nodes from the linked list
  150.          ArrayList<Node>  openList= new ArrayList<Node>();
  151.          
  152.          // gets all available neighboring nodes form a linked list
  153.          get_neigbours();
  154.        
  155.          
  156.        
  157.          // our open list must not be empty we must initialize it with
  158.          // the values neighbors of the end node
  159.         openList.add(start);
  160.         while(!openList.isEmpty()) {
  161.             // set the cost value to +infinity
  162.             float cost=Float.MAX_VALUE;
  163.             for(int i=0;i<openList.size();i++) {
  164.                
  165.                 closedList.add(openList.get(i));  
  166.                 for(int j=0;j<openList.get(i).Neigbours.size();j++) {
  167.                     Node nd=openList.get(i).Neigbours.get(j);
  168.                     /*the open list should not contain the node and the closed list should not
  169.                      contain the node
  170.                      if the cell contains the value 1 is a wall
  171.                      only add nodes containing the value 0 since its a walkable path
  172.                      */
  173.                     if(!closedList.contains(nd)&&!openList.contains(nd)&& !isObstacle(nd.cell.getValue())) {
  174.                         if(!openList.contains(nd))
  175.                         {
  176.                        
  177.                         // get the global goal from the start to the end
  178.                         nd.cell.globalG= nd.heuristic2(openList.get(i), nd);
  179.                        
  180.                         if(nd.cell.globalG<=cost) {
  181.                             // add the target neighbor to the OpenList for testing
  182.                         openList.add(nd);  
  183.                         //initialize the parent node
  184.                         nd.parent=openList.get(i);
  185.                         // reset the cost to the current neighbor
  186.                          cost=nd.cell.globalG;
  187.                          
  188.                              }
  189.                        
  190.                         }
  191.                     }
  192.                    
  193.                    
  194.                 }
  195.                
  196.                
  197.                
  198.             }
  199.            
  200.             // since we have tested the nodes in the open list  remove them
  201.             for(int i=0;i<closedList.size();i++) {
  202.                 if(openList.contains(closedList.get(i))){
  203.                     openList.remove(closedList.get(i));
  204.                 }
  205.             }
  206.            
  207.             // reached the target
  208.             if(openList.contains(end)) {   
  209.                 break;
  210.             }
  211.            
  212.         }
  213.        
  214.      
  215.     }
  216.    
  217.    
  218.     // returns the size of a Node list
  219.     private static int ListSize(Node Head) {
  220.         int size=0;
  221.         while(Head!=null) {
  222.             size++;
  223.             Head=Head.next;
  224.         }
  225.        
  226.         return size-1;
  227.     }
  228.    
  229.    
  230.    
  231.     // get the list of neighbors from a particular  node
  232.     private static void get_neigbours() {
  233.        
  234.          for(int i=0;i<map_Ref.length;i++) {
  235.              for(int j=0;j<map_Ref.length;j++) {
  236.                
  237.                  ArrayList<Node> ngbrs= new ArrayList<Node>();
  238.                  Node nd= map_Ref[i][j];
  239.                  // get the neighbors on the left hand side
  240.                  if(i>0) {
  241.                      ngbrs.add(map_Ref[i-1][j]);
  242.                  }
  243.                  // gets the neighboring node on the right hand side
  244.                  if(i!=map_Ref.length-1) {
  245.                      ngbrs.add(map_Ref[i+1][j]);
  246.                  }
  247.                  // get the neighboring node on the bottom part adjacent to the current node
  248.                  if(j>0) {
  249.                      ngbrs.add(map_Ref[i][j-1]);
  250.                  }
  251.                  // get the top neighboring node on the top part of the adjacent node
  252.                  if(j!=map_Ref.length-1) {
  253.                      ngbrs.add(map_Ref[i][j+1]);
  254.                  }
  255.                  // set the neighbors to the current node
  256.                  nd.Neigbours=ngbrs;
  257.              }
  258.          }
  259.          
  260.            
  261.     }
  262.    
  263.     // returns true if there's an obstacles on the graph
  264.     // using the obstacles array
  265.     private static  boolean isObstacle(int in ) {
  266.         for(int i=0;i<obstacles.length;i++) {
  267.             if(obstacles[i]==in) {
  268.                
  269.                 return true;
  270.             }
  271.         }
  272.         return false;
  273.     }
  274.     //prints all nodes in the linked list
  275.     private static void PrintNode(Node Head) {
  276.         while(Head!=null) {
  277.         out.print(Head.cell.getValue());
  278.         Head=Head.next;
  279.         }
  280.        
  281.     }
  282.    
  283.     private static void PrintMatrix() {
  284.         // prints our matrix on the console
  285.           for(int i=0;i<map.length;i++)
  286.           {
  287.               for(int j=0;j<map.length;j++) {
  288.                   out.print(map[i][j]);
  289.                    
  290.               }
  291.               out.print("\n");
  292.             ;
  293.           }
  294.     }
  295.    
  296.     public static void main(String[] args) {
  297.         // TODO Auto-generated method stub
  298.       Node Path= new Node();
  299.       Path.cell=new Cell();
  300.       Path.cell.setValue(1);
  301.       Path= addNode(Path,new Node());
  302.       out.print("The Original Graph Matrix\n");
  303.       PrintMatrix();
  304.       //gets the shortest path
  305.       getPaths(Path);
  306.       //Gets shortest path parent nodes
  307.       Node t=end;
  308.       while(t!=null) {
  309.           map[t.cell.getRow()][t.cell.getCol()]=3;
  310.           t=t.parent;
  311.       }
  312.       //state whether a path was found on the graph
  313.       if(map[start.cell.getRow()][start.cell.getCol()]==3)
  314.       out.print("\nA Path was Found\n");
  315.       else
  316.       out.print("A Path wasn't Found\n");
  317.       out.print("The Shortest Path On the graph\n");
  318.       PrintMatrix();
  319.      
  320.     }
  321.    
  322.  
  323.  
  324.    
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement