www.gusucode.com > globaloptim 案例源码程序 matlab代码 > globaloptim/stochasticOpt.m
%% Optimization of Stochastic Objective Function % This example shows how to find a minimum of a stochastic objective % function using |patternsearch|. It also shows how Optimization % Toolbox(TM) solvers are not suitable for this type of problem. The % example uses a simple 2-dimensional objective function that is then % perturbed by noise. %% Initialization X0 = [2.5 -2.5]; % Starting point. LB = [-5 -5]; % Lower bound UB = [5 5]; % Upper bound range = [LB(1) UB(1); LB(2) UB(2)]; Objfcn = @smoothFcn; % Handle to the objective function. % Plot the smooth objective function fig = figure('Color','w'); showSmoothFcn(Objfcn,range); hold on; title('Smooth objective function'); ph = []; ph(1) = plot3(X0(1),X0(2),Objfcn(X0)+30,'or','MarkerSize',10,'MarkerFaceColor','r'); hold off; ax = gca; ax.CameraPosition = [-31.0391 -85.2792 -281.4265]; ax.CameraTarget = [0 0 -50]; ax.CameraViewAngle = 6.7937; % Add legend information legendLabels = {'Start point'}; lh = legend(ph,legendLabels,'Location','SouthEast'); lp = lh.Position; lh.Position = [1-lp(3)-0.005 0.005 lp(3) lp(4)]; %% Run |fmincon| on a Smooth Objective Function % The objective function is smooth (twice continuously differentiable). % Solve the optimization problem using the Optimization Toolbox |fmincon| % solver. |fmincon| finds a constrained minimum of a function of several % variables. This function has a unique minimum at the point |x* = [-5,-5]| % where it has a value |f(x*) = -250|. % % Set options to return iterative display. options = optimoptions(@fmincon,'Algorithm','interior-point','Display','iter'); [Xop,Fop] = fmincon(Objfcn,X0,[],[],[],[],LB,UB,[],options) figure(fig); hold on; %% % Plot the final point ph(2) = plot3(Xop(1),Xop(2),Fop,'dm','MarkerSize',10,'MarkerFaceColor','m'); % Add a legend to plot legendLabels = [legendLabels, '|fmincon| solution']; lh = legend(ph,legendLabels,'Location','SouthEast'); lp = lh.Position; lh.Position = [1-lp(3)-0.005 0.005 lp(3) lp(4)]; hold off; %% Stochastic Objective Function % Now perturb the objective function by adding random noise. rng(0,'twister') % Reset the global random number generator peaknoise = 4.5; Objfcn = @(x) smoothFcn(x,peaknoise); % Handle to the objective function. % Plot the objective function (non-smooth) fig = figure('Color','w'); showSmoothFcn(Objfcn,range); title('Stochastic objective function') ax = gca; ax.CameraPosition = [-31.0391 -85.2792 -281.4265]; ax.CameraTarget = [0 0 -50]; ax.CameraViewAngle = 6.7937; %% Run |fmincon| on a Stochastic Objective Function % The perturbed objective function is stochastic and not smooth. |fmincon| % is a general constrained optimization solver which finds a local minimum % using derivatives of the objective function. If you do not provide the % first derivatives of the objective function, |fmincon| uses finite % differences to approximate the derivatives. In this example, the % objective function is random, so finite difference estimates derivatives % hence can be unreliable. |fmincon| can potentially stop at a point that % is not a minimum. This may happen because the optimal conditions seems to % be satisfied at the final point because of noise, or |fmincon| could not % make further progress. [Xop,Fop] = fmincon(Objfcn,X0,[],[],[],[],LB,UB,[],options) figure(fig); hold on; ph = []; ph(1) = plot3(X0(1),X0(2),Objfcn(X0)+30,'or','MarkerSize',10,'MarkerFaceColor','r'); ph(2) = plot3(Xop(1),Xop(2),Fop,'dm','MarkerSize',10,'MarkerFaceColor','m'); % Add legend to plot legendLabels = {'Start point','|fmincon| solution'}; lh = legend(ph,legendLabels,'Location','SouthEast'); lp = lh.Position; lh.Position = [1-lp(3)-0.005 0.005 lp(3) lp(4)]; hold off; %% Run |patternsearch| % Now minimize the stochastic objective function using the Global % Optimization Toolbox |patternsearch| solver. Pattern search optimization % techniques are a class of direct search methods for optimization. A % pattern search algorithm does not use derivatives of the objective % function to find an optimal point. PSoptions = optimoptions(@patternsearch,'Display','iter'); [Xps,Fps] = patternsearch(Objfcn,X0,[],[],[],[],LB,UB,PSoptions) figure(fig); hold on; ph(3) = plot3(Xps(1),Xps(2),Fps,'dc','MarkerSize',10,'MarkerFaceColor','c'); % Add legend to plot legendLabels = [legendLabels, 'Pattern Search solution']; lh = legend(ph,legendLabels,'Location','SouthEast'); lp = lh.Position; lh.Position = [1-lp(3)-0.005 0.005 lp(3) lp(4)]; hold off %% % Pattern search is not as strongly affected by random noise in the % objective function. Pattern search requires only function values and not % the derivatives, hence noise (of some uniform kind) may not affect it. % However, pattern search requires more function evaluation to find the % true minimum than derivative based algorithms, a cost for not using the % derivatives.