%% Run |GlobalSearch| on 1-D Problem
% Consider a function with several local minima.
%
%% 
format compact
%%
fun = @(x) x.^2 + 4*sin(5*x);
fplot(fun,[-5,5])
%% 
% To search for the global minimum, run |GlobalSearch| using the |fmincon| 
% |'sqp'| algorithm.

rng default % For reproducibility
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',...
    fun,'x0',3,'lb',-5,'ub',5,'options',opts);
%        x0=3,  lb=-5,  ub=5
%%
gs = GlobalSearch;
[x,f] = run(gs,problem)
%% Timings
%
% 1) Use parallel off
opts.UseParallel=0
Tstart=tic;
[x,f] = run(gs,problem);
Tserial=toc(Tstart)
%%
parpool
%%
% 2) Use parallel on
opts.UseParallel=1
Tstart=tic;
[x,f] = run(gs,problem);
Tparallel=toc(Tstart)
%%
Tserial
Tparallel
%{
Tserial =
    0.3525
Tparallel =
    0.2614
%}

