THMMY's "Optimization Techniques" course assignments.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Script_4_LevMar.m 4.8 KiB

2 dni temu
2 dni temu
2 dni temu
2 dni temu
2 dni temu
2 dni temu
2 dni temu
2 dni temu
2 dni temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. % Define environment (functions, gradients etc...)
  2. GivenEnv
  3. % Define parameters
  4. max_iter = 300; % Maximum iterations
  5. tol = 1e-4; % Tolerance
  6. % Point x0 = (0, 0)
  7. % =========================================================================
  8. point = 1;
  9. x0 = [0, 0];
  10. f = fun(x0(1), x0(2));
  11. gf = grad_fun(x0(1), x0(2));
  12. hf = hessian_fun(x0(1), x0(2));
  13. ev = eig(hf);
  14. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can NOT use method\n', x0, f, gf, hf, ev);
  15. disp(' ');
  16. % Point x0 = (-1, 1)
  17. % =========================================================================
  18. point = 2;
  19. x0 = [-1, 1];
  20. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  21. f = fun(-1, 1);
  22. gf = grad_fun(x0(1), x0(2));
  23. hf = hessian_fun(x0(1), x0(2));
  24. ev = eig(hf);
  25. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can use method\n', x0, f, gf, hf, ev);
  26. % Find the best fixed gamma
  27. k = zeros(100, 1);
  28. j = 1;
  29. n = linspace(0.1, 1.5, 100);
  30. for g = n
  31. gamma_fixed_step = g;
  32. [x, f, k(j)] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  33. if ~(x(end, 1) < -1.57 && x(end, 1) > -1.59 && x(end, 2) < 0.01 && x(end,2) > -0.01 && f(end) < -0.8 && f(end) > -0.82)
  34. k(j) = 300;
  35. end
  36. j = j + 1;
  37. end
  38. [~, j] = min(k);
  39. gamma_fixed_step = n(j);
  40. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  41. fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
  42. plotPointsOverContour(x_fixed, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt $\gamma$ = " + gamma_fixed_step, "figures/LevMar_fixed_" + point + ".png");
  43. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'minimized');
  44. fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
  45. plotPointsOverContour(x_fixed, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt minimized $f(x_k + \gamma_kd_k)$", "figures/LevMar_minimized_" + point + ".png");
  46. % Armijo Rule
  47. % Methods tuning
  48. amijo_beta = 0.4; % typical range: [0.1, 0.8]
  49. amijo_sigma = 0.1; % typical range: [0.01, 0.3]
  50. [x_armijo, f_armijo, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'armijo');
  51. fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
  52. plotPointsOverContour(x_armijo, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt Armijo method", "figures/StDes_armijo_" + point + ".png");
  53. disp(' ');
  54. % Point x0 = (1, -1)
  55. % =========================================================================
  56. point = 3;
  57. x0 = [1, -1];
  58. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  59. f = fun(-1, 1);
  60. gf = grad_fun(x0(1), x0(2));
  61. hf = hessian_fun(x0(1), x0(2));
  62. ev = eig(hf);
  63. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Eigenvalues= [%f, %f], Can use method\n', x0, f, gf, hf, ev);
  64. % Find the best fixed gamma
  65. k = zeros(100, 1);
  66. j = 1;
  67. n = linspace(0.1, 1.5, 100);
  68. for g = n
  69. gamma_fixed_step = g;
  70. [x, f, k(j)] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  71. if ~(x(end, 1) < -1.57 && x(end, 1) > -1.59 && x(end, 2) < 0.01 && x(end,2) > -0.01 && f(end) < -0.8 && f(end) > -0.82)
  72. k(j) = 300;
  73. end
  74. j = j + 1;
  75. end
  76. [~, j] = min(k);
  77. gamma_fixed_step = n(j);
  78. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  79. fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
  80. plotPointsOverContour(x_fixed, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt $\gamma$ = " + gamma_fixed_step, "figures/LevMar_fixed_" + point + ".png");
  81. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'minimized');
  82. fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
  83. plotPointsOverContour(x_fixed, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt minimized $f(x_k + \gamma_kd_k)$", "figures/LevMar_minimized_" + point + ".png");
  84. % Armijo Rule
  85. % Methods tuning
  86. amijo_beta = 0.4; % typical range: [0.1, 0.8]
  87. amijo_sigma = 0.1; % typical range: [0.01, 0.3]
  88. [x_armijo, f_armijo, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'armijo');
  89. fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
  90. plotPointsOverContour(x_armijo, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt Armijo method", "figures/StDes_armijo_" + point + ".png");
  91. disp(' ');