-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspmultistart.m
162 lines (145 loc) · 4.51 KB
/
spmultistart.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
function [xopt,fval,exitflag,output] = spmultistart(z, xbox, options)
% SPMULTISTART Multiple random start optimization method for
% sparse grid interpolants.
% X = SPMULTISTART(Z) Attemps to find a global optimizer X of the
% sparse grid interpolant Z by performing multiple local searches
% starting from random start points. The entire range of the
% sparse grid interpolant is searched. Using the default settings,
% the first start point is not random but the best available sparse
% grid point. By default, SPCOMPSEARCH is used for the local
% searches if the grid type is not Chebyshev. If it is, SPCGSEARCH
% is used.
%
% X = SPMULTISTART(Z, XBOX) Uses the search box XBOX, X = [a1,
% b1; a2, b2; ...]. The size of search box XBOX must be smaller
% than or equal to the range of the interpolant.
%
% X = SPMULTISTART(Z, XBOX, OPTIONS) Additionally, an
% OPTIONS structure can be provided, created with SPOPTIMSET.
% For instance, the local optimization method (e.g. SPFMINSEARCH)
% used can be selected.
%
% [X,FVAL] = SPMULTISTART(...) returns the value of the
% sparse grid interpolant at X.
%
% [X,FVAL,EXITFLAG] = SPMULTISTART(...) returns a EXITFLAG
% that describes the exit condition of SPCOMPSEARCH. Possible
% values of EXITFLAG and the corresponding exit conditions are
%
% 1 SPMULTISTART converged to at least one solution X.
% 0 Maximum number of function evaluations or iterations
% reached for all local searches.
%
% [X,FVAL,EXITFLAG,OUTPUT] = SPMULTISTART(...) returns a
% structure OUTPUT with the following information:
% .time : Total computing time in s.
% .allResults : Cell array of all local search results.
%
% Example:
% f = inline(['(5/pi*x-5.1/(4*pi^2)*x.^2+y-6).^2 +' ...
% '10*(1-1/(8*pi))*cos(x)+10']);
% range = [-5 10; 0 15];
% options = spset('keepFunctionValues','on', ...
% 'GridType', 'Chebyshev', ...
% 'DimensionAdaptive', 'on', ...
% 'DimAdaptDegree', 1, ...
% 'MinPoints', 10);
% z = spvals(f, 2, range, options);
% [xopt, fval] = spmultistart(z)
%
% See also SPOPTIMSET.
t0 = clock;
if nargin < 2, xbox = []; end
if nargin < 3, options = []; end
d = z.d;
% In case that no range has been provided to spvals -> set it to
% [0,1]^d.
if isempty(z.range)
z.range = [zeros(d,1) ones(d,1)];
end
if isempty(xbox)
xbox = z.range;
end
minimize = spoptimget(options, 'Minimize', 'on');
maximize = spoptimget(options, 'Maximize', 'off');
isminimize = 0; ismaximize = 0;
xoptmin = []; xoptmax = [];
ymin = []; ymax = [];
if strcmpi(minimize, 'on'), isminimize = 1; end
if strcmpi(maximize, 'on'), ismaximize = 1; end
switch(lower(z.gridType))
case {'maximum', 'noboundary', 'clenshaw-curtis', 'gauss-patterson'}
method = spoptimget(options, 'Method', 'spcompsearch');
case {'chebyshev'}
method = spoptimget(options, 'Method', 'spcgsearch');
otherwise
error('MATLAB:spinterp:badopt',['Unknown grid type ''' gridtype '''.']);
end
dispopt = spoptimget(options, 'Display', 'off');
d = z.d;
% By default, do multiple optimizations from best and from random
% start points.
snum = spoptimget(options, 'NumStarts', 10);
if isminimize
xminvec = zeros(d,snum);
yminvec = zeros(1,snum);
end
if ismaximize
xmaxvec = zeros(d,snum);
ymaxvec = zeros(1,snum);
end
xopt = [];
ymin = [];
ymax = [];
exitflag = [];
if nargout == 4
allResults.x = [];
allResults.fval = [];
allResults.exitflag = [];
allResults.output = {};
end
for k = 1:snum
if k == 1
options.StartPoint = 'best';
else
options.StartPoint = 'random';
end
if nargout == 4
[xopt, fval, tmpexitflag, tmpoutput] = feval(method, z, xbox, options);
allResults.x = [allResults.x xopt];
allResults.fval = [allResults.fval fval];
allResults.exitflag = [allResults.exitflag tmpexitflag];
allResults.output = [allResults.output tmpoutput];
else
[xopt, fval, tmpexitflag] = feval(method, z, xbox, options);
end
if isempty(exitflag)
exitflag = tmpexitflag;
else
exitflag = max(exitflag,tmpexitflag);
end
s = 1;
if isminimize
xminvec(:,k) = xopt(:,1);
yminvec(k) = fval(1);
s = 2;
end
if ismaximize
xmaxvec(:,k) = xopt(:,s);
ymaxvec(k) = fval(s);
end
end
xopt = [];
if isminimize
[ymin, id] = min(yminvec);
xopt = xminvec(:,id);
end
if ismaximize
[ymax, id] = max(ymaxvec);
xopt = [xopt xmaxvec(:,id)];
end
fval = [ymin ymax];
if nargout == 4
output.time = etime(clock, t0);
output.allResults = allResults;
end