-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_optimizer_example.py
46 lines (34 loc) · 1.5 KB
/
new_optimizer_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
from benchmark_handler import BenchmarkHandler
import matplotlib.pyplot as plt
from optimizers.optimizer import Optimizer
class RandomSearch(Optimizer):
def __init__(self, max_budget=99, verbose=False):
self.max_budget = max_budget
self.verbose = verbose
def observe_and_suggest(self, configurations, observed_lc):
if self.verbose:
print("Observed Learning Curves:")
print(observed_lc)
min_budget = self.max_budget+1
while min_budget == (self.max_budget+1): #we iterate till we find a curve that is not complete
ix = np.random.randint(len(configurations))
if ix in observed_lc.keys():
min_budget = len(observed_lc[ix])
else:
min_budget = 1
budget = np.random.randint(min_budget, self.max_budget)
return ix, budget
benchmark = BenchmarkHandler(environment = "Pong-v0",
search_space = "PPO",
seed = 0)
random_searcher = RandomSearch(verbose=False)
observed_lc, max_observed_perf, best_configuration, best_configuration_ix = benchmark.run_bo(optimizer=random_searcher,
iterations=1000)
plt.plot(observed_lc[best_configuration_ix])
plt.title("Best observed learning curve")
plt.show()
for lc in observed_lc.values():
plt.plot(lc)
plt.title("All observed learning curves")
plt.show()