Skip to content

Commit 3e331ce

Browse files
committed
pbt tests
1 parent a899a8c commit 3e331ce

9 files changed

Lines changed: 63 additions & 18 deletions

File tree

examples/configs/sac_pb2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defaults:
77

88
hydra:
99
sweeper:
10-
budget: ${algorithm.total_timesteps}
10+
budget: ${multiply:${algorithm.total_timesteps},${hydra.sweeper.sweeper_kwargs.optimizer_kwargs.population_size}}
1111
budget_variable: algorithm.total_timesteps
1212
loading_variable: load
1313
saving_variable: save

examples/configs/sac_pbt.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defaults:
77

88
hydra:
99
sweeper:
10-
budget: ${algorithm.total_timesteps}
10+
budget: ${multiply:${algorithm.total_timesteps},${hydra.sweeper.sweeper_kwargs.optimizer_kwargs.population_size}}
1111
budget_variable: algorithm.total_timesteps
1212
loading_variable: load
1313
saving_variable: save

examples/sb3_rl_agent.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from stable_baselines3.common.evaluation import evaluate_policy
1010
from stable_baselines3.common.monitor import Monitor
1111

12+
OmegaConf.register_new_resolver("multiply", lambda x, y: x * y, replace=True)
13+
1214
__copyright__ = "Copyright 2022, Theresa Eimer"
1315
__license__ = "3-clause BSD"
1416

hydra_plugins/hyper_pbt/hyper_pbt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def ask(self):
8787
self.population_id += 1
8888
if iteration_end:
8989
self.iteration += 1
90-
return Info(config=config, budget=self.budget_per_run, load_path=None, seed=None), iteration_end
90+
return Info(config=config, budget=self.budget_per_run, load_path=None, seed=None), iteration_end, False
9191
config, load_path = self.perturb_config(self.population_id)
9292
self.population_id += 1
9393
if iteration_end:
@@ -172,8 +172,8 @@ def tell(self, info, value):
172172
# Now that we have finished the iteration,
173173
# we can safely remove all checkpoints from the previous iteration
174174
print(f"Finished iteration {self.iteration}")
175-
print("Remove checkpoints")
176175
if self.self_destruct and self.iteration > 1:
176+
print("Remove checkpoints")
177177
self.remove_checkpoints(self.iteration - 2)
178178

179179
def finish_run(self, output_path):

hydra_plugins/hyper_pbt/pb2_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ def K2(self, x, x2):
187187
def K(self, x, x2):
188188
"""Compute the kernel."""
189189
# clip epsilons
190-
self.epsilon_1 = min(self.epsilon_1, 0.5)
191-
192-
self.epsilon_2 = min(self.epsilon_2, 0.5)
190+
self.epsilon_1 = GPy.core.Param("epsilon_1", min(self.epsilon_1, 0.5))
191+
self.epsilon_2 = GPy.core.Param("epsilon_1", min(self.epsilon_2, 0.5))
193192

194193
# format data
195194
if x2 is None:

hydra_plugins/hypersweeper/hypersweeper_sweeper.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,12 @@ def run(self, verbose=False):
456456
infos = []
457457
t = 0
458458
terminate = False
459-
while (
460-
t < self.max_parallel
461-
and not terminate
462-
and not trial_termination
463-
and not budget_termination
464-
and not optimizer_termination
465-
):
459+
while t < self.max_parallel and not terminate and not trial_termination and not optimizer_termination:
460+
if not any(b is None for b in self.history["budget"]) and self.budget is not None:
461+
budget_termination = sum(self.history["budget"]) >= self.budget
462+
if budget_termination:
463+
break
464+
466465
try:
467466
info, terminate, optimizer_termination = self.optimizer.ask()
468467
except Exception as e: # noqa: BLE001
@@ -484,8 +483,7 @@ def run(self, verbose=False):
484483
if info.load_path is not None:
485484
loading_paths.append(info.load_path)
486485
infos.append(info)
487-
if not any(b is None for b in self.history["budget"]) and self.budget is not None:
488-
budget_termination = sum(self.history["budget"]) >= self.budget
486+
489487
if self.n_trials is not None:
490488
trial_termination = self.trials_run + len(configs) >= self.n_trials
491489

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ log_level = "DEBUG"
7070
xfail_strict = true
7171
addopts = "--durations=10 -vv"
7272
markers = ["example: An example"]
73-
adopts = "--ignore=tests/test_runs/test_neps.py"
7473

7574
[tool.coverage.run]
7675
branch = true

tests/test_runs/test_neps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
# runhistory = pd.read_csv("./tmp/mlp_neps_priorband/runhistory.csv")
3030
# assert not runhistory.empty, "Run history is empty"
3131
# assert len(runhistory) == 5, "Run history should contain 5 entries"
32-
# shutil.rmtree(Path("./tmp/mlp_neps_priorband"))
32+
# shutil.rmtree(Path("./tmp/mlp_neps_priorband"))

tests/test_runs/test_pbt.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import shutil
2+
import subprocess
3+
from pathlib import Path
4+
5+
import pandas as pd
6+
7+
8+
def test_pbt_sac_example():
9+
if Path("./tmp/sac_pbt").exists():
10+
shutil.rmtree(Path("./tmp/sac_pbt"))
11+
subprocess.call(
12+
[
13+
"python",
14+
"examples/sb3_rl_agent.py",
15+
"--config-name=sac_pbt",
16+
"-m",
17+
"algorithm.total_timesteps=1000",
18+
"hydra.sweeper.sweeper_kwargs.optimizer_kwargs.config_interval=500",
19+
]
20+
)
21+
assert Path("./tmp/sac_pbt").exists(), "Run directory not created"
22+
assert Path("./tmp/sac_pbt/runhistory.csv").exists(), "Run history file not created"
23+
runhistory = pd.read_csv("./tmp/sac_pbt/runhistory.csv")
24+
assert not runhistory.empty, "Run history is empty"
25+
assert len(runhistory) == 4, "Run history should contain 4 entries"
26+
shutil.rmtree(Path("./tmp/sac_pbt"))
27+
28+
29+
def test_pb2_sac_example():
30+
if Path("./tmp/sac_pb2").exists():
31+
shutil.rmtree(Path("./tmp/sac_pb2"))
32+
subprocess.call(
33+
[
34+
"python",
35+
"examples/sb3_rl_agent.py",
36+
"--config-name=sac_pb2",
37+
"-m",
38+
"algorithm.total_timesteps=1000",
39+
"hydra.sweeper.sweeper_kwargs.optimizer_kwargs.config_interval=500",
40+
]
41+
)
42+
assert Path("./tmp/sac_pb2").exists(), "Run directory not created"
43+
assert Path("./tmp/sac_pb2/runhistory.csv").exists(), "Run history file not created"
44+
runhistory = pd.read_csv("./tmp/sac_pb2/runhistory.csv")
45+
assert not runhistory.empty, "Run history is empty"
46+
assert len(runhistory) == 4, "Run history should contain 4 entries"
47+
shutil.rmtree(Path("./tmp/sac_pb2"))

0 commit comments

Comments
 (0)