Home > utility > narySearch4max.m

narySearch4max

PURPOSE ^

narySearch4max: N-ary-search-like method for 1-d optimization

SYNOPSIS ^

function [minPair, allPairs]=narySearch4max(objFunction, op, varargin)

DESCRIPTION ^

 narySearch4max: N-ary-search-like method for 1-d optimization
    Usage: [minPair, allPairs]=narySearch4max(objFunction, op, varargin)
        objFunction: Objective function
        op: Optimization parameters
            op.searchRange: Search range
            op.division: No. of division for each depth (a odd number)
            op.depth: depth for search
            op.plotOpt: Option for plotting
        varargin: Input arguments for objFunction
        minPair: minPair(1) is the minimizing x, minPair(2) is the corresponding value of objFunction
        allPairs: All pairs of function evalustions

    Number of evaluation counts = op.division+[(op.division-1)*2+1-3]*(op.depth-1).
    If op.searchRange=[0, 1000], op.division=11, op.depth=3, then the minimum search step is 1.

    For example:

        objFunction='humps';
        op.searchRange=[0.1, 0.95];
        op.division=11;
        op.depth=3;
        op.plotOpt=1;
        [minPair, allPairs]=narySearch4max(objFunction, op);

    See also binarySearch4min.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [minPair, allPairs]=narySearch4max(objFunction, op, varargin)
0002 % narySearch4max: N-ary-search-like method for 1-d optimization
0003 %    Usage: [minPair, allPairs]=narySearch4max(objFunction, op, varargin)
0004 %        objFunction: Objective function
0005 %        op: Optimization parameters
0006 %            op.searchRange: Search range
0007 %            op.division: No. of division for each depth (a odd number)
0008 %            op.depth: depth for search
0009 %            op.plotOpt: Option for plotting
0010 %        varargin: Input arguments for objFunction
0011 %        minPair: minPair(1) is the minimizing x, minPair(2) is the corresponding value of objFunction
0012 %        allPairs: All pairs of function evalustions
0013 %
0014 %    Number of evaluation counts = op.division+[(op.division-1)*2+1-3]*(op.depth-1).
0015 %    If op.searchRange=[0, 1000], op.division=11, op.depth=3, then the minimum search step is 1.
0016 %
0017 %    For example:
0018 %
0019 %        objFunction='humps';
0020 %        op.searchRange=[0.1, 0.95];
0021 %        op.division=11;
0022 %        op.depth=3;
0023 %        op.plotOpt=1;
0024 %        [minPair, allPairs]=narySearch4max(objFunction, op);
0025 %
0026 %    See also binarySearch4min.
0027 
0028 %    Roger Jang, 20070216, 20091124
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     % ====== left side
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     % ====== right side
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     % ====== Combine both sides
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);        % Original plot
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);        % Closeup plot
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 % ====== Self demo
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)]);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003