Skip to content

Commit

Permalink
Fix astar_log test (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaps0dy authored Apr 26, 2024
2 parents 0183b4b + 9c6a5cc commit 9b32dd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
5 changes: 5 additions & 0 deletions envpool/sokoban/level_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ LevelLoader::LevelLoader(const std::filesystem::path& base_path,
level_file_paths_.push_back(entry.path());
}
}
std::sort(
level_file_paths_.begin(), level_file_paths_.end(),
[](const std::filesystem::path& a, const std::filesystem::path& b) {
return a.filename().string() < b.filename().string();
});
}
cur_file_ = level_file_paths_.begin();
}
Expand Down
50 changes: 25 additions & 25 deletions envpool/sokoban/sokoban_py_envpool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import re
import subprocess
import sys
import tempfile
import time

import numpy as np
Expand Down Expand Up @@ -111,7 +110,7 @@ def test_envpool_load_sequentially(capfd) -> None:
levels_dir = "/app/envpool/sokoban/sample_levels"
files = glob.glob(f"{levels_dir}/*.txt")
levels_by_files = []
for file in files:
for file in sorted(files):
with open(file, "r") as f:
text = f.read()
levels = text.split("\n;")
Expand Down Expand Up @@ -204,10 +203,10 @@ def print_obs(obs: np.ndarray):


action_astar_to_envpool = {
"0": 1,
"1": 4,
"2": 2,
"3": 3,
"0": 0,
"1": 3,
"2": 1,
"3": 2,
}


Expand Down Expand Up @@ -243,7 +242,7 @@ def test_solved_level_does_not_truncate(solve_on_time: bool):
)
assert not term and not trunc, "Level should not have reached time limit"

NOOP = 0
wrong_action = str((int(SOLVE_LEVEL_ZERO[-1]) + 1) % 4)

if solve_on_time:
obs, reward, term, trunc, infos = env.step(
Expand All @@ -256,30 +255,31 @@ def test_solved_level_does_not_truncate(solve_on_time: bool):
assert term and not trunc, "Level should finish within the time limit"

else:
obs, reward, term, trunc, infos = env.step(make_1d_array(NOOP))
obs, reward, term, trunc, infos = env.step(make_1d_array(wrong_action))
assert not term and trunc, "Level should truncate at precisely this step"

_, _, term, trunc, _ = env.step(make_1d_array(NOOP))
_, _, term, trunc, _ = env.step(make_1d_array(wrong_action))
assert not term and not trunc, "Level should reset correctly"


@pytest.mark.skip
def test_astar_log() -> None:
def test_astar_log(tmp_path) -> None:
level_file_name = "/app/envpool/sokoban/sample_levels/small.txt"
with tempfile.NamedTemporaryFile() as f:
log_file_name = f.name
subprocess.run(
[
"/root/go/bin/bazel", "run", "//envpool/sokoban:astar_log", "--",
level_file_name, log_file_name, "1"
],
check=True,
cwd="/app",
env=dict(HOME="/root"),
)
with open(log_file_name, "r") as f:
log = f.read()
assert f"1, {SOLVE_LEVEL_ZERO}, 21, 1443" == log.split("\n")[1]
log_file_name = tmp_path / "log_file.csv"
subprocess.run(
[
"/root/go/bin/bazel", f"--output_base={str(tmp_path)}", "run",
"//envpool/sokoban:astar_log", "--", level_file_name,
str(log_file_name), "1"
],
check=True,
cwd="/app/envpool",
env={
"HOME": "/root",
"PATH": "/opt/conda/bin:/usr/bin"
},
)
log = log_file_name.read_text()
assert f"0,{SOLVE_LEVEL_ZERO},21,1380" == log.split("\n")[1]


if __name__ == "__main__":
Expand Down

0 comments on commit 9b32dd0

Please sign in to comment.