1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| clc; clear all;
global Iter_Num n x r r1 r2 alpha beta v s y a ans_x ans_y s_x s_y xx vv ss yy; Iter_Num = 1000; n = 5; x = zeros(1, n); r = zeros(1, n); r1 = [1, 4, 9, 4, 8]; r2 = [6, 8, 8, 2, 3]; alpha = 0.01; beta = 0.01; v = diag(ones(1, n)); s = phi(x); y = zeros(1, n); a = [ 1/2, 0, 1/2, 0, 0; 1/3, 1/3, 0, 0, 1/3; 0, 1/2, 1/2, 0, 0; 0, 1/2, 0, 1/2, 0; 0, 0, 0, 1/2, 1/2 ]; ans_x = zeros(1, 1); ans_y = zeros(1, 1); s_x = zeros(1, 1); s_y = zeros(1, 1); xx = x; vv = v; ss = s; yy = y;
solve(); fprintf('经过%d轮迭代, 最终F(x)收敛到: %f\n', Iter_Num, cal(Iter_Num)); draw_1(); draw_2();
function draw_2() global Iter_Num; figure (2); subplot(1, 3, 1); draw_2_sub(1); subplot(1, 3, 2); draw_2_sub(Iter_Num / 2); subplot(1, 3, 3); draw_2_sub(Iter_Num); end
function draw_2_sub(iter_num) hold on; global n r1 r2 ans_x ans_y; axis([0, 10, 0, 10]); plot(5, 5, '*'); for i = 1 : n plot(ans_x(i, iter_num), ans_y(i, iter_num), '+', 'Color', 'blue'); end plot([r1(1), r1(2), r1(3), r1(5), r1(4), r1(1)], [r2(1), r2(2), r2(3), r2(5), r2(4), r2(1)], 'Marker','x', 'Color', 'red'); sum_x = 0; sum_y = 0; for i = 1 : n sum_x = sum_x + ans_x(i, iter_num); sum_y = sum_y + ans_y(i, iter_num); end plot(sum_x / n, sum_y / n, 'o'); xlabel('$x_1$', 'Interpreter', 'latex'); ylabel('$x_2$', 'Interpreter', 'latex'); end
function draw_1() figure (1); global Iter_Num; F_best = 18.874999999999645; error = []; for i = 1 : (Iter_Num - 1) error = [error, abs(cal(i + 1) - F_best) / abs(cal(i) - F_best)]; end plot(error); xlabel('Iteration k'); ylabel('$\frac{f_{k+1} - f^*}{f_k - f^*}$', 'Interpreter', 'latex', 'FontSize', 20); end
function res = cal(iter_num) global n ans_x r1 ans_y r2 s_x s_y; sum = 0; for i = 1 : n sum = sum + 0.5 * ((ans_x(i, iter_num) - r1(i)) ^ 2 + (ans_y(i, iter_num) - r2(i)) ^ 2)... + 0.5 * ((ans_x(i, iter_num) - 5) ^ 2 + (ans_y(i, iter_num) - 5) ^ 2)... + ((s_x(i, iter_num) - 5) ^ 2 + (s_y(i, iter_num) - 5) ^ 2); end res = sum; end
function solve() global Iter_Num n xx ss ans_x ans_y s_x s_y; init(1); for T = 1 : Iter_Num for i = 1 : n upd_x(i); ans_x(i, T) = xx(i); upd_v(i); upd_s(i); s_x(i, T) = ss(i); upd_y(i); end backup(); end init(2); for T = 1 : Iter_Num for i = 1 : n upd_x(i); ans_y(i, T) = xx(i); upd_v(i); upd_s(i); s_y(i, T) = ss(i); upd_y(i); end backup(); end end
function upd_y(i) global a ss yy y beta n; sum = 0; for j = 1 : n sum = sum + a(i, j) * ss(j); end yy(i) = y(i) + beta * (ss(i) - sum); end
function upd_s(i) global a s ss x xx vv n v; sum = 0; for j = 1 : n sum = sum + a(i, j) * s(j); end ss(i) = sum + phi(xx(i)) / vv(i, i) - phi(x(i)) / v(i, i); end
function upd_v(i) global n a v vv; sum = zeros(1, n); for j = 1 : n sum = sum + a(i, j) * v(j, :); end vv(i, :) = sum; end
function upd_x(i) global xx x alpha r n y s v; xx(i) = x(i) - alpha * ((x(i) - r(i)) + (x(i) - 5) + (1 / n) * (y(i) + 2 * (s(i) - 5) / n / v(i, i))); end
function backup() global x v s y xx vv ss yy x = xx; v = vv; s = ss; y = yy; end
function init(op) global n x r v s y xx vv ss yy r1 r2 x = randi([1, 5], 1, n); if (op == 1) r = r1; else r = r2; end v = diag(ones(1, n)); s = phi(x); y = zeros(1, n); xx = x; vv = v; ss = s; yy = y; end
function res = phi(x) global n; res = x / n; end
|