Rosenbrock banana¶
Here, we perform optimization for the Rosenbrock banana function, which does not require an AMICI model. In particular, we try several ways of specifying derivative information.
[1]:
import pypesto
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
%matplotlib inline
Define the objective and problem¶
[2]:
# first type of objective
objective1 = pypesto.Objective(fun=sp.optimize.rosen,
grad=sp.optimize.rosen_der,
hess=sp.optimize.rosen_hess)
# second type of objective
def rosen2(x):
return sp.optimize.rosen(x), sp.optimize.rosen_der(x), sp.optimize.rosen_hess(x)
objective2 = pypesto.Objective(fun=rosen2, grad=True, hess=True)
dim_full = 10
lb = -2 * np.ones((dim_full, 1))
ub = 2 * np.ones((dim_full, 1))
problem1 = pypesto.Problem(objective=objective1, lb=lb, ub=ub)
problem2 = pypesto.Problem(objective=objective2, lb=lb, ub=ub)
Illustration¶
[3]:
x = np.arange(-2, 2, 0.1)
y = np.arange(-2, 2, 0.1)
x, y = np.meshgrid(x, y)
z = np.zeros_like(x)
for j in range(0, x.shape[0]):
for k in range(0, x.shape[1]):
z[j,k] = objective1([x[j,k], y[j,k]], (0,))
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X=x, Y=y, Z=z)
[3]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7fd15ad50278>
Run optimization¶
[4]:
optimizer = pypesto.ScipyOptimizer()
n_starts = 20
result1 = pypesto.minimize(problem=problem1, optimizer=optimizer, n_starts=n_starts)
result2 = pypesto.minimize(problem=problem2, optimizer=optimizer, n_starts=n_starts)
Visualize and analyze results¶
pypesto offers easy-to-use visualization routines:
[5]:
import pypesto.visualize
pypesto.visualize.waterfall(result1)
pypesto.visualize.parameters(result1)
[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd15a929d68>
If the result needs to be examined in more detail, it can easily be exported as a pandas.DataFrame:
[6]:
result1.optimize_result.as_dataframe(['fval', 'n_fval', 'n_grad', 'n_hess', 'n_res', 'n_sres', 'time'])
[6]:
fval | n_fval | n_grad | n_hess | n_res | n_sres | time | |
---|---|---|---|---|---|---|---|
0 | 2.967459e-12 | 83 | 83 | 0 | 0 | 0 | 0.015485 |
1 | 4.049190e-12 | 87 | 87 | 0 | 0 | 0 | 0.014637 |
2 | 7.886079e-12 | 99 | 99 | 0 | 0 | 0 | 0.038658 |
3 | 9.738153e-12 | 78 | 78 | 0 | 0 | 0 | 0.010974 |
4 | 1.327963e-11 | 72 | 72 | 0 | 0 | 0 | 0.011126 |
5 | 1.548685e-11 | 60 | 60 | 0 | 0 | 0 | 0.008633 |
6 | 2.444331e-11 | 87 | 87 | 0 | 0 | 0 | 0.016484 |
7 | 4.726193e-11 | 103 | 103 | 0 | 0 | 0 | 0.014987 |
8 | 7.583920e-11 | 72 | 72 | 0 | 0 | 0 | 0.011051 |
9 | 8.009222e-11 | 73 | 73 | 0 | 0 | 0 | 0.011766 |
10 | 8.351843e-11 | 79 | 79 | 0 | 0 | 0 | 0.023473 |
11 | 8.571021e-11 | 86 | 86 | 0 | 0 | 0 | 0.016769 |
12 | 9.786016e-11 | 77 | 77 | 0 | 0 | 0 | 0.010761 |
13 | 1.190891e-10 | 95 | 95 | 0 | 0 | 0 | 0.017382 |
14 | 1.233392e-10 | 73 | 73 | 0 | 0 | 0 | 0.010384 |
15 | 1.463236e-10 | 87 | 87 | 0 | 0 | 0 | 0.012187 |
16 | 1.952418e-10 | 63 | 63 | 0 | 0 | 0 | 0.010246 |
17 | 3.268263e-10 | 81 | 81 | 0 | 0 | 0 | 0.011533 |
18 | 3.986579e+00 | 71 | 71 | 0 | 0 | 0 | 0.010817 |
19 | 3.986579e+00 | 90 | 90 | 0 | 0 | 0 | 0.016129 |