THMMY's "Optimization Techniques" course assignments.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LevMar_script.m 2.1 KiB

18 tuntia sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. % Define environment (functions, gradients etc...)
  2. GivenEnv
  3. % Define parameters
  4. max_iter = 300; % Maximum iterations
  5. tol = 1e-4; % Tolerance
  6. % Methods tuning
  7. amijo_beta = 0.5; % Armijo reduction factor
  8. amijo_sigma = 0.1; % Armijo condition constant
  9. m = 0.01;
  10. % Point x0 = (0, 0)
  11. % x0 = [0, 0];
  12. % Point x0 = (0, 0)
  13. x0s = [0, 0; -1, 1 ; 1, -1]; % Initial points
  14. for i = 1:size(x0s, 1)
  15. x0 = x0s(i, :);
  16. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  17. % Find the best fixed gamma
  18. k = zeros(100, 1);
  19. j = 1;
  20. n = linspace(0.1, 1.5, 100);
  21. for g = n
  22. gamma_fixed_step = g;
  23. [~, ~, k(j)] = lev_mar(fun, grad_fun, hessian_fun, m, x0, tol, max_iter, 'fixed');
  24. j = j + 1;
  25. end
  26. plotIterationsOverGamma(n, k, "Iteration for different $\gamma$ values", "LevMar_Iter_o_gamma_" + i + ".png");
  27. [~, j] = min(k);
  28. gamma_fixed_step = n(j);
  29. [x_fixed, f_fixed, kk] = lev_mar(fun, grad_fun, hessian_fun, m, x0, tol, max_iter, 'fixed');
  30. 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));
  31. plotPointsOverContour(x_fixed, fun, [-2, 2], [-3, 3], 100, point_str + ": LevMar $\gamma$ = " + gamma_fixed_step, "LevMar_fixed_" + i + ".png");
  32. % Minimized f
  33. [x_minimized, f_minimized, kk] = lev_mar(fun, grad_fun, hessian_fun, m, x0, tol, max_iter, 'minimized');
  34. 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));
  35. plotPointsOverContour(x_minimized, fun, [-2, 2], [-3, 3], 100, point_str + ": LevMar minimized $f(x_k + \gamma_kd_k)$", "LevMar_minimized_" + i + ".png");
  36. % Armijo Rule
  37. [x_armijo, f_armijo, kk] = lev_mar(fun, grad_fun, hessian_fun, m, x0, tol, max_iter, 'armijo');
  38. 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));
  39. plotPointsOverContour(x_armijo, fun, [-2, 2], [-3, 3], 100, point_str + ": LevMar Armijo method", "LevMar_armijo_" + i + ".png");
  40. end