-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSolve2PBVPsNeighborhoodSet.m
286 lines (263 loc) · 11.6 KB
/
Solve2PBVPsNeighborhoodSet.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
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
% Solve2PBVPsNeighborhoodSet solves the optimal two point boundary value
% problem between a state and it's incoming or outgoing neighborhood
%
% Ross Allen, ASL, Stanford University
% May 21, 2014
%
% Inputs:
% - initIDs = (#) identification number of initial states of 2PBVPs.
% initIDs of length=1 calculates cost from state one to all finalIDs
% - finalIDs = (#) identification number of initial states of 2PBVPs.
% finalIDs of length=1 calculates cost from all initIDs to finalID.
% - maxNeighbors = (#) maximum number of neighbors to evaluate
%
% Functionality:
%
% Notes:
% - the labeling of n2PBVPs and nTot2PBVPs is misleading. nTot2PBVPs
% indicates the original, total number of 2PBVPs attempted, where
% n2PBVPs were the number of successful 2PBVPs out of those. However
% n2PBVPs was used as the CaseNum count so it grew after the initial
% BVP solution set
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mpinfo = Solve2PBVPsNeighborhoodSet(...
initIDs, finalIDs, maxNeighbors, mpinfo)
% Unpack variables to be accessed and modified
n2PBVPs = mpinfo.sampling.n2PBVPs;
%NOTE: modified in loop to avoid having to making additional copies
%evalMat
%trajMat
%controlMat
%outNeighborCell
%inNeighborCell
% Unpack variables to accessed (not modified)
sysDefs = mpinfo.systemDefs;
nStateDims = sysDefs.nStateDims;
nControlDims = sysDefs.nControlDims;
stateLabels = sysDefs.stateLabels;
controlLabels = sysDefs.controlLabels;
% set parameters that are constant for all runs
numerics.n_nodes = mpinfo.sampling.nTrajNodes;
robot = mpinfo.robot;
environment = mpinfo.environment;
options = mpinfo.onlineOptions;
options.print_summary = false;
options.plot_results = false;
options.costThreshold = mpinfo.learning.neighborCostThreshold;
% Check inputs
nCurProbs = length(initIDs);
if length(finalIDs) ~= nCurProbs
disp('Dimension mismatch between initIDs and finalIDs.')
disp('Exiting Solve2PBVPsNeighborhoodSet prematurely...')
return;
end
neighbor_counter = 0;
curTrajMat = zeros(nCurProbs, nStateDims, mpinfo.sampling.nTrajNodes);
curCtrlMat = zeros(nCurProbs, nControlDims, mpinfo.sampling.nTrajNodes);
nCur2PBVPs = 0;
for j = 1:nCurProbs
% skip if solving trivial pair (initial state = final state
if initIDs(j) == finalIDs(j)
continue;
end
% Set initial and final boundary values
boundary_values.t0 = 0;
for k = 1:nStateDims
boundary_values.([stateLabels{k},'0']) = ...
mpinfo.stateMat(initIDs(j),k);
boundary_values.([stateLabels{k},'f']) = ...
mpinfo.stateMat(finalIDs(j),k);
end
% Clear previous problem
clear bvpinfo
bvpinfo.numerics = numerics;
bvpinfo.robot = robot;
bvpinfo.boundary_values = boundary_values;
bvpinfo.environment = environment;
bvpinfo.options = options;
% Call Solver
bvpinfo = BVPOptimizer(bvpinfo, mpinfo.systemDefs);
% Save Cost, Trajectory and Control and neighborhood
if bvpinfo.solution.exitflag > 0
neighbor_counter = neighbor_counter + 1;
n2PBVPs = n2PBVPs+1;
nCur2PBVPs = nCur2PBVPs+1;
curCost = bvpinfo.solution.cost;
% evaluation number
mpinfo.evalMat(initIDs(j),finalIDs(j)) = n2PBVPs;
% cost
mpinfo.costMat(n2PBVPs,1) = curCost;
% extract state and control trajectory
% state trajectory
for k = 1:nStateDims
curTrajMat(nCur2PBVPs,k,:) = ...
bvpinfo.solution.(stateLabels{k});
end
% control trajectory
for k = 1:nControlDims
curCtrlMat(nCur2PBVPs,k,:) = ...
bvpinfo.solution.(controlLabels{k});
end
% if isfield(mpinfo.systemDefs, 'CustomBVPOptimizer')
% mpinfo.trajMat(n2PBVPs,:,:) = reshape(bvpinfo.solution.optTraj,...
% 1,nStateDims,mpinfo.sampling.nTrajNodes);
% mpinfo.controlMat(n2PBVPs,:,:) = reshape(bvpinfo.solution.optCtrl,...
% 1,nControlDims,mpinfo.sampling.nTrajNodes);
% else
% % state trajectory
% for k = 1:nStateDims
% mpinfo.trajMat(n2PBVPs,k,:) = ...
% bvpinfo.solution.(stateLabels{k});
% end
% % control trajectory
% for k = 1:nControlDims
% mpinfo.controlMat(n2PBVPs,k,:) =...
% bvpinfo.solution.(controlLabels{k});
% end
% end
% Insert into sorted outgoing neighborhood for initID
if isempty(mpinfo.outNeighborCell{initIDs(j)})
mpinfo.outNeighborCell{initIDs(j)} = [finalIDs(j) curCost];
mpinfo.outNeighborSortedIDs{initIDs(j)} = uint32(finalIDs(j));
if curCost < mpinfo.learning.neighborCostThreshold
mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)} = uint32(finalIDs(j));
end
else
% Sorted by Cost
insertIX = find(mpinfo.outNeighborCell{initIDs(j)}(:,2) > curCost, 1);
if insertIX == 1
mpinfo.outNeighborCell{initIDs(j)} =...
[[finalIDs(j) curCost];...
mpinfo.outNeighborCell{initIDs(j)}];
elseif isempty(insertIX)
mpinfo.outNeighborCell{initIDs(j)} =...
[mpinfo.outNeighborCell{initIDs(j)};...
[finalIDs(j) curCost]];
else
mpinfo.outNeighborCell{initIDs(j)} =...
[mpinfo.outNeighborCell{initIDs(j)}(1:insertIX-1,:);...
[finalIDs(j) curCost];...
mpinfo.outNeighborCell{initIDs(j)}(insertIX:end,:)];
end
clear insertIX;
% Sorted by ID
insertIX = find(mpinfo.outNeighborSortedIDs{initIDs(j)} > finalIDs(j), 1);
if insertIX == 1
mpinfo.outNeighborSortedIDs{initIDs(j)} =...
uint32([finalIDs(j);...
mpinfo.outNeighborSortedIDs{initIDs(j)}]);
elseif isempty(insertIX)
mpinfo.outNeighborSortedIDs{initIDs(j)} =...
uint32([mpinfo.outNeighborSortedIDs{initIDs(j)};...
finalIDs(j)]);
else
mpinfo.outNeighborSortedIDs{initIDs(j)} =...
uint32([mpinfo.outNeighborSortedIDs{initIDs(j)}(1:insertIX-1);...
finalIDs(j);...
mpinfo.outNeighborSortedIDs{initIDs(j)}(insertIX:end)]);
end
clear insertIX;
% Sorted by ID and trimmed to the cost threshold
if curCost < mpinfo.learning.neighborCostThreshold
insertIX = find(mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)} > finalIDs(j), 1);
if insertIX == 1
mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)} =...
uint32([finalIDs(j);...
mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)}]);
elseif isempty(insertIX)
mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)} =...
uint32([mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)};...
finalIDs(j)]);
else
mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)} =...
uint32([mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)}(1:insertIX-1);...
finalIDs(j);...
mpinfo.outNeighborTrimmedSortedIDs{initIDs(j)}(insertIX:end)]);
end
clear insertIX;
end
end
% Insert into sorted incoming neighborhood for finalID
if isempty(mpinfo.inNeighborCell{finalIDs(j)})
mpinfo.inNeighborCell{finalIDs(j)} = [initIDs(j) curCost];
mpinfo.inNeighborSortedIDs{finalIDs(j)} = uint32(initIDs(j));
if curCost < mpinfo.learning.neighborCostThreshold
mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)} = uint32(initIDs(j));
end
else
% Sorted by Cost
insertIX = find(mpinfo.inNeighborCell{finalIDs(j)}(:,2) > curCost, 1);
if insertIX == 1
mpinfo.inNeighborCell{finalIDs(j)} =...
[[initIDs(j) curCost];...
mpinfo.inNeighborCell{finalIDs(j)}];
elseif isempty(insertIX)
mpinfo.inNeighborCell{finalIDs(j)} =...
[mpinfo.inNeighborCell{finalIDs(j)};...
[initIDs(j) curCost]];
else
mpinfo.inNeighborCell{finalIDs(j)} =...
[mpinfo.inNeighborCell{finalIDs(j)}(1:insertIX-1,:);...
[initIDs(j) curCost];...
mpinfo.inNeighborCell{finalIDs(j)}(insertIX:end,:)];
end
clear insertIX;
% Sorted by ID
insertIX = find(mpinfo.inNeighborSortedIDs{finalIDs(j)} > initIDs(j), 1);
if insertIX == 1
mpinfo.inNeighborSortedIDs{finalIDs(j)} =...
uint32([initIDs(j);...
mpinfo.inNeighborSortedIDs{finalIDs(j)}]);
elseif isempty(insertIX)
mpinfo.inNeighborSortedIDs{finalIDs(j)} =...
uint32([mpinfo.inNeighborSortedIDs{finalIDs(j)};...
initIDs(j)]);
else
mpinfo.inNeighborSortedIDs{finalIDs(j)} =...
uint32([mpinfo.inNeighborSortedIDs{finalIDs(j)}(1:insertIX-1);...
initIDs(j);...
mpinfo.inNeighborSortedIDs{finalIDs(j)}(insertIX:end)]);
end
clear insertIX;
% Sorted by ID and trimmed to the cost threshold
if curCost < mpinfo.learning.neighborCostThreshold
insertIX = find(mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)} > initIDs(j), 1);
if insertIX == 1
mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)} =...
uint32([initIDs(j);...
mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)}]);
elseif isempty(insertIX)
mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)} =...
uint32([mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)};...
initIDs(j)]);
else
mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)} =...
uint32([mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)}(1:insertIX-1);...
initIDs(j);...
mpinfo.inNeighborTrimmedSortedIDs{finalIDs(j)}(insertIX:end)]);
end
clear insertIX;
end
end
end
% Check if max number of neighbors reached
if neighbor_counter >= maxNeighbors
break;
end
end
% remove exess storage for curTraj and curCtrl matrices
curTrajMat(nCur2PBVPs+1:end,:,:) = [];
curCtrlMat(nCur2PBVPs+1:end,:,:) = [];
% concatentate curTrajMat onto mpinfo.trajMat only once to avoid repeated
% memory allocations
mpinfo.trajMat = cat(1,mpinfo.trajMat,curTrajMat);
mpinfo.controlMat = cat(1,mpinfo.controlMat,curCtrlMat);
% Consolidate Results
mpinfo.sampling.n2PBVPs = n2PBVPs;
%NOTE: modified in loop to avoid having to making additional copies
%evalMat
%trajMat
%controlMat
%outNeighborCell
%inNeighborCell
end