Code covered by the BSD License  

Highlights from
New Approaches to Constrained Optimization in MATLAB

image thumbnail
from New Approaches to Constrained Optimization in MATLAB by Rakesh Kumar
M-files accompanying the webinar held on November 05, 2005

GAdemo_NO_GUI_Version.m
% The original demo used in the webinar demonstrated the graphical user
% interface, GATOOL in an interactive fashion. This demo does not use the
% GUI and runs the optimization problem using command line only. You may
% want to look at the  recorded webinar (http://www.mathworks.com/products/gads/)
% and 'GAdemo_GUI_Version.m' as an alternate way to run the demo.

%% 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('PlotFcn',@gaplotbestf, ...
    'MutationFcn',@mutationadaptfeasible, ...
    'Display','iter');
% Run the GA solver
[Xga,Fga] = ga(Objfcn,2,[],[],[],[],lb,ub,[],options);

%% Plot the final point from GA
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. Also, change
% the 'Generations' to 20 in the 'Stopping criteria' panel. 
options = gaoptimset(options,'Generations',20, ...
    'HybridFcn',{@fmincon,optimset('Display','iter')});
% Run the GA-Hybrid solver
[Xhybrid,Fhybrid] = ga(Objfcn,2,[],[],[],[],lb,ub,[],options);
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 use PATTERNSEARCH as a hybrid function to the GA solver. 
options = gaoptimset(options,'HybridFcn',{@patternsearch,psoptimset('Display','iter')});
% Run the GA-Hybrid solver
[Xhybrid,Fhybrid] = ga(Objfcn,2,[],[],[],[],lb,ub,[],options);
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)]);

Contact us at files@mathworks.com