-
Notifications
You must be signed in to change notification settings - Fork 3
/
impulse2LTI_opts.m
258 lines (218 loc) · 7.3 KB
/
impulse2LTI_opts.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
%--------------------------------------------------------------------------
% impulse2LTI_opts.m
% Set the unset options to their defaults
%--------------------------------------------------------------------------
%
%--------------------------------------------------------------------------
% Primary contributor: Daniel R. Herber (danielrherber)
% Link: https://github.com/danielrherber/impulse-2-lti
%--------------------------------------------------------------------------
function opts = impulse2LTI_opts(opts)
%--------------------------------------------------------------------------
% general options
%--------------------------------------------------------------------------
% plots
if ~isfield(opts,'plotflag')
opts.plotflag = true; % create the plots
% opts.plotflag = false; % don't create the plots
end
% save the solution to disk
if ~isfield(opts,'saveflag')
% opts.saveflag = true; % save
opts.saveflag = false; % don't save
end
% name of the example
if ~isfield(opts,'name')
opts.name = 'impulse2LTI';
end
% path for saving
if ~isfield(opts,'path')
opts.path = mfoldername('INSTALL_impulse2LTI','');
end
% controls displaying diagnostics to the command window
if ~isfield(opts,'displevel')
opts.displevel = 3; % very verbose
% opts.displevel = 2; % verbose
% opts.displevel = 1; % minimal
% opts.displevel = 0; % none
end
% parallel computing
if ~isfield(opts,'parallel')
% opts.parallel = true; % yes
opts.parallel = false; % no
end
% potentially start the parallel pool
if opts.parallel
gcp;
end
%--------------------------------------------------------------------------
% testing points options
%--------------------------------------------------------------------------
% initialize field
if ~isfield(opts,'points')
opts.points = [];
end
% length of the time horizon
if ~isfield(opts.points,'tf')
opts.points.tf = 5;
end
% number of testing points
if ~isfield(opts.points,'N')
opts.points.N = 2000;
end
% reduce the number of testing points based on MFX 52552
if ~isfield(opts.points,'reduceflag')
% opts.points.reduceflag = true; % yes
opts.points.reduceflag = false; % no
end
% normalize the testing points
if ~isfield(opts.points,'normflag')
opts.points.normflag = true; % yes
% opts.points.normflag = false; % no
end
%--------------------------------------------------------------------------
% fitting options
%--------------------------------------------------------------------------
% initialize field
if ~isfield(opts,'fitting')
opts.fitting = [];
end
% fitting formulation
if ~isfield(opts.fitting,'formulation')
% opts.fitting.formulation = 'full'; % NOT AVAILABLE
% opts.fitting.formulation = 'canon.direct'; % NOT AVAILABLE
% opts.fitting.formulation = 'canon.direct.ls'; % NOT AVAILABLE
% opts.fitting.formulation = 'canon.roots'; % NOT AVAILABLE
% opts.fitting.formulation = 'canon.roots.ls'; % NOT AVAILABLE
% opts.fitting.formulation = 'prony';
% opts.fitting.formulation = 'basis.pbf';
opts.fitting.formulation = 'basis.pbf.ls';
% opts.fitting.formulation = 'basis.ghm'; % NOT AVAILABLE
% opts.fitting.formulation = 'basis.ghm.ls'; % NOT AVAILABLE
% opts.fitting.formulation = 'basis.ps'; % NOT AVAILABLE
% opts.fitting.formulation = 'basis.ps.ls'; % NOT AVAILABLE
% opts.fitting.formulation = 'imp2ss';
end
% fitting algorithm
if ~isfield(opts.fitting,'algorithm')
% opts.fitting.algorithm = 'multistart-lsqnonlin';
% opts.fitting.algorithm = 'particleswarm-lsqnonlin';
% opts.fitting.algorithm = 'ga-lsqnonlin';
% opts.fitting.algorithm = 'particleswarm';
% opts.fitting.algorithm = 'ga';
% opts.fitting.algorithm = 'patternsearch';
% opts.fitting.algorithm = 'fmincon';
opts.fitting.algorithm = 'lsqnonlin';
end
% number of basis functions, 2*N states
if ~isfield(opts.fitting,'N')
opts.fitting.N = 4; % 4 basis functions
end
% scale the optimization variables between 0-1
if ~isfield(opts.fitting,'scaleflag')
opts.fitting.scaleflag = true; % use
% opts.fitting.scaleflag = false; % don't use
end
% minimum amplitude bound
if ~isfield(opts.fitting,'T1min')
opts.fitting.T1min = 0;
end
% maximum amplitude bound
if ~isfield(opts.fitting,'T1max')
opts.fitting.T1max = 500;
end
% minimum decay rate bound
if ~isfield(opts.fitting,'T2min')
opts.fitting.T2min = 1e-4;
end
% maximum decay rate bound
if ~isfield(opts.fitting,'T2max')
opts.fitting.T2max = 100;
end
% minimum frequency bound
if ~isfield(opts.fitting,'T3min')
opts.fitting.T3min = 0;
end
% maximum frequency bound
if ~isfield(opts.fitting,'T3max')
opts.fitting.T3max = 300;
end
% minimum phase shift
if ~isfield(opts.fitting,'T4min')
opts.fitting.T4min = 0;
end
% maximum phase shift
if ~isfield(opts.fitting,'T4max')
opts.fitting.T4max = 2*pi;
end
% number of start points to test with multistart option
if ~isfield(opts.fitting,'nStart')
opts.fitting.nStart = 10;
end
% perform model reduction to reduce the number of states
if ~isfield(opts,'modredflag')
opts.modredflag = true; % use
% opts.modredflag = false % don't use
end
% optimization algorithm options (see function below)
opts = opts_algorithms(opts);
%--------------------------------------------------------------------------
% plotting options
%--------------------------------------------------------------------------
% initialize field
if ~isfield(opts,'plot')
opts.plot = [];
end
% perform the additional simulations for validating the fit
if ~isfield(opts.plot,'simulationflag')
% opts.plot.simulationflag = true; % yes
opts.plot.simulationflag = false; % no
end
end
% optimization algorithm options subfunction
function opts = opts_algorithms(opts)
% extract
algorithm = opts.fitting.algorithm;
% control display of iterations during the fitting
if (opts.displevel > 2) % very verbose
iterflag = 'iter';
else
iterflag = 'none';
end
% lsqnonlin options
if contains(algorithm,'lsqnonlin','IgnoreCase',true)
opts.fitting.lsqnonlin = optimoptions('lsqnonlin','Display',iterflag,...
'UseParallel',opts.parallel,'StepTolerance',1e-7,...
'FunctionTolerance',0,'OptimalityTolerance',0,...
'MaxFunctionEvaluations',1e7,'MaxIterations',50000,...
'SpecifyObjectiveGradient',true,...
'CheckGradients',false);
end
% fmincon options
if contains(algorithm,'fmincon','IgnoreCase',true)
opts.fitting.fmincon = optimoptions('fmincon','Display',iterflag,...
'UseParallel',opts.parallel);
end
% patternsearch options
if contains(algorithm,'patternsearch','IgnoreCase',true)
opts.fitting.patternsearch = optimoptions('patternsearch',...
'Display',iterflag,'UseParallel',opts.parallel);
end
% ga options
if contains(algorithm,'ga','IgnoreCase',true)
opts.fitting.ga = optimoptions('ga',...
'Display',iterflag,'UseParallel',opts.parallel,...
'MaxGenerations',50,'PopulationSize',5000);
end
% particleswarm options
if contains(algorithm,'particleswarm','IgnoreCase',true)
opts.fitting.particleswarm = optimoptions('particleswarm',...
'Display',iterflag,'UseParallel',opts.parallel,'SwarmSize',1000,...
'MaxIterations',200);
end
% multistart options
if contains(algorithm,'multistart','IgnoreCase',true)
opts.fitting.multistart = MultiStart('Display',iterflag,...
'UseParallel',opts.parallel,'StartPointsToRun','bounds');
end
end