THMMY's "Optimization Techniques" course assignments.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

34 lines
1.1 KiB

  1. function [gamma] = gamma_armijo(f, grad_f, dk, xk)
  2. % Calculates the best step based on amijo method
  3. %
  4. % f(xk​+ γk*dk) ≤ f(xk) + σ * γk * dk^T * ∇f(xk)
  5. % γk = β*γk_0
  6. %
  7. % f: Objective function
  8. % grad_fun: Gradient function of f
  9. % dk: Current value of selected direction -∇f or -inv{H}*∇f or -inv{H + lI}*∇f
  10. % xk: Current point (x,y)
  11. % beta: beta factor in [0.1, 0.5]
  12. % signam: sigma factor in (0, 0.1]
  13. global amijo_beta
  14. global amijo_sigma
  15. gf = grad_f(xk(1), xk(2));
  16. gamma = 1; % Start with a step size of 1
  17. % Perform Armijo line search
  18. while f(xk(1) + gamma * dk(1), xk(2) + gamma * dk(2)) > ...
  19. f(xk(1), xk(2)) + amijo_sigma * gamma * dk' * gf
  20. %while f(xk(1) + gamma * dk(1), xk(2) + gamma * dk(2)) > ...
  21. % f(xk(1), xk(2)) + amijo_sigma * gamma * norm(dk)^2
  22. gamma = amijo_beta * gamma; % Reduce step size
  23. if gamma < 1e-12 % Safeguard to prevent infinite reduction
  24. warning('Armijo step size became too small.');
  25. break;
  26. end
  27. end
  28. end