Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
from scipy.optimize import minimize_scalar, minimize
import matplotlib.pyplot as plt
""" EXAMPLE PROB """
N_EVAL = 5
N_BOUNDS = (0., 1.2)
grid1d = np.linspace(*N_BOUNDS, N_EVAL)
def fun(x):
return -(1.4 - 3*x) * np.sin(18*x)
""" MANUAL (NON-)GRID-SEARCH WITH POLISHING """
raw_funs, polished_x, polished_funs = [], [], []
for x in grid1d:
fun_eval = fun(x)
raw_funs.append(fun_eval)
# use multivariate-opt as 1d-opt finds global-opt too easily!
# (want to show local-opt!)
polished_res = minimize(fun, x, method='L-BFGS-B', bounds=[N_BOUNDS])
polished_x.append(polished_res.x)
polished_funs.append(polished_res.fun)
""" PLOT """
plot_x = np.linspace(*N_BOUNDS, 100)
plot_y = fun(plot_x)
ax = plt.subplot(111)
ax.plot(plot_x, plot_y, color='black')
[ax.axvline(i, color='blue') for i in grid1d]
ax.scatter(grid1d, raw_funs, color='blue', label='raw grid-eval')
ax.scatter(polished_x, polished_funs, color='red', label='polished by local-opt')
# ugly code
for i in range(N_EVAL):
ax.arrow(grid1d[i], raw_funs[i],
(polished_x[i]-grid1d[i])[0],
(polished_funs[i]-raw_funs[i])[0],
color='magenta')
plt.legend(loc=2)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment