THMMY's "Optimization Techniques" course assignments.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Script_4_LevMar.m 5.1 KiB

3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
3 주 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/LevMar_Iter_o_gamma_" + point + ".png");
  39. [~, j] = min(k);
  40. gamma_fixed_step = n(j);
  41. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  42. 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));
  43. plotPointsOverContour(x_fixed, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt $\gamma$ = " + gamma_fixed_step, "figures/LevMar_fixed_" + point + ".png");
  44. [x_minimized, f_minimized, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'minimized');
  45. fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_minimized(end, :), f_minimized(end));
  46. plotPointsOverContour(x_minimized, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt minimized $f(x_k + \gamma_kd_k)$", "figures/LevMar_minimized_" + point + ".png");
  47. % Armijo Rule
  48. % Methods tuning
  49. amijo_beta = 0.4; % typical range: [0.1, 0.8]
  50. amijo_sigma = 0.1; % typical range: [0.01, 0.3]
  51. [x_armijo, f_armijo, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'armijo');
  52. 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));
  53. plotPointsOverContour(x_armijo, fun, [-3, 0], [-2, 2], 100, point_str + ": Levenberg-Marquardt Armijo method", "figures/LevMar_armijo_" + point + ".png");
  54. disp(' ');
  55. % Compare methods
  56. plotConvCompare(x_fixed, "Fixed", x_minimized, "Minimized", x_armijo, "Armijo", Xmin, "Convergence compare", "figures/LevMar_compare_" + point + ".png");
  57. % Point x0 = (1, -1)
  58. % =========================================================================
  59. point = 3;
  60. x0 = [1, -1];
  61. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  62. f = fun(-1, 1);
  63. gf = grad_fun(x0(1), x0(2));
  64. hf = hessian_fun(x0(1), x0(2));
  65. ev = eig(hf);
  66. 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);
  67. % Find the best fixed gamma
  68. k = zeros(100, 1);
  69. j = 1;
  70. n = linspace(0.1, 1.5, 100);
  71. for g = n
  72. gamma_fixed_step = g;
  73. [x, f, k(j)] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  74. 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)
  75. k(j) = 300;
  76. end
  77. j = j + 1;
  78. end
  79. [~, j] = min(k);
  80. gamma_fixed_step = n(j);
  81. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'fixed');
  82. 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));
  83. plotPointsOverContour(x_fixed, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt $\gamma$ = " + gamma_fixed_step, "figures/LevMar_fixed_" + point + ".png");
  84. [x_fixed, f_fixed, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'minimized');
  85. 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));
  86. 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");
  87. % Armijo Rule
  88. % Methods tuning
  89. amijo_beta = 0.4; % typical range: [0.1, 0.8]
  90. amijo_sigma = 0.1; % typical range: [0.01, 0.3]
  91. [x_armijo, f_armijo, kk] = method_lev_mar(fun, grad_fun, hessian_fun, 0.3, x0, tol, max_iter, 'armijo');
  92. 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));
  93. plotPointsOverContour(x_armijo, fun, [-3, 2], [-2, 2], 100, point_str + ": Levenberg-Marquardt Armijo method", "figures/LevMar_armijo_" + point + ".png");
  94. disp(' ');