MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / evolution_strategies

Function evolution_strategies

rl3v2/visualize_es.py:10–30  ·  view source on GitHub ↗
(
    f, bounds, pop_size=50, sigma=0.3, alpha=0.03, iterations=100
)

Source from the content-addressed store, hash-verified

8
9# Evolution Strategies optimizer (simple version)
10def 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
33def visualize_es(history, bounds, f, resolution=100):

Callers 1

visualize_es.pyFile · 0.85

Calls 2

fFunction · 0.70
copyMethod · 0.45

Tested by

no test coverage detected