|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from benchmark.funcs import TEST_FUNCTIONS |
| 7 | +from sambo import minimize |
| 8 | + |
| 9 | +test_funcs = [test for test in TEST_FUNCTIONS if len(test['bounds']) == 2] |
| 10 | +test_funcs[0]['func'] = lambda x: np.sin(x[1]) * np.exp((1-np.cos(x[0]))**2) + np.cos(x[0]) * np.exp((1-np.sin(x[1]))**2) + (x[0] - x[1])**2 |
| 11 | + |
| 12 | +fig, axes = plt.subplots(1, len(test_funcs), figsize=(len(test_funcs) * 2, 2), constrained_layout=True) |
| 13 | + |
| 14 | + |
| 15 | +for ax, test in zip(axes, test_funcs): |
| 16 | + b = test['bounds'] |
| 17 | + x = np.linspace(*b[0], 20) |
| 18 | + y = np.linspace(*b[1], 20) |
| 19 | + X, Y = np.meshgrid(x, y) |
| 20 | + |
| 21 | + Z = np.array([test['func'](np.array(x)) for x in zip(X.ravel(), Y.ravel())]).reshape(X.shape) |
| 22 | + ax: plt.Axes |
| 23 | + cnt = ax.contourf(X, Y, Z, levels=100, cmap='inferno_r') |
| 24 | + cnt.set_edgecolor('face') |
| 25 | + ax.axis('off') |
| 26 | + |
| 27 | + res = minimize(test['func'], bounds=test['bounds'], max_iter=500, n_init=300, disp=True) |
| 28 | + ax.scatter(res.x[0], res.x[1], s=12, c='k', alpha=.4, marker='x') |
| 29 | + |
| 30 | +fig.savefig(Path(__file__).parent.parent.parent / 'www/contourf.jpg') |
| 31 | +plt.show() |
0 commit comments