Advertisement
Guest User

Untitled

a guest
Aug 9th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.94 KB | None | 0 0
  1. function [visitedNodes, f, cameFrom] = aStar(workSpace, startIndx, goalIndx, nNodes, h, g)
  2.  
  3. dim = sqrt(nNodes);
  4. node = startIndx;
  5.  
  6. cameFrom(nNodes, 1) = 0;
  7. cameFrom(node) = node;
  8.  
  9. closedSet(nNodes, 1) = false;
  10. openSet(nNodes, 1) = false;
  11.  
  12. costSoFar(nNodes, 1) = 0;
  13.  
  14. f = inf(nNodes, 1);
  15.  
  16. openSet(node) = true;
  17. costSoFar(node) = 0;
  18. f(node) = 0;
  19.  
  20. visitedNodes = 0;
  21.  
  22. %preallocate neighbours -- this only makes sense if we expect to explore
  23. %most of them. If the workspace is not very maze like, dynamic computation
  24. %of the neighbours might be faster
  25. % further this code section is 'optimized' for readability
  26. neighbours = zeros(nNodes,4);
  27. %infinite space movement: up, down, right, left
  28. neighbours(:,1) = (1:nNodes)+dim; % go up
  29. neighbours(:,2) = (1:nNodes)-dim; % go down
  30. neighbours(:,3) = (1:nNodes)+1; % go right
  31. neighbours(:,4) = (1:nNodes)-1; % go left
  32.  
  33. %add a border around the work space
  34. neighbours(1:dim,1) = 1:dim;                            %nodes on the top cant co up
  35. neighbours(nNodes-dim:nNodes,2) = nNodes-dim:nNodes;    %nodes on the bottom can't go down
  36. neighbours(1:dim:nNodes,3) = 1:dim:nNodes;              %nodes on the right side cant go right
  37. neighbours(dim:dim:nNodes,4) = dim:dim:nNodes;          %nodes on the left can't go left
  38.  
  39. %add the obstacles
  40. for idx = 1:numel(workSpace)
  41.     if workSpace(idx)
  42.         edges = neighbours(idx,:); % we can only reach the cell from these 4 elements
  43.         neighbours(edges(1),2) = edges(1); % above node can't go down
  44.         neighbours(edges(2),1) = edges(2); % below node can't go up
  45.         neighbours(edges(3),4) = edges(3); % right node can't go left
  46.         neighbours(edges(4),3) = edges(4); % left node can't go right
  47.     end
  48. end
  49.  
  50. % main A* loop
  51. while any(openSet)
  52.     [~, minFIndx] = min(f);
  53.     f(minFIndx) = inf;
  54.     currentNode = minFIndx;
  55.    
  56.     if currentNode == goalIndx
  57.         disp('goal Found')
  58.         return
  59.     end
  60.    
  61.     openSet(currentNode) = false;
  62.     closedSet(currentNode) = true;
  63.    
  64.     for node = neighbours(currentNode,:)
  65.        
  66.         if closedSet(node)
  67.             % depending on cost of g(x) it might be wise to not continue
  68.             % jumping around in the code is painfully slow, maybe even
  69.             % slower than evaluating g(x).
  70.             % below if will skip this node regardless
  71.            
  72.             % further if we remove this continue, there is no need for the
  73.             % variable closedSet anymore.
  74.             continue
  75.         end
  76.        
  77.         % if node == currentNode, tentativeGScore > costSoFar(node)
  78.         tentativeGScore = costSoFar(currentNode) + g(currentNode);
  79.        
  80.         if ~openSet(node) || tentativeGScore < costSoFar(node)
  81.             cameFrom(node) = currentNode;
  82.             costSoFar(node) = tentativeGScore;
  83.             f(node) = costSoFar(node) + h(node);
  84.             if ~openSet(node)
  85.                 openSet(node) = true;
  86.             end
  87.         end
  88.     end
  89. end
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement