Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ny=function(roads,car,packages){
- nextMove=0
- toGo=0
- offset=0
- if (car$load==0) {
- toGo=which(packages[,5]==0)[1]
- } else {
- toGo=car$load
- offset=2
- }
- goal = c(packages[toGo,1+offset], packages[toGo,2+offset])
- open <- list()
- moves <- list()
- start = c(car$x, car$y, 0, h(c(car$x, car$y), goal), 5)
- while((start[1]!=goal[1]) && (start[2]!=goal[2])){
- neighborDown = neighborLeft = neighborRight = neighborUp = 0
- #Down - 2
- if(start[2] != 1){
- neighborDown = c(start[1], start[2]-1, g(start, 2, roads) + start[3], h(c(start[1], start[2]-1), goal), 2)
- open[[length(open)+1]] <- neighborDown
- }
- #Left - 4
- if(start[1] != 1){
- neighborLeft = c(start[1]-1, start[2], g(start, 4, roads) + start[3], h(c(start[1]-1, start[2]), goal), 4)
- open[[length(open)+1]] <- neighborLeft
- }
- #Right - 6
- if(start[1] != 10){
- neighborRight = c(start[1]+1, start[2], g(start, 6, roads) + start[3], h(c(start[1]+1, start[2]),goal), 6)
- open[[length(open)+1]] <- neighborRight
- }
- #Up - 8
- if(start[2] != 10){
- neighborUp = c(start[1], start[2]+1, g(start, 8, roads) + start[3], h(c(start[1], start[2]+1), goal), 8)
- open[[length(open)+1]] <- neighborUp
- }
- lowestCost = 2 ** 20
- for(node in open){
- nodeCost = node[3] + node[4]
- if(lowestCost > nodeCost){
- start = node
- lowestCost = nodeCost
- }
- }
- for(i in 1:length(open)){
- if(all(start==open[[i]])){
- open[[i]] <- NULL
- break
- }
- }
- #Add next move to end of list
- moves[[length(moves)+1]] <- start[5]
- }
- if(length(moves)==0){
- print(paste("nextMove: 5"))
- nextMove = 5
- } else{
- print(paste("nextMove:",moves[[1]]))
- nextMove = moves[[1]]
- }
- car$nextMove=nextMove
- car$mem=list()
- return (car)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement