Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. aStarDM=function(roads,car,packages){
  2. nextMove=0
  3. toGo=0
  4. offset=0
  5. if (car$load==0) {
  6. toGo=which(packages[,5]==0)[1]
  7. } else {
  8. toGo=car$load
  9. offset=2
  10. }
  11. goal = c(packages[toGo,1+offset], packages[toGo,2+offset])
  12.  
  13. #Pseudocode for the code under
  14. #open <- []
  15. #start <- start
  16.  
  17. #while start is not goal {
  18. # add successors of start to open
  19. # start <- select from open (choose the node with lowest cost plus heuristic value)
  20. # remove start from open
  21. #}
  22.  
  23. #return start
  24.  
  25. #List for the frontier
  26. open <- list()
  27. #List to keep track of moves
  28. moves <- list()
  29. #Start is the current position of the car and only has a heuristic value
  30. start = c(car$x, car$y, 0, h(c(car$x, car$y), goal), 5)
  31.  
  32. #Loop runs until start == goal, we always expand the neighbor with lowest cost
  33. while((start[1]!=goal[1]) || (start[2]!=goal[2])){
  34. neighborDown = neighborLeft = neighborRight = neighborUp = 0
  35.  
  36. #The if statements below finds the neighbors start and adds them to the frontier
  37. #Down - 2
  38. if(start[2] != 1){
  39. neighborDown = c(start[1], start[2]-1, g(start, 2, roads) + start[3], h(c(start[1], start[2]-1), goal), 2)
  40. open[[length(open)+1]] <- neighborDown
  41. }
  42. #Left - 4
  43. if(start[1] != 1){
  44. neighborLeft = c(start[1]-1, start[2], g(start, 4, roads) + start[3], h(c(start[1]-1, start[2]), goal), 4)
  45. open[[length(open)+1]] <- neighborLeft
  46. }
  47. #Right - 6
  48. if(start[1] != 10){
  49. neighborRight = c(start[1]+1, start[2], g(start, 6, roads) + start[3], h(c(start[1]+1, start[2]),goal), 6)
  50. open[[length(open)+1]] <- neighborRight
  51. }
  52. #Up - 8
  53. if(start[2] != 10){
  54. neighborUp = c(start[1], start[2]+1, g(start, 8, roads) + start[3], h(c(start[1], start[2]+1), goal), 8)
  55. open[[length(open)+1]] <- neighborUp
  56. }
  57.  
  58. #Find node with lowest f = cost(g) + heursitic(h)
  59. lowestF = 2 ** 20
  60. for(node in open){
  61. f = node[3] + node[4]
  62. if(lowestCost >= f){
  63. start = node
  64. lowestF = f
  65. }
  66. }
  67.  
  68. #Remove start node from the frontier
  69. for(i in 1:length(open)){
  70. if(all(start==open[[i]])){
  71. open[[i]] <- NULL
  72. break
  73. }
  74. }
  75.  
  76. #Add the move to end of moves list
  77. moves[[length(moves)+1]] <- start[5]
  78. }
  79.  
  80. #the if statements here are for error handling, but should always set nextMove to moves[[1]]
  81. if(length(moves)==0){
  82. print(paste("nextMove: 5"))
  83. nextMove = 5
  84. } else{
  85. print(paste("nextMove:",moves[[1]]))
  86. nextMove = moves[[1]]
  87. }
  88.  
  89. car$nextMove=nextMove
  90. car$mem=list()
  91. return (car)
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement