(history, bounds, f, resolution=100)
| 33 | |
| 34 | # Visualization function |
| 35 | def visualize_es(history, bounds, f, resolution=100): |
| 36 | x = np.linspace(bounds[0], bounds[1], resolution) |
| 37 | y = np.linspace(bounds[0], bounds[1], resolution) |
| 38 | X, Y = np.meshgrid(x, y) |
| 39 | Z = f(X, Y) |
| 40 | |
| 41 | plt.figure(figsize=(8, 6)) |
| 42 | for i, (pop, mu) in enumerate(history): |
| 43 | plt.clf() |
| 44 | plt.contourf(X, Y, Z, levels=50, cmap='viridis') |
| 45 | plt.colorbar(label="f(x, y)") |
| 46 | plt.scatter(pop[:, 0], pop[:, 1], c='white', s=20, label='Population') |
| 47 | plt.scatter(mu[0], mu[1], c='red', s=80, label='Mean', edgecolors='black') |
| 48 | plt.title(f"Hill Climbing - Step {i+1}") |
| 49 | plt.xlim(bounds[0], bounds[1]) |
| 50 | plt.ylim(bounds[0], bounds[1]) |
| 51 | plt.xlabel('x') |
| 52 | plt.ylabel('y') |
| 53 | plt.legend() |
| 54 | # plt.pause(0.1) |
| 55 | plt.waitforbuttonpress() |
| 56 | plt.show() |
| 57 | |
| 58 | # Run |
| 59 | bounds = (-5, 5) |
no test coverage detected