1 | %% Constrained optimization with FMINSEARCH |
---|
2 | |
---|
3 | function testoptimize |
---|
4 | |
---|
5 | |
---|
6 | %% Introduction |
---|
7 | |
---|
8 | %% Usage |
---|
9 | |
---|
10 | %% |
---|
11 | % first, define a test function: |
---|
12 | clc, rosen = @(x) (1-x(1))^2 + 105*(x(2)-x(1)^2)^2; |
---|
13 | |
---|
14 | %% |
---|
15 | % this is the classical Rosenbr\"uck function, which has a global minimum |
---|
16 | % at _f(x)_ = _f_([1, 1]) = 0. The function is relatively hard to optimize, |
---|
17 | % because that minimum is located in a long narrow ``valley'': |
---|
18 | k = 0; range = -5:0.1:5; |
---|
19 | z = zeros(101); |
---|
20 | for i = range |
---|
21 | m = 0; k = k + 1; |
---|
22 | for j = range |
---|
23 | m = m + 1; |
---|
24 | z(k, m) = rosen([i, j]); |
---|
25 | end |
---|
26 | end |
---|
27 | [y, x] = meshgrid(range, range); |
---|
28 | surf(x, y, z, 'linestyle', 'none'), view(-150, 30), axis tight |
---|
29 | |
---|
30 | %% |
---|
31 | % Optimizing the fully unconstrained problem with OPTIMIZE indeed finds |
---|
32 | % the global minimum: |
---|
33 | % warning work only with optimization toolbox !!! |
---|
34 | |
---|
35 | solution = optimize(rosen, [3 3]) |
---|
36 | |
---|
37 | %% |
---|
38 | % Imposing a lower bound on the variables gives |
---|
39 | [solution, fval] = optimize(rosen, [3 3], [2 2], []) |
---|
40 | |
---|
41 | %% |
---|
42 | % in the figure, this looks like |
---|
43 | zz = z; zz(x > 2 & y > 2) = inf; |
---|
44 | ZZ = z; ZZ(x < 2) = inf; ZZ(y < 2) = inf; |
---|
45 | figure, hold on |
---|
46 | surf(x, y, zz, 'linestyle', 'none', 'FaceAlpha', 0.2) |
---|
47 | surf(x, y, ZZ, 'linestyle', 'none') |
---|
48 | view(-150, 30), grid on, axis tight |
---|
49 | plot3(solution(1), solution(2), fval+1e3, 'g.', 'MarkerSize', 20) |
---|
50 | |
---|
51 | %% |
---|
52 | % Similarly, imposing an upper bound yields |
---|
53 | solution = optimize(rosen, [3 3], [], [0.5 0.5]) |
---|
54 | |
---|
55 | zz = z; zz(x < 0.5 & y < 0.5) = inf; |
---|
56 | ZZ = z; ZZ(x > 0.5) = inf; ZZ(y > 0.5) = inf; |
---|
57 | figure, hold on |
---|
58 | surf(x, y, zz, 'linestyle', 'none', 'FaceAlpha', 0.2) |
---|
59 | surf(x, y, ZZ, 'linestyle', 'none') |
---|
60 | view(150, 30), grid on, axis tight |
---|
61 | plot3(solution(1), solution(2), fval+1e3, 'g.', 'MarkerSize', 20) |
---|
62 | |
---|
63 | %% |
---|
64 | % Optimize with _x_(2) fixed at 3. In this case, OPTIMIZE simply |
---|
65 | % removes the variable before FMINSEARCH sees it, essentially |
---|
66 | % reducing the dimensionality of the problem. This is particularly |
---|
67 | % useful when the number of dimensions _N_ becomes large. |
---|
68 | optimize(rosen, [3 3], [-inf 3], [inf 3]) |
---|
69 | |
---|
70 | %% |
---|
71 | % Also general nonlinear constraints can be used. A simple example: |
---|
72 | % |
---|
73 | % nonlinear inequality: |
---|
74 | % |
---|
75 | % $$\sqrt{x_1^2 + x_2^2} \leq 1$$ |
---|
76 | % |
---|
77 | % nonlinear equality : |
---|
78 | % |
---|
79 | % $$x_1^2 + x_2^3 = 0.5$$ |
---|
80 | |
---|
81 | options = optimset('TolFun', 1e-8, 'TolX', 1e-8); |
---|
82 | [sol, fval, exitflag, output] = optimize(rosen, [3 -3], [], [], [], ... |
---|
83 | [], [], [], @nonlcon, [], options); |
---|
84 | |
---|
85 | %% |
---|
86 | % Note that |nonlcon| is a subfunction, listed below. In a figure, this |
---|
87 | % looks like |
---|
88 | zz = z; zz(sqrt(x.^2 + y.^2) <= 1) = inf; |
---|
89 | ZZ = z; ZZ(sqrt(x.^2 + y.^2) >= 1.2) = inf; |
---|
90 | zZ = z; zZ(x.^2 + y.^3 >= 0.5 + 0.1) = inf; |
---|
91 | zZ(x.^2 + y.^3 <= 0.5 - 0.1) = inf; |
---|
92 | figure, hold on |
---|
93 | surf(x, y, zz, 'linestyle', 'none', 'FaceAlpha', 0.2) |
---|
94 | surf(x, y, ZZ, 'linestyle', 'none') |
---|
95 | xX = x(isfinite(zZ)); xX = xX(:); |
---|
96 | yY = y(isfinite(zZ)); xX = xX(:); |
---|
97 | zZ = zZ(isfinite(zZ)); zZ = zZ(:); |
---|
98 | [xX, inds] = sort(xX); yY = yY(inds); zZ = zZ(inds); |
---|
99 | xyz = [xX, yY, zZ]; |
---|
100 | for i = 1:length(xX)-1 % line-command is *somewhat* inconvenient... |
---|
101 | l = line( [xyz(i, 1); xyz(i+1, 1)],[xyz(i, 2); xyz(i+1, 2)], [xyz(i, 3); xyz(i+1, 3)]); |
---|
102 | set(l, 'color', 'r', 'linewidth', 2) |
---|
103 | end |
---|
104 | view(150, 50), grid on, axis tight |
---|
105 | plot3(sol(1), sol(2), fval+1e3, 'g.', 'MarkerSize', 20) |
---|
106 | |
---|
107 | %% |
---|
108 | % Note that the output structure contains a field ``constrviolation'': |
---|
109 | output |
---|
110 | |
---|
111 | %% |
---|
112 | % The contents of which shows that all constraints have been satisfied: |
---|
113 | output.constrviolation |
---|
114 | |
---|
115 | |
---|
116 | end |
---|
117 | |
---|
118 | %% |
---|
119 | function [c, ceq] = nonlcon(x) |
---|
120 | c = norm(x) - 1; |
---|
121 | ceq = x(1)^2 + x(2)^3 - 0.5; |
---|
122 | end |
---|
123 | |
---|