% The original demo used in the webinar demonstrated the graphical user
% interface, GATOOL in an interactive fashion. You mant want to look at the
% recorded webinar (http://www.mathworks.com/products/gads/) and run this
% file along. An alternate version of the demo is also provided which does
% not use the GUI.
%% Initialization
format compact
% Reset the state of random number generators
rand('state',0);
randn('state',0);
% Range used to plot the objective function
range = [-5 5;-5 5];
% Starting point used by FMINCON
X0 = [-3.5 -3.5];
% Handle to the objective function
Objfcn = @nonSmoothFcn;
% Lower and upper bounds on decision variables
lb = [-5 -5];
ub = [5 5];
%% Plot the non-smooth function and the start point
showNonSmoothFcn(Objfcn,range);
title('Non-Smooth Objective Function','FontSize',16);
set(gca,'CameraPosition',[68.5203 48.5906 96.0540]);
set(gca,'CameraTarget',[-1.1189 -0.2845 20.1599])
set(gca,'CameraViewAngle',5.8952)
% Plot of the starting point (used by the FMINCON solver)
plot3(X0(1),X0(2),feval(Objfcn,X0)+3,'or','MarkerSize',10,'MarkerFaceColor','b');
fig = gcf;
% Create textarrow
annotation1 = annotation(...
fig,'textarrow',...
[0.6821 0.4375],[0.9333 0.8833],...
'String',{'Start point'},'FontSize',14);
%% Optimization using FMINCON (Optimization Toolbox)
% Set options for display and indicate to use medium-scale solver.
options = optimset('Display','iter','LargeScale','off');
[Xfmin,Ffmin] = fmincon(Objfcn,X0,[],[],[],[],lb,ub,[],options)
%% Plot the final point from FMINCON
figure(fig);
hold on;
plot3(Xfmin(1),Xfmin(2),Ffmin+3,'dk','MarkerSize',10,'MarkerFaceColor','k');
hold off;
% Create textarrow
annotation1 = annotation(...
fig,'textarrow',...
[0.5769 0.643],[0.5762 0.6714],...
'String',{'FMINCON Solution'},...
'FontSize',14);
%% Optimization using the Genetic Algorithm
% We are using the new mutation function 'mutationadaptfeasible' needed for
% bound constrained problem.
options = gaoptimset('MutationFcn',@mutationadaptfeasible);
% You may start the Genetic Algorithm Tool with options structure as an
% input argument.
gatool(options);
%% Plot the final point from GA
% At this point you must press the 'Start' button to run the solver and
% then export the results to MATLAB workspace. The variables garesults
% should be exported to the workspace before running this cell.
Xga = garesults.x;
Fga = garesults.fval;
figure(fig)
hold on;
plot3(Xga(1),Xga(2),Fga+3,'vm','MarkerSize',10,'MarkerFaceColor','m');
hold off;
fig = gcf;
annotation1 = annotation(...
fig,'textarrow',...
[0.7643 0.6928],[0.2452 0.1119],...
'String',{'GA Solution'},...
'FontSize',14);
%% Using FMINCON hybrid function
% Run GA-Hybrid
% We now add FMINCON as a hybrid function to the GA solver. You may want to
% select FMINCON under the 'Hybrid function' drop-down menu. Also, change
% the 'Generations' to 20 in the 'Stopping criteria' panel.
% At this point you must press the 'Start' button to run the solver and
% then export the results to MATLAB workspace. The variables garesults1
% should be exported to the workspace before running this cell.
Xhybrid = garesults1.x;
Fhybrid = garesults1.fval;
figure(fig);
hold on;
plot3(Xhybrid(1),Xhybrid(2),Fhybrid+3,'^c','MarkerSize',10,'MarkerFaceColor','c');
hold off;
%% Is the answer from fmincon hybrid approach different?
disp(['The second order norm of |Xga - Xhb| is ', num2str(norm(Xga-Xhybrid))]);
disp(['The difference in function values Fga and Fhb is ', num2str(Fga - Fhybrid)]);
%% Using PATTERNSEARCH hybrid function
% Run GA-Hybrid
% We now add PATTERNSEARCH as a hybrid function to the GA solver. You may want to
% select PATTERNSEARCH under the 'Hybrid function' drop-down menu.
% At this point you must press the 'Start' button to run the solver and
% then export the results to MATLAB workspace. The variables garesults2
% should be exported to the workspace before running this cell.
Xhybrid = garesults2.x;
Fhybrid = garesults2.fval;
figure(fig);
hold on;
plot3(Xhybrid(1),Xhybrid(2),Fhybrid+3,'^c','MarkerSize',10,'MarkerFaceColor','y');
hold off;
%% Is the answer from pattern search hybrid approach different?
disp(['The second order norm of |Xga - Xhb| is ', num2str(norm(Xga-Xhybrid))]);
disp(['The difference in function values Fga and Fhb is ', num2str(Fga - Fhybrid)]);