Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. ny=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. open <- list()
  14. moves <- list()
  15. start = c(car$x, car$y, 0, h(c(car$x, car$y), goal), 5)
  16.  
  17. while((start[1]!=goal[1]) && (start[2]!=goal[2])){
  18. neighborDown = neighborLeft = neighborRight = neighborUp = 0
  19.  
  20. #Down - 2
  21. if(start[2] != 1){
  22. neighborDown = c(start[1], start[2]-1, g(start, 2, roads) + start[3], h(c(start[1], start[2]-1), goal), 2)
  23. open[[length(open)+1]] <- neighborDown
  24. }
  25. #Left - 4
  26. if(start[1] != 1){
  27. neighborLeft = c(start[1]-1, start[2], g(start, 4, roads) + start[3], h(c(start[1]-1, start[2]), goal), 4)
  28. open[[length(open)+1]] <- neighborLeft
  29. }
  30. #Right - 6
  31. if(start[1] != 10){
  32. neighborRight = c(start[1]+1, start[2], g(start, 6, roads) + start[3], h(c(start[1]+1, start[2]),goal), 6)
  33. open[[length(open)+1]] <- neighborRight
  34. }
  35. #Up - 8
  36. if(start[2] != 10){
  37. neighborUp = c(start[1], start[2]+1, g(start, 8, roads) + start[3], h(c(start[1], start[2]+1), goal), 8)
  38. open[[length(open)+1]] <- neighborUp
  39. }
  40.  
  41. lowestCost = 2 ** 20
  42. for(node in open){
  43. nodeCost = node[3] + node[4]
  44. if(lowestCost > nodeCost){
  45. start = node
  46. lowestCost = nodeCost
  47. }
  48. }
  49.  
  50. for(i in 1:length(open)){
  51. if(all(start==open[[i]])){
  52. open[[i]] <- NULL
  53. break
  54. }
  55. }
  56.  
  57. #Add next move to end of list
  58. moves[[length(moves)+1]] <- start[5]
  59. }
  60. if(length(moves)==0){
  61. print(paste("nextMove: 5"))
  62. nextMove = 5
  63. } else{
  64. print(paste("nextMove:",moves[[1]]))
  65. nextMove = moves[[1]]
  66. }
  67. car$nextMove=nextMove
  68. car$mem=list()
  69. return (car)
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement