0001 function [minPair, allPairs]=narySearch4max(objFunction, op, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 if nargin<1; selfdemo; return; end
0031 if nargin<2;
0032 op.searchRange=[-2, 2];
0033 op.division=11;
0034 op.depth=3;
0035 op.plotOpt=0;
0036 end
0037
0038 xStart=op.searchRange(1); xStop=op.searchRange(2);
0039 if (xStart==xStop)
0040 error('The given search range is an empty set!');
0041 end
0042 x=linspace(xStart, xStop, op.division);
0043 y=zeros(1, op.division);
0044 fprintf('depth=%d/%d, range=%s\n', 1, op.depth, mat2str([xStart, xStop]));
0045 for j=1:length(x)
0046 t0=clock;
0047 y(j)=feval(objFunction, x(j), varargin{:});
0048 fprintf('\tj=%d/%d, x=%g, y=%g, time=%g sec\n', j, op.division, x(j), y(j), etime(clock, t0));
0049 end
0050 if sum(abs(diff(y)))==0
0051 fprintf('Warning: Obj. function has the same value at the first stage. Perhaps you should change the search range for a better search result.\n');
0052 end
0053 allPairs=[x; y];
0054 [yMax, indexMax]=max(y); xMax=x(indexMax);
0055 for i=2:op.depth
0056 index1=indexMax-1; if index1<1, index1=1; end
0057 index2=indexMax+1; if index2>length(x), index2=length(x); end
0058 xStart=x(index1); xStop=x(index2);
0059 fprintf('depth=%d/%d, range=%s\n', i, op.depth, mat2str([xStart, xStop]));
0060
0061 if (xStart~=xMax)
0062 x1=linspace(xStart, xMax, op.division);
0063 y1=zeros(1, op.division);
0064 for j=1:length(x1)-1
0065 t0=clock;
0066 y1(j)=feval(objFunction, x1(j), varargin{:});
0067 fprintf('\tj=%d/%d, x=%g, y=%g, time=%g sec\n', j, 2*op.division-1, x1(j), y1(j), etime(clock, t0));
0068 end
0069 x1(end)=[]; y1(end)=[];
0070 else
0071 x1=[]; y1=[];
0072 end
0073
0074 if (xMax~=xStop)
0075 x2=linspace(xMax, xStop, op.division);
0076 y2=zeros(1, op.division);
0077 for j=2:length(x2)
0078 t0=clock;
0079 y2(j)=feval(objFunction, x2(j), varargin{:});
0080 fprintf('\tj=%d/%d, x=%g, y=%g, time=%g sec\n', j+op.division-1, 2*op.division-1, x2(j), y2(j), etime(clock, t0));
0081 end
0082 x2(1)=[]; y2(1)=[];
0083 else
0084 x2=[]; y2=[];
0085 end
0086
0087 x=[x1, xMax, x2];
0088 y=[y1, yMax, y2];
0089 [yMax, indexMax]=max(y); xMax=x(indexMax);
0090 allPairs=[allPairs, [[x1, x2]; [y1, y2]]];
0091 end
0092 minPair=[xMax, yMax]';
0093
0094 if op.plotOpt
0095 x=allPairs(1,:); y=allPairs(2,:);
0096 [x, index]=sort(x); y=y(index);
0097 subplot(2,1,1);
0098 plot(x, y);
0099 xlabel('x'); ylabel('Obj. function');
0100 line(allPairs(1,:), allPairs(2,:), 'lineStyle', 'none', 'marker', '.', 'color', 'm');
0101 for i=1:size(allPairs, 2)
0102 text(allPairs(1,i), allPairs(2,i), int2str(i), 'vertical', 'top', 'hori', 'center');
0103 end
0104 line(minPair(1), minPair(2), 'lineStyle', 'none', 'marker', 'square', 'color', 'r');
0105 boundary=axis; yMin=boundary(3);
0106 for i=1:size(allPairs, 2)
0107 line(allPairs(1,i)*[1 1], [yMin, allPairs(2,i)], 'color', 'r');
0108 end
0109 box on; grid on; axis tight
0110 subplot(2,1,2);
0111 plot(x, y);
0112 xlabel('x'); ylabel('Obj. function');
0113 line(allPairs(1,:), allPairs(2,:), 'lineStyle', 'none', 'marker', '.', 'color', 'm');
0114 for i=1:size(allPairs, 2)
0115 text(allPairs(1,i), allPairs(2,i), int2str(i), 'vertical', 'top', 'hori', 'center');
0116 end
0117 line(minPair(1), minPair(2), 'lineStyle', 'none', 'marker', 'square', 'color', 'r');
0118 axis tight; box on; grid on;
0119 boundary=axis; yMin=boundary(3);
0120 for i=1:size(allPairs, 2)
0121 line(allPairs(1,i)*[1 1], [yMin, allPairs(2,i)], 'color', 'r');
0122 end
0123 box on; grid on; axis([minPair(1)-range(x)/40, minPair(1)+range(x)/40, -inf inf]);
0124 end
0125
0126
0127 function selfdemo
0128 objFunction='humps';
0129 op.searchRange=[0.1, 0.95];
0130 op.division=11;
0131 op.depth=3;
0132 op.plotOpt=1;
0133 [minPair, allPairs]=feval(mfilename, objFunction, op);
0134 fprintf('minX=%f, minY=%f\n', minPair(1), minPair(2));
0135 x=linspace(op.searchRange(1), op.searchRange(2), 1001);
0136 y=humps(x);
0137 line(x, y, 'color', 'g'); grid on
0138 set(gca, 'ylim', [min(y), max(y)]);