{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def newton_armijo(\n", " f, grad, hess, x,\n", " step = 1.0,\n", " sigma = 0.1,\n", " eta = 0.1,\n", " epsilon = 1e-6,\n", " return_niter = False,\n", " tau = 0.1,\n", " theta = 0.1\n", " ):\n", "\n", " g_k = grad( x )\n", "\n", " niter = 0\n", "\n", " while np.linalg.norm( g_k ) > epsilon:\n", "\n", " step_k = step\n", "\n", " d_k = np.linalg.solve( hess( x ), -g_k )\n", " d_k_g_k = d_k @ g_k\n", " if d_k_g_k > -tau * np.linalg.norm( g_k ) * np.linalg.norm( d_k ):\n", " d_k = -g_k\n", " d_k_g_k = d_k @ g_k\n", " elif np.linalg.norm( d_k ) < theta * np.linalg.norm( g_k ):\n", " d_k = -g_k\n", " d_k_g_k = d_k @ g_k\n", "\n", " f_k = f( x )\n", "\n", " x_try = x - step_k * g_k\n", " while f( x_try ) > f_k + sigma * step_k * d_k_g_k:\n", " step_k = step_k * eta\n", " x_try = x - step_k * g_k\n", "\n", " x = x_try\n", " g_k = grad( x )\n", "\n", " niter = niter + 1\n", "\n", " if return_niter:\n", " return ( x, niter )\n", " else:\n", " return x" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def rosenbrock( x, a = 100, b = 1 ):\n", "\n", " return a * ( x[ 1 ] - x[ 0 ] ** 2 ) ** 2 + ( x[ 0 ] - b ) ** 2\n", "\n", "def rosenbrock_grad( x, a = 100, b = 1 ):\n", "\n", " return np.array(\n", " (\n", " -400 * ( x[ 1 ] - x[ 0 ] ** 2 ) * x[ 0 ] + 2 * ( x[ 0 ] - 1 ),\n", " 200 * ( x[ 1 ] - x[ 0 ] ** 2 )\n", " )\n", " )\n", "\n", "def rosenbrock_hess( x, a = 100, b = 1 ):\n", "\n", " return np.array(\n", " (\n", " ( -400 * ( x[ 1 ] - x[ 0 ] ** 2 ) + 800 * x[ 0 ] ** 2 + 2, -400 * x[ 0 ] ),\n", " ( -400 * x[ 0 ], 200 )\n", " )\n", " )" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([1., 1.]), 833)\n" ] } ], "source": [ "print( newton_armijo( rosenbrock, rosenbrock_grad, rosenbrock_hess, np.zeros( ( 2, ) ), epsilon = 1e-10, return_niter = True ) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }