(
f, bounds, pop_size=50, sigma=0.3, alpha=0.03, iterations=100
)
| 8 | |
| 9 | # Evolution Strategies optimizer (simple version) |
| 10 | def evolution_strategies( |
| 11 | f, bounds, pop_size=50, sigma=0.3, alpha=0.03, iterations=100 |
| 12 | ): |
| 13 | dim = 2 |
| 14 | mu = np.random.uniform(bounds[0], bounds[1], size=dim) |
| 15 | |
| 16 | history = [] |
| 17 | |
| 18 | for gen in range(iterations): |
| 19 | # Sample noise |
| 20 | noise = np.random.randn(pop_size, dim) |
| 21 | population = mu + sigma * noise |
| 22 | fitness = np.array([f(x[0], x[1]) for x in population]) |
| 23 | |
| 24 | history.append((population.copy(), mu.copy())) |
| 25 | |
| 26 | # Normalize fitness for weighting |
| 27 | fitness_norm = (fitness - np.mean(fitness)) / (np.std(fitness) + 1e-8) |
| 28 | mu += alpha / (pop_size * sigma) * np.dot(noise.T, fitness_norm) |
| 29 | |
| 30 | return history |
| 31 | |
| 32 | # Visualization function |
| 33 | def visualize_es(history, bounds, f, resolution=100): |
no test coverage detected