-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.m
More file actions
executable file
·286 lines (255 loc) · 8.67 KB
/
debug.m
File metadata and controls
executable file
·286 lines (255 loc) · 8.67 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
start =[2,2];
finish=[14,2];
% get the map
map =map_convert("map_1.txt");
% ------------------------------------------------------HARDCODED ON TOP
%save a copy of the unchanged map
original_map= map;
%get the size of the map
s= size(map);
rows=s(1);
cols=s(2);
%iterate through map and find the nodes on the path
%each node has an ID start at 5 ( value which is not 0 (path) or 1(wall)
offset =1;
ID=offset+1;
%create a set of nodes to be populated with an ID
nodes=[];
locations=[];
for i=1:1:rows,
for j=1:1:cols,
%if location is a node
if(map(i,j)==0)
%printing all the locations on the path
%{
fprintf("(");
fprintf('%d',i);
fprintf(",");
fprintf('%d',j);
fprintf(")"+'\n');
%}
%put the ID
map(i,j)=ID;
% add the node to a Set
nodes=[nodes;ID];
%save location of the set (row number corresponds to node)
locations=[locations; i j];
%increment ID
ID=ID+1;
end;
end;
end;
%find node id of start point and end point
startNode=map(start(2),start(1));
endNode=map(finish(2),finish(1));
%create an array of unvisited nodes
unVisited=nodes;
%the distance of all nodes is initialised as Infinity
distN= Inf(size(nodes));
%the parent node is null for all nodes at beginning
prevN=zeros(size(nodes));
% matrix for the edges or relations between nodes
relations=[];
%init start node as 0 distance
distN(startNode-1)=0; %%%HARDCODED
%Find all relations
relations=find_relations(nodes,original_map,rows,cols,locations);
%Djakstras(distN,prevN,nodes,unVisited,relations);
while(~(isempty(unVisited)))
%the node with the minimum distance
u=find_minDist_unvisited(unVisited,distN,nodes);
%remove node from unVisited nodes
unVisited=setdiff(unVisited,u);
r=size(relations);
%iterate through all the relations
for relation=1:1:r(1),
%for all edges u,v
if ( relations(relation,1)==u ),
%calculate the new distN(v) and compare to old
new = distN(u-1)+1;
% if (distN(relations(2,relation)-1)==Inf),
% distN(relations(2,relation)-1)=new;
% prevN(relations(2,relation)-1)=u;
% end;
v=relations(relation,2);
%relations(2)=v but have to subtract offset 1
if( new < distN(v-1) ),
distN(v-1)=new;
prevN(v-1)=u;
end;
end;
end;
end
disp("djakstras done");
path=[];
currentNode=endNode;
%perform relaxation
while(~ismember(startNode,path))
%add the node to the path
path=[path;currentNode];
%find the previous node
currentNode=prevN(currentNode-1);
end
%flip the path array so that is from start to finish
retpath=flip(path);
psize =size(retpath);
pathsize=psize(1);
%store x y coords of the nodes in the path
retstep=zeros(pathsize,2);
for i=1:pathsize,
%find the path by nodeID
for j=1:rows,
for k=1:cols,
if ( map(j,k)==retpath(i) ),
%format of retstep is row,col
%matrix of 2 cols for co-ords -rows correspond to each node
retstep(i,1)=k;
retstep(i,2)=j;
end;
end;
end;
end;
disp("done");
function index = findNode_index(Node,nodes)
index=NaN;
for i=1:1:size(nodes),
if (nodes(i)==Node),
index=i;
return
end;
end;
return
end
%find an node and show its location on the map
function [x,y] = findNode_xy(Node,nodes,locations)
%Node=10;
for i=1:1:size(nodes),
if (nodes(i)==Node),
disp(i);
%index value i corresponds to row of locations matrix
x=locations(i,1);
y=locations(i,2);
end;
end;
end
function relations=find_relations(nodes,original_map,rows,cols,locations)
%for all the nodes:
%1.Check for all the squares around
rel=0;
relations=[];
a=size(nodes);
%iterate through all nodes
%{
for i=1:1:a(1),
x=locations(i,1);
y=locations(i,2);
%check all surrounding locations (filter out if it is out of
%bounds)
for j=-1:2:1,
for k=-1:2:1,
%this is same location so skip
if ( j==k ),
%disp("detected");
continue;
%skip out of bounds
else if ( ((x+j)<1) || ((x+k)<1) || ((x+j)>cols) ||((x+k)>rows) ),
disp("out of bounds");
continue;
else
node_index=Node(x+j,y+k,locations)
if (node_index ~= 0),
relations=[relations;[nodes(i),node_index]];
%disp(" add relation working");
end;
end;
end;
end
end;
end;
%}
%iterate through the map and check to see if there is a node next to
%the location.
%if there is then create a new relation
%there are 4 possible surrounding points in the map
for i= 1:1:rows,
for j=1:1:cols,
if original_map(i,j) == 0
currentloc(1) = i;
currentloc(2) = j;
if currentloc(1) - 1 ~= 0 && original_map(currentloc(1)-1,currentloc(2)) == 0
relations = [relations;[(Node(currentloc(1),currentloc(2),locations)),Node(currentloc(1)-1,currentloc(2),locations)]];
end
if currentloc(2) + 1 ~= 20 && original_map(currentloc(1),currentloc(2)+1) == 0
relations = [relations;[(Node(currentloc(1),currentloc(2),locations)),Node(currentloc(1),currentloc(2)+1,locations)]];
end
if currentloc(1) + 1 ~= 16 && original_map(currentloc(1)+1,currentloc(2)) == 0
relations = [relations;[(Node(currentloc(1),currentloc(2),locations)),Node(currentloc(1)+1,currentloc(2),locations)]];
end
if currentloc(2) - 1 ~= 0 && original_map(currentloc(1),currentloc(2)-1) == 0
relations = [relations;[(Node(currentloc(1),currentloc(2),locations)),Node(currentloc(1),currentloc(2)-1,locations)]];
end
end;
end;
end;
end
%uses locations index to find node ID +1 as offset by 1
function node = Node(x,y,locations)
node = 0;
s =size(locations);
s=s(1);
%loop through all locations and find if this point is in the path
for i=1:1:s,
if( (locations(i,1)==x) && (locations(i,2)==y)),
node=i+1;%+1 is offset as we start at id 2
%disp("found node index");
return
end;
end;
end
function Djakstras(distN,prevN,nodes,unVisited,relations)
while(~(isempty(unVisited)))
%the node with the minimum distance
u=find_minDist_unvisited(unVisited,distN,nodes)
%remove node from unVisited nodes
unVisited=setdiff(unVisited,u)
r=size(relations);
%iterate through all the relations
for relation=1:1:r(1),
%for all edges u,v
if ( relations(relation,1)==u ),
%calculate the new distN(v) and compare to old
new = distN(u-1)+1;
if (distN(relations(2)-1)==Inf),
distN(relations(2)-1)=new;
prevN(relations(2)-1)=u;
end;
%relations(2)=v but have to subtract offset 1
if( new < distN(relations(2)-1) ),
distN(relations(2)-1)=new;
prevN(relations(2)-1)=u;
end;
end;
end;
end
disp("djakstras done");
end
%returns the node with the min dist
function node_min=find_minDist_unvisited(unVisited,distN,nodes)
s=size(nodes);
%store the min distance
min_distance=Inf;
%return 0 by default we know this is wrong
node_min=0;
%loop through the nodes
for node=1:s(1),
%check if node is unvisited
if ismember(nodes(node),unVisited),
%find the distance of this node and compare
if(distN(node) <= min_distance),
%update the new distance and min_node
min_distance=distN(node);
node_min=nodes(node);
end;
end;
end;
end