-
Notifications
You must be signed in to change notification settings - Fork 209
/
getDefaultOptions.m
301 lines (246 loc) · 8.31 KB
/
getDefaultOptions.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
function problem = getDefaultOptions(problem)
% problem = getDefaultOptions(problem)
%
% This function fills in any blank entries in the problem.options struct.
% It is designed to be called from inside of optimTraj.m, and not by the
% user.
%
%%%% Top-level default options:
OPT.method = 'trapezoid';
OPT.verbose = 2;
OPT.defaultAccuracy = 'medium';
%%%% Basic setup
% ensure that options is not empty
if ~isfield(problem,'options')
problem.options.method = OPT.method;
end
opt = problem.options;
% Loop over each options struct and fill in top-level options
for i=1:length(opt)
if ~isfield(opt(i),'method')
opt(i).method = OPT.method;
elseif isempty(opt(i).method)
opt(i).method = OPT.method;
end
if ~isfield(opt(i),'verbose')
opt(i).verbose = OPT.verbose;
elseif isempty(opt(i).verbose)
opt(i).verbose = OPT.verbose;
end
if ~isfield(opt(i),'defaultAccuracy')
opt(i).defaultAccuracy = OPT.defaultAccuracy;
elseif isempty(opt(i).defaultAccuracy)
opt(i).defaultAccuracy = OPT.defaultAccuracy;
end
end
% Figure out basic problem size:
nState = size(problem.guess.state,1);
nControl = size(problem.guess.control,1);
% Loop over opt and fill in nlpOpt struct:
for i=1:length(opt)
switch opt(i).verbose
case 0
NLP_display = 'notify';
case 1
NLP_display = 'final-detailed';
case 2
NLP_display = 'iter';
case 3
NLP_display = 'iter-detailed';
otherwise
error('Invalid value for options.verbose');
end
switch opt(i).defaultAccuracy
case 'low'
OPT.nlpOpt = optimset(...
'Display',NLP_display,...
'TolFun',1e-4,...
'MaxIter',200,...
'MaxFunEvals',1e4*(nState+nControl));
case 'medium'
OPT.nlpOpt = optimset(...
'Display',NLP_display,...
'TolFun',1e-6,...
'MaxIter',400,...
'MaxFunEvals',5e4*(nState+nControl));
case 'high'
OPT.nlpOpt = optimset(...
'Display',NLP_display,...
'TolFun',1e-8,...
'MaxIter',800,...
'MaxFunEvals',1e5*(nState+nControl));
otherwise
error('Invalid value for options.defaultAccuracy')
end
if isfield(opt(i),'nlpOpt')
if isstruct(opt(i).nlpOpt) && ~isempty(opt(i).nlpOpt)
names = fieldnames(opt(i).nlpOpt);
for j=1:length(names)
if ~isfield(OPT.nlpOpt,names{j})
disp(['WARNING: options.nlpOpt.' names{j} ' is not a valid option']);
else
OPT.nlpOpt.(names{j}) = opt(i).nlpOpt.(names{j});
end
end
end
end
opt(i).nlpOpt = OPT.nlpOpt;
end
% Check ChebFun dependency:
missingChebFun = false;
for i=1:length(opt)
if strcmp(opt(i).method,'chebyshev')
try
chebpts(3); %Test call to chebfun
catch ME %#ok<NASGU>
missingChebFun = true;
opt(i).method = 'trapezoid'; %Force default method
end
end
end
if missingChebFun
warning('''chebyshev'' method requires the Chebfun toolbox');
disp(' --> Install Chebfun toolbox: (http://www.chebfun.org/)');
disp(' --> Running with default method instead (''trapezoid'')');
end
% Fill in method-specific paramters:
for i=1:length(opt)
OPT_method = opt(i).method;
switch OPT_method
case 'trapezoid'
OPT.trapezoid = defaults_trapezoid(opt(i).defaultAccuracy);
case 'hermiteSimpson'
OPT.hermiteSimpson = defaults_hermiteSimpson(opt(i).defaultAccuracy);
case 'chebyshev'
OPT.chebyshev = defaults_chebyshev(opt(i).defaultAccuracy);
case 'multiCheb'
OPT.multiCheb = defaults_multiCheb(opt(i).defaultAccuracy);
case 'rungeKutta'
OPT.rungeKutta = defaults_rungeKutta(opt(i).defaultAccuracy);
case 'gpops'
OPT.gpops = defaults_gpops(opt(i).defaultAccuracy);
otherwise
error('Invalid value for options.method');
end
if isfield(opt(i),OPT_method)
if isstruct(opt(i).(OPT_method)) && ~isempty(opt(i).(OPT_method))
names = fieldnames(opt(i).(OPT_method));
for j=1:length(names)
if ~isfield(OPT.(OPT_method),names{j})
disp(['WARNING: options.' OPT_method '.' names{j} ' is not a valid option']);
else
OPT.(OPT_method).(names{j}) = opt(i).(OPT_method).(names{j});
end
end
end
end
opt(i).(OPT_method) = OPT.(OPT_method);
end
problem.options = opt;
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
% Method-specific parameters %
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
function OPT_trapezoid = defaults_trapezoid(accuracy)
switch accuracy
case 'low'
OPT_trapezoid.nGrid = 12;
case 'medium'
OPT_trapezoid.nGrid = 30;
case 'high'
OPT_trapezoid.nGrid = 60;
otherwise
error('Invalid value for options.defaultAccuracy')
end
OPT_trapezoid.adaptiveDerivativeCheck = 'off';
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
function OPT_hermiteSimpson = defaults_hermiteSimpson(accuracy)
switch accuracy
case 'low'
OPT_hermiteSimpson.nSegment = 10;
case 'medium'
OPT_hermiteSimpson.nSegment = 20;
case 'high'
OPT_hermiteSimpson.nSegment = 40;
otherwise
error('Invalid value for options.defaultAccuracy')
end
OPT_hermiteSimpson.adaptiveDerivativeCheck = 'off';
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
function OPT_chebyshev = defaults_chebyshev(accuracy)
switch accuracy
case 'low'
OPT_chebyshev.nColPts = 9;
case 'medium'
OPT_chebyshev.nColPts = 13;
case 'high'
OPT_chebyshev.nColPts = 23;
otherwise
error('Invalid value for options.defaultAccuracy')
end
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
function OPT_multiCheb = defaults_multiCheb(accuracy)
switch accuracy
case 'low'
OPT_multiCheb.nColPts = 6;
OPT_multiCheb.nSegment = 3;
case 'medium'
OPT_multiCheb.nColPts = 8;
OPT_multiCheb.nSegment = 6;
case 'high'
OPT_multiCheb.nColPts = 8;
OPT_multiCheb.nSegment = 12;
otherwise
error('Invalid value for options.defaultAccuracy')
end
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
function OPT_rungeKutta = defaults_rungeKutta(accuracy)
switch accuracy
case 'low'
OPT_rungeKutta.nSegment = 10;
OPT_rungeKutta.nSubStep = 2;
case 'medium'
OPT_rungeKutta.nSegment = 20;
OPT_rungeKutta.nSubStep = 2;
case 'high'
OPT_rungeKutta.nSegment = 20;
OPT_rungeKutta.nSubStep = 4;
otherwise
error('Invalid value for options.defaultAccuracy')
end
OPT_rungeKutta.adaptiveDerivativeCheck = 'off';
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
function OPT_gpops = defaults_gpops(accuracy)
OPT_gpops.bounds.phase.integral.lower = -inf;
OPT_gpops.bounds.phase.integral.upper = inf;
OPT_gpops.guess.phase.integral = 0;
OPT_gpops.name = 'OptimTraj_GPOPS';
OPT_gpops.auxdata = [];
OPT_gpops.nlp.solver = 'ipopt'; % {'ipopt','snopt'}
OPT_gpops.derivatives.dependencies = 'full'; %�full�, �sparse� or �sparseNaN�
OPT_gpops.derivatives.supplier = 'sparseCD'; %'sparseCD'; %'adigator'
OPT_gpops.derivatives.derivativelevel = 'first'; %'second';
OPT_gpops.mesh.method = 'hp-PattersonRao';
OPT_gpops.method = 'RPM-Integration';
OPT_gpops.mesh.phase.colpoints = 10*ones(1,10);
OPT_gpops.mesh.phase.fraction = ones(1,10)/10;
OPT_gpops.scales.method = 'none'; % { 'none' , automatic-hybridUpdate' , 'automatic-bounds';
switch accuracy
case 'low'
OPT_gpops.mesh.tolerance = 1e-2;
OPT_gpops.mesh.maxiterations = 0;
case 'medium'
OPT_gpops.mesh.tolerance = 1e-3;
OPT_gpops.mesh.maxiterations = 1;
case 'high'
OPT_gpops.mesh.tolerance = 1e-4;
OPT_gpops.mesh.maxiterations = 3;
otherwise
error('Invalid value for options.defaultAccuracy')
end
end