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

Function hill_climb

rl3v2/visualize_hill_climbing.py:10–32  ·  view source on GitHub ↗
(
    f, bounds, pop_size=1, sigma=0.3, alpha=0.3, iterations=100
)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 2

fFunction · 0.70
copyMethod · 0.45

Tested by

no test coverage detected