(
f, bounds, pop_size=1, sigma=0.3, alpha=0.3, iterations=100
)
| 8 | |
| 9 | # Evolution Strategies optimizer (simple version) |
| 10 | def hill_climb( |
| 11 | f, bounds, pop_size=1, sigma=0.3, alpha=0.3, iterations=100 |
| 12 | ): |
| 13 | dim = 2 |
| 14 | mu = np.random.uniform(bounds[0], bounds[1], size=dim) |
| 15 | |
| 16 | history = [] |
| 17 | best_f = f(mu) |
| 18 | |
| 19 | for gen in range(iterations): |
| 20 | # Sample noise |
| 21 | noise = np.random.randn(pop_size, dim) |
| 22 | population = mu + sigma * noise |
| 23 | fitness = np.array([f(x[0], x[1]) for x in population]) |
| 24 | |
| 25 | history.append((population.copy(), mu.copy())) |
| 26 | |
| 27 | # Update point if it's better |
| 28 | if fitness[0] > best_f: |
| 29 | best_f = fitness[0] |
| 30 | mu = population.flatten() |
| 31 | |
| 32 | return history |
| 33 | |
| 34 | # Visualization function |
| 35 | def visualize_es(history, bounds, f, resolution=100): |
no test coverage detected