Unconstrained local optimization with Scipy (Newton algorithm)


In [ ]:
%matplotlib inline

import matplotlib

try:
    from ailiib.utils.plot import plot_2d_contour_solution_space, plot_2d_solution_space
    from ailib.optimize.functions.unconstrained import rosen2d
except ModuleNotFoundError:
    print("Please install ailib:")
    print("pip install ailib")

import numpy as np

from scipy import optimize

The the Newton-Raphson method


In [ ]:
func = rosen2d

bounds = [[-10, 10], [-10, 10]]

res = optimize.newton(func, x0=[1., 1.])

print("x* =", res)

Plot solution


In [ ]:
plot_2d_contour_solution_space(func,
                               xmin=[-10, -5],
                               xmax=10*np.ones(2),
                               xstar=res,
                               title="Rosenbrock function")

plt.tight_layout()

plt.show();

In [ ]:
plot_2d_solution_space(func,
                       xmin=-2*np.ones(2),
                       xmax=2*np.ones(2),
                       xstar=res,
                       angle_view=(55, 83),
                       title="Sphere function")

plt.tight_layout()

plt.show();