Advertisement
elisim

ass4_q2

Jul 7th, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.55 KB | None | 0 0
  1. # Eli Simhayev - 2017
  2. # Question 2
  3.  
  4. # ===== Task C =====
  5.  
  6. mu = 20; # Global variable for penalty function.  
  7.  
  8. """
  9. Backtracking line search using the Armijo condition.
  10. (source: lecture notes, page 108)
  11. """
  12. function linesearch(f::Function,x,d,gk,alpha0=1.0,beta=0.5,c=1e-4)
  13.     alphaj = alpha0;
  14.     for jj = 1:10
  15.         x_temp = x + alphaj*d;
  16.         if f(x_temp) <= f(x) + alphaj*c*dot(d,gk)
  17.             break;
  18.         else
  19.             alphaj = alphaj*beta;
  20.         end
  21.     end
  22.     return alphaj;
  23. end
  24.  
  25. """  
  26. Steepest Descent:
  27.    x⁽ᵏ⁺¹⁾ = x⁽ᵏ⁾ - α⁽ᵏ⁾*∇f(x⁽ᵏ⁾)
  28. """
  29. function sd(f::Function, grad_f::Function, x_SD, maxIter)
  30.     grad = grad_f(x_SD); # ∇f(x⁽⁰⁾)
  31.     alpha_SD = linesearch(f,x_SD,-grad,grad); # α⁽⁰⁾
  32.     for k=1:maxIter
  33.         x_SD = x_SD - alpha_SD*grad;
  34.         grad = grad_f(x_SD); # ∇f(x⁽ᵏ⁾)
  35.         alpha_SD = linesearch(f,x_SD,-grad,grad); # α⁽ᵏ⁾
  36.     end
  37.  
  38.     return x_SD;
  39. end
  40.  
  41. ### main
  42. f = (x) -> (x[1]+x[2])^2 - 10(x[1]+x[2]);
  43. c1eq = (x) -> 3*x[1] + x[2] - 6;
  44. c2ieq = (x) -> x[1]^2+x[2]^2-5;
  45. c3ieq = (x) -> -x[1];
  46.  
  47. f_penalty = (x) -> f(x) + mu*(c1eq(x)^2 + max(0,c2ieq(x))^2 + max(0,c3ieq(x))^2);
  48. grad_f_penalty = (x) -> [2(x[1]+x[2])-10 + mu*(6(3*x[1]+x[2]-6)+max(0,4*x[1]*(x[1]^2+x[2]^2-5))+max(0,-2*x[1]));
  49.                         2(x[1]+x[2])-10 + mu*(2(3*x[1]+x[2]-6)+max(0,4*x[2]*(x[1]^2+x[2]^2-5)))];
  50.  
  51. x_SD = [1; 2]; # inital guess
  52. ans = sd(f_penalty, grad_f_penalty, x_SD, 100);
  53. println("ans = ", ans); # ans = [1.42599,1.72947]
  54.  
  55. # Note: mu = 20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement