0% found this document useful (0 votes)
28 views4 pages

Assignment 2

Line search methods

Uploaded by

ed22b033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Assignment 2

Line search methods

Uploaded by

ed22b033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
5716124, 12222 PM ‘Assignment2ipynb - Colab fngort nunpy 25 np sof cate xk yes, 9) rad_t = npsarray({L(2*(r-2)*#3}02*(0-2)*(9*2)) 1, (C*((r-2)**2)*y + 24(249))1) he F mparray({[(42*-2))e(2My"*2)), (4PCH-2)"y)]}, [C$ECHR) YI, (BAC C42)"*2} 22) 10) pe erat aipha,nur = grad f.7 @ upnazden = prad.t-T @ hf @ eraef sipne"> -1*(31aha_nun/alona_cen) [Link] = np-aeray(( lx, (91D) ewval = old_val + alphap return em v3i[0,0], new vat{2,@] for 4 An eange(asan): initial, ylinitial ~ cale_ek yk(xinteial, y_inétial) PrineCO™CGAD = (ocinatianyy yo) = initial) xa = 2.euesson7ssi30scs, y_2 = o.aussavaceaaaces3 X02 = 2.0706057158856504, y_2 > -0,991142396295678 03 = 2iezenaseerseasoae, y°3 = -0,993051564613397 X06 = 2,ea00089002923398, _6 = -8,9999999007945276 oy = ‘ipython-input-1-554a62650a20>:9: funtinekarning: Anvalld value encountered tn divie Stoha = -°(alpha_run/alpho_en) Anzort nunpy 25 ne ingort [Link] as sit fron [Link] Anport mininize e0F FH 2) return @.5 + (x[e]t2 + 3 xan set grad_foe, 2) return [Link]((a(e]. @ ¢ x(2)]) ef steepest descent init, 3): we init Seeraier = Dd for _ in range(300) Garection = -prad tx, 2) fet = nininsze(lanba alpha: F(x + alpha * direction, a), 8) $verates. append) ef plot(ay x ante) X= np. Linspace(-28, 28, 40 y= npsLinepace(-38, 10, 490) X, v's psnesherid(s, 99 2s 4%, YI, a) ple. Figure(#igsize=(6, 6)) plt-contaur(e, ¥, 2, levelserp-logspacel®, 3, 18)) Eterates = nprarray(steepest_descert(x init, 2)) [Link](iterstes(:, 0], Aterates[:, 1}, 'r0-') pietstieces = 03)" pit-shoe() init = mparray(la, #1) avaiues = [403,27 G0 +3), santa) for a sn avalues plot(ay x anit) ‘ntps:ifoolab research [Link]/1S-p_2KKiz-BpS2uCw20Nd0_oTYLDMIMGHscrolfo-xTZQCXINIwPSprintMode=true wa 5716924, 12:22 PM ‘Assignment2ipynb - Colab 15. as 00. po -100 tao a = 0.07692307692307693 1. as 00. -100 15 50 00 50 ‘ntps:icolab research [Link]/1S-p_2KKvz-BpS2uCw20Nd0_oTYLDMIMGH#scrolfo-xFTZQCXINIWPSprintMode=true 214 5716924, 12222 PM ‘Assignment ipynb - Colab 15 -100 noort nunpy 25 mp fron [Link] anport Line search ingort [Link] as plt ‘moort panes 36 pa class optintzationtettods: ef _init_(Sel¢, 99): tod - 10 Seif.nistory_slgha = (Nene) self.history_eirection = fone] aot f(selt, 0) return (x[0]-2)°%8 + (x) ayeeaentayeea + (aext33)2 det gra #(sel*, 0) Geax = «(x[0}-2)83 + 20(x(0)-2)4L2)2 fray = 2°Gx[0}-2)"2254] + 2*C2(4)) return npsatray( (aoc, of6y]) ot hessian f(s0lt, ») tai = 234(4[0]-2)2 + 264f3]0"2 ffany = 46Ge(8]-2)°x03], rayne = 4¢Ge(8)-2)°x01) fray = 2¢Gaf0)-2)™02 + 2 return np. 2eray(( [efx stony), (Fane, 6f699))) det steepast_descent with exact Line_search(self, alpha-®.0l, nox ster xo elioe history © De-cony()] for — in range(nax iter): Bo ssetf-erad Oo) bipha = Line scarch(sel*.f, self.2r ens ola hastory-oppend(x. copy) return x, mpcerray(history) 0) TO) et newton_nethod- with exact_line_search(sel¢, alphaed.02, max stenste): wo wife ean iter) B = -mp-Linalg nv (se1f-nesstan_f(2))-2ot(selt.gred_#(0)) fiphe = Line search(selt-f, [Link]#, x, 2)10) exe ala p [Link](s) return x, mprarray (history) ot stoepast_ descent with packtracking(self, clete-3, max itersie) xe nelfe history = [setf.90] for _ in range(nax iter): B= -sert-grad ro) Sipe = 1.8 inile self. ¢(x + alpha * p) > self.f(0) + c& * alpha * [Link](selt grad f(9), 9): alpha "= 0.4 8 Bacetracking : recucing te value of alpha by nultiplying #t by a factor of 0.5 t4ll At satisfies the AMIIO ¢ wee aipna history-appena(s) return x, apeseray(Mstory) “xe = npearray{(2.5, 4.35)) opt_nethads + Optinszationtetnods(x@) # bxact Line searen 1sd_exact, history_sd_oxact ~ opt nethods.steepest_descent_with_exact_line search(nax_iter=10) pé,oatarrane(history_seracty columse["*", "9'1) orton reoreh_values.x1st°, SndexcFalse) ‘ntps:icolab research [Link]!1S-p_2KKvz-BpS2uCw20Nd0_oTYLDMIMGHscrolfo-xrTZQOXINIWPSprintMode=true 3a 5716924, 12:22 PM ‘Assignment2ipynb - Colab Gf = [Link]([Link], colums=["x', "y"T) . Sf tovexcel(‘reuton_pethod,voluee- xlsx, indowsFalse) f Steapest descent with backtracking ssd_ backtrace, history sd backtrace = opt sethods. steepest_descent with backtracking(nax feereia) GF = pasoatafrane( history £4 backtrack, columse(‘x°y °¥']) GF tovexcel{ steepest dercent_values_packtracking.alsx", Index-False) eet Fox ye return (1-ayer4 4 (2) "24 Gea x = npeLinspacet®, 5, 480) y= mpoLinspace(-3, 6, 408) Bs y= npsnesnerid(x, 9) ret») pls. tigure(ftesize~da8, 5) plevcontour(es Ys 2, levelsenp,logspace(®, 5» 38)» enape"et") pitiplos(nsstory sa backtrack(:, 8], nistorys@_pocktrsck{:, 2], ‘roe', Iabelo'steepest Descent (@acktracking)') pltiplot(nistoryasé_exact(:, 0], history.sd_oract[:, 1], 'go-', Iabel- ‘steepest Descent (Exact Line Search") pitiplot(nistorycnewton_exact[:, 8], nistory_rewton_exact[:, 2], "bo-', Iabele"vewton's Method (Exact Line Search)") prt aegend() pledater("x') [Link]('y") Ie. title(" contour plot of the function with optintzation paths") plsveriTrue) ple-shoe() = Contour plot of the function with optimization paths Ae stespest escent Baekerackng) ~O steepest Descent (Exact Line Search) { o> Newton's Method (Exact Line Search) 1 hntps:ilolab research [Link]!1S-p_2KK\7-BpS2uGw20NA0_oTYLDMHG#scrollTorxrT2QCXINIWPSprintMode=rue aa

You might also like