-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastar.R
More file actions
130 lines (119 loc) · 2.47 KB
/
astar.R
File metadata and controls
130 lines (119 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#heuristic = function(src,dest,points) {
# srcx=points[[src,1]]
# srcy=points[[src,2]]
# destx=points[[dest,1]]
# desty=points[[dest,2]]
# x=sqrt(abs(srcx-destx))
# y=sqrt(abs(srcy-desty))
# return(ceiling(x+y))
#}
heuristic = function(curr, dest) {
diff = abs(curr-dest)
if(diff <= 8) {
return (1)
}
else if (diff <= 14) {
return (2)
}
else if (diff <= 19) {
return (3)
}
else if (diff <= 24) {
return (4)
}
else if (diff <= 29) {
return (5)
}
else if (diff <= 34) {
return (6)
}
else {
return (7)
}
}
# Calculate the f(n) value for A* of a node.
nodeVal = function(src,curr,dest,dist) {
cost=dist
#heuristic=heuristic(src,dest,points)
heuristic=heuristic(curr,dest)
return(list(f=cost+heuristic,g=cost,h=heuristic))
}
createNode = function(src,curr,f) {
return(list(f,curr,src))
}
# Insert a f(n) value of a node into the sorted frontier list.
insertFrontier = function(frontier,node) {
insert=F
len=length(frontier)
for(i in 1:len) {
if(len==0) { break }
e=frontier[[i]]
if (node[[1]]<e[[1]]) {
frontier=append(frontier,list(node),after=(i-1))
insert=T
break
}
}
if (!insert) {
frontier=append(frontier,list(node))
}
return(frontier)
}
# Traverse the graph from the destination to the start to determine the path the A* algorithm found.
traverse = function(graph,dest) {
curr=graph[[dest]]
prev=curr[[3]]
if(prev==0) {
return(list(dest))
#return(dest)
}
i = 1
path=list()
while(T) {
list[[ curr[[2]] ]]
temp=prev
curr=graph[[prev]]
prev=curr[[3]]
if(prev==0) {
#return(temp)
return(path)
}
}
#return(0)
return(list())
}
astar = function(tree,edges,src,dest) {
numNodes=length(tree)
graph=list()
frontier=list()
root=createNode(0,src,0)
graph[[src]]=root
curr=src
dist=1
while(curr!=dest) {
edgeNodes=getOptions(curr,edges)
numEdges=length(edgeNodes)
for(i in 1:numEdges) {
edge=edgeNodes[[i]]
#val=nodeVal(src,edge,dest,dist,points)
val=nodeVal(src,edge,dest,dist)
node=createNode(src,edge,val$f)
frontier=insertFrontier(frontier,node)
old=graph[[i]]
if(!is.null(old)) {
if(old[[1]]>node[[1]]) {
graph[[i]]=node
}
}
else {
graph[[i]]=node
}
}
curr=(frontier[[1]])[[2]]
frontier=frontier[-1]
dist=dist+1
}
move=traverse(graph,dest)
#return(c(move,0))
return(move)
}