-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPRM.m
87 lines (75 loc) · 2.92 KB
/
PRM.m
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
function roadmap = PRM (RandomSample, Dist, LocalPlanner, nsamples, k)
% PRM - ProbablisticRoadMap : This procedure computes a probabilistic road
% map of configuration space. It relies on 3 functions
% RandomSample which generates the coordinate vector for a random sample in
% free space. Dist which measures the distance between two
% coordinate vectors and LocalPlanner which decides whether two samples are
% connected.
%
% Inputs :
%
% RandomSample : A function that returns a random sample in freespace
%
% Dist : A function that computes the distance between a given point in
% configuration space and all of the entries in an array of samples
%
% LocalPlanner : A function that determines whether there is a collision
% free straight line path between two points in configuration space
%
% nsamples : The number of random samples to generate
%
% k : The number of neighbors that should be considered in
% forming the roadmap graph.
%
% Output :
% roadmap - a structure the samples, the edges and edge lengths in the
% roadmap graph
x = RandomSample();
% Array of random samples, each column corresponds to the coordinates
% of a point in configuration space.
samples = repmat(x(:), 1, nsamples);
% edges - an array with 2 rows each column has two integer entries
% (i, j) which encodes the fact that sample i and sample j are connected
% by an edge. For each
edges = zeros(nsamples*k, 2);
edge_lengths = zeros(nsamples*k, 1);
% nedges - this integer keeps track of the number of edges we
% have in the graph so far
nedges = 0;
for i = 2:nsamples
% Note that we are assuming that RandomSample returns a sample in
% freespace
x = RandomSample();
samples(:,i) = x(:);
% Find the nearest neighbors
% Here we assume that the Dist function can compute the
% distance to multiple samples corresponding to the columns of
% the second argument
% at the end of this call the array distances will indicate the
% distance between the new sample and each of the samples that has been
% generated so far in the program.
distances = Dist(x, samples(:,1:(i-1)));
%%% YOUR CODE HERE
%
% Find the closest k samples, use the LocalPlanner function to see if
% you can forge an edge to any of these samples and update the edges,
% edge_lengths and nedges variables accordingly.
%
[sorted_distances, idx] = sort(distances);
n = length(distances);
for i = 1:size(sorted_distances)
% display(i)
for j =idx(i)
% display(j)
if (LocalPlanner(x, samples(:,j)))
nedges = nedges + 1;
edges(nedges,:) = [n+1, j];
edge_lengths(nedges) = sorted_distances(i);
end
end
end
fprintf (1, 'nsamples = %d, nedges = %d\n', i, nedges);
end
roadmap.samples = samples;
roadmap.edges = edges(1:nedges, :);
roadmap.edge_lengths = edge_lengths(1:nedges);