% Chapter 6: Numerical Techniques
%            For Unconstrained Optimization
%--------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Applied Optimization with Matlab Programming
% Dr. P.Venkataraman
% Second Edition,  John Wiley
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Zoom and Scan Algorithm for unconstrained minimum
% Side constraints are not checked
%  P. Venkataraman - April 2008
%--------------------------------------------------
%  6.5.2   Scan and Zoom 
% Example 6.3: Zero Order Method
%  Boundary value Problem
%------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% management functions
clear  % clear all variable/information in the workspace - use CAUTION
clc    % position the cursor at the top of the screen
close   %  closes the figure window
format compact  % avoid skipping a line when writing to the command window
warning off  % don't report any warnings like divide by zero etc.

%%%   Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1Low =  0;  x1High = 2;
x2Low = -2;  x2High = 2;
x3Low =  3;  x3High = 10;

%%%  stopping criteria is based on the tolerance if the
%%% objective function

%%% monitor cpu time
starttime = cputime;

%%% choose current solution at mid point of design region
%%% this will also be center of the design space at current level
x1mid = 0.5*(x1High+x1Low);
x2mid = 0.5*(x2High+x2Low);
x3mid = 0.5*(x3High+x3Low);

%%% the extant of the design region in current scanning level
delx1 = 0.5*(x1High-x1Low);
delx2 = 0.5*(x2High-x2Low);
delx3 = 0.5*(x3High-x3Low);

%%% tolerance on search for variables and function
tolx1 = 1.0e-08;
tolx2 = 1.0e-08;
tolx3 = 1.0e-08;
ftol =  1.0e-08;

%%% define the limits for search in the current level
x1L = x1mid - delx1;   x1U = x1mid + delx1;
x2L = x2mid - delx2;   x2U = x2mid + delx2;
x3L = x3mid - delx3;   x3U = x3mid + delx3;

x1m = 0;  % storage for the minimum value of x1
x2m = 0;  % storage for the minimum value of x2
x3m = 0;   % storage for the minimum value of x2
fm = 1.0e10;  % default min - large number

nSL = 50;  % number of scanning levels
nSP = 11; % number of scanning points per level

for ii = 1: nSL
    x1 = linspace(x1L,x1U,nSP);
    x2 = linspace(x2L,x2U,nSP);
    x3 = linspace(x3L,x3U,nSP);
    for i = 1:nSP
        for j = 1:nSP
            for k = 1:nSP
                xv = [x1(i), x2(j), x3(k)];
                fun = Example6_3(xv);

                if fun < fm
                    x1m = x1(i);
                    x2m = x2(j);
                    x3m = x3(k);
                    fm = fun;
                end
            end
        end
    end
    
    %%% print the best values for the current zoom level

    fprintf('\n Example 6.3 ')
    fprintf('\n-------------')
    fprintf('\n Zoom Level (%2i) :\n',ii)
    fprintf('\nx1* = %10.7f',x1m)
    fprintf('\nx2* = %10.7f',x2m)
    fprintf('\nx3* = %10.7f',x3m)
    fprintf('\n f* = %10.7f\n',fm)
    
    % stopping condition based on the tolerance
    % in the objective function with zoom level
    
    if abs (fun - fm) <= ftol
        fprintf('\nReached tolerance in f %7.3e \n',ftol)
        break;
    end
%     %  stopping based on tolerance on the variables
%     if abs(x1m - x1mid) <= tolx1
%         fprintf('\n tolerance in x1 <= %7.3e',tolx1)
%         if abs(x2m - x2mid) <= tolx2
%             fprintf('\n tolerance in x2 <= %7.3e',tolx2)
%             break;
%             if abs(x3m - x3mid) <= tolx3
%                 fprintf('\n tolerance in x3 <= %7.3e',tolx2)
%                 break;
%             end
%         end
%     end
    %%  the tolerance on the variables was not effective

    % initialize the values for the next level.
    x1mid = x1m;
    x2mid = x2m;
    x3mid = x3m;
    delx1 = 0.5*delx1;
    delx2 = 0.5*delx2;
    delx3 = 0.5*delx3;

    x1L = x1mid - delx1;   x1U = x1mid + delx1;
    x2L = x2mid - delx2;   x2U = x2mid + delx2;
    x3L = x3mid - delx3;   x3U = x3mid + delx3;
end



totaltime = cputime - starttime;
fprintf('\nTotal time (s)= %7.4f \n\n',totaltime)


