peaks Minimization with Simmulated Annealing
Copyright (c) 2010, The MathWorks, Inc. All rights reserved.
Contents
Objective Function
We wish find the minimum of the peaks function
clear all, close all, clc peaks
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2)

Nonlinear Constraint Function
Subject to a nonlinear constraint defined by a circular region of radius three around the origin
type circularConstraint
function [c,ceq] = circularConstraint(x) % Nonlinear constraint definition % Copyright (c) 2010, The MathWorks, Inc. % All rights reserved. % Define nonlinear equality constraint (none) ceq = []; % Define nonlinear inequality constraint % circular region with radius 3: x1^2 + x^2 -3^2 <= 0 c = x(:,1).^2 + x(:,2).^2 - 9;
Define Optimization Problem
problem = createOptimProblem('fmincon',... 'objective',@(x) peaks(x(1),x(2)), ... 'nonlcon',@circularConstraint,... 'x0',[-1 -1],... 'lb',[-3 -3],... 'ub',[3 3],... 'options',optimset('OutputFcn',... @peaksPlotIterates))
problem = objective: @(x)peaks(x(1),x(2)) x0: [-1 -1] Aineq: [] bineq: [] Aeq: [] beq: [] lb: [-3 -3] ub: [3 3] nonlcon: @circularConstraint solver: 'fmincon' options: [1x1 struct]
Run the solver fmincon from the inital point
We can see the solution is not the global minimum
[x,f] = fmincon(problem)
x = -1.3473 0.2045 f = -3.0498

Use Simmulated Annealing to Find the Global Minimum
Solve the problem using simmulated annealing. Note that simmulated annealing does not support nonlinear so we need to account for this in the objective function.
problem.solver = 'simulannealbnd'; problem.objective = @(x) peaks(x(1),x(2)) + (x(1)^2 + x(2)^2 - 9); problem.options = saoptimset('OutputFcn',@peaksPlotIterates,... 'Display','iter',... 'InitialTemperature',10,... 'MaxIter',300) [x,f] = simulannealbnd(problem) f = peaks(x(1),x(2))
problem = objective: @(x)peaks(x(1),x(2))+(x(1)^2+x(2)^2-9) x0: [-1 -1] Aineq: [] bineq: [] Aeq: [] beq: [] lb: [-3 -3] ub: [3 3] nonlcon: @circularConstraint solver: 'simulannealbnd' options: [1x1 struct] Best Current Mean Iteration f-count f(x) f(x) temperature 0 1 -5.14411 -5.14411 10 10 11 -6.65766 -4.78895 5.688 20 21 -7.71946 -7.71946 3.40562 30 31 -8.74141 -5.43077 2.03907 40 41 -10.2347 -9.20311 1.22087 50 51 -11.1116 -11.1116 0.730977 60 61 -12.5608 -12.5608 0.437663 70 71 -12.7595 -12.7595 0.262045 80 81 -12.8431 -12.6448 0.156896 90 91 -13.0077 -13.0077 0.0939395 100 101 -13.0233 -13.0233 0.056245 110 111 -13.0233 -12.8797 0.033676 120 121 -13.0233 -12.9952 0.0201631 130 131 -13.0233 -12.9995 0.0120724 140 141 -13.0233 -12.9924 0.00722817 150 151 -13.0233 -13.0139 0.00432777 160 161 -13.0233 -13.0182 0.0025912 170 171 -13.0233 -13.0212 0.00155145 180 181 -13.0233 -13.0222 0.000928908 190 191 -13.0233 -13.0218 0.000556171 200 201 -13.0233 -13.0219 0.000333 210 211 -13.0233 -13.0221 0.00019938 * 214 217 -13.0233 -13.0222 5.70294 220 223 -13.0233 -1.22539 4.19218 230 233 -13.0233 -9.49878 2.51002 240 243 -13.0233 -5.96102 1.50284 250 253 -13.0233 -8.47664 0.899805 260 263 -13.0233 -12.9689 0.538747 270 273 -13.0233 -12.9689 0.322568 280 283 -13.0233 -12.9689 0.193133 290 293 -13.0233 -12.9689 0.115636 300 303 -13.0233 -13.0101 0.0692355 Maximum number of iterations exceeded: increase options.MaxIter. x = 0.2239 -1.5161 f = -13.0233 f = -6.3720
