Home > utility > binarySearch4min.m

binarySearch4min

PURPOSE ^

binarySearch4min: Binary-search-like method for 1-d minimization

SYNOPSIS ^

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

DESCRIPTION ^

 binarySearch4min: Binary-search-like method for 1-d minimization
    Usage: [minPair, allPairs]=binarySearch4min(objFunction, op, varargin)
        objFunction: Objective function
        op: Optimization parameters
            op.searchRange: Search range
            op.evalCount: No. of function evaluations
            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

    For example:
        objFunction='humps';
        op.searchRange=[0.0, 1.0];
        op.evalCount=11;
        op.plotOpt=1;
        [minPair, allPairs]=binarySearch4min(objFunction, op);
        fprintf('minX=%f, minY=%f\n', minPair(1), minPair(2));
        x=linspace(op.searchRange(1), op.searchRange(2), 101);
        y=humps(x);
        line(x, y, 'color', 'r'); grid on
        set(gca, 'ylim', [min(y), max(y)]);

    See also narySearch4max.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [minPair, allPairs]=binarySearch4min(objFunction, op, varargin)
0002 % binarySearch4min: Binary-search-like method for 1-d minimization
0003 %    Usage: [minPair, allPairs]=binarySearch4min(objFunction, op, varargin)
0004 %        objFunction: Objective function
0005 %        op: Optimization parameters
0006 %            op.searchRange: Search range
0007 %            op.evalCount: No. of function evaluations
0008 %            op.plotOpt: Option for plotting
0009 %        varargin: Input arguments for objFunction
0010 %        minPair: minPair(1) is the minimizing x, minPair(2) is the corresponding value of objFunction
0011 %        allPairs: All pairs of function evalustions
0012 %
0013 %    For example:
0014 %        objFunction='humps';
0015 %        op.searchRange=[0.0, 1.0];
0016 %        op.evalCount=11;
0017 %        op.plotOpt=1;
0018 %        [minPair, allPairs]=binarySearch4min(objFunction, op);
0019 %        fprintf('minX=%f, minY=%f\n', minPair(1), minPair(2));
0020 %        x=linspace(op.searchRange(1), op.searchRange(2), 101);
0021 %        y=humps(x);
0022 %        line(x, y, 'color', 'r'); grid on
0023 %        set(gca, 'ylim', [min(y), max(y)]);
0024 %
0025 %    See also narySearch4max.
0026 
0027 %    Roger Jang, 20060606
0028 
0029 if nargin<1; selfdemo; return; end
0030 if nargin<2;
0031     op.searchRange=[-2, 2];
0032     op.evalCount=31;
0033     op.plotOpt=0;
0034 end
0035 
0036 x=mean(op.searchRange);
0037 side=(max(op.searchRange)-x)/2;
0038 
0039 y1=feval(objFunction, x, varargin{:});
0040 allPairs=[x; y1];
0041 for j=1:(op.evalCount-1)/2
0042     y2 = feval(objFunction, x-side, varargin{:});
0043     y3 = feval(objFunction, x+side, varargin{:});
0044     allPairs=[allPairs, [x-side; y2], [x+side; y3]];
0045     y=[y1, y2, y3];
0046     shift=[0, -side, side];
0047     [y1, minIndex]=min(y);
0048     x=x+shift(minIndex);
0049     side=side/2;
0050 end
0051 minPair=[x, y1]';
0052 
0053 if op.plotOpt
0054     x=allPairs(1,:);
0055     y=allPairs(2,:);
0056     [junk, index]=sort(x);
0057     plot(x(index), y(index));
0058     line(allPairs(1,:), allPairs(2,:), 'lineStyle', 'none', 'marker', '.', 'color', 'm');
0059     for i=1:op.evalCount
0060         text(allPairs(1,i), allPairs(2,i), int2str(i), 'vertical', 'top', 'hori', 'center');
0061     end
0062     line(minPair(1), minPair(2), 'lineStyle', 'none', 'marker', 'square', 'color', 'r');
0063     axis([op.searchRange(1), op.searchRange(2), -inf inf]); box on;
0064 end
0065 
0066 % ====== Self demo
0067 function selfdemo
0068 objFunction='humps';
0069 op.searchRange=[0.0, 1.0];
0070 op.evalCount=11;
0071 op.plotOpt=1;
0072 [minPair, allPairs]=feval(mfilename, objFunction, op);
0073 fprintf('minX=%f, minY=%f\n', minPair(1), minPair(2));
0074 x=linspace(op.searchRange(1), op.searchRange(2), 101);
0075 y=humps(x);
0076 line(x, y, 'color', 'r'); grid on
0077 set(gca, 'ylim', [min(y), max(y)]);

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