Skip to content

Commit

Permalink
Added script demonstrating how to restore a policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Panchenko committed Aug 14, 2024
1 parent 36993fa commit 31d3294
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions scripts/restore_and_watch_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path
from typing import cast

from armscan_env.wrapper import ArmscanEnvFactory

from tianshou.highlevel.env import EnvMode
from tianshou.highlevel.experiment import Experiment

# Place your path here
saved_experiment_dir = Path("log/random-actions/42/20240814-150513")

if __name__ == "__main__":
restored_experiment = Experiment.from_directory(str(saved_experiment_dir), restore_policy=True)

restored_policy = restored_experiment.create_experiment_world().policy
# this can be now used to perform actions, you can also use a notebook

env_factory: ArmscanEnvFactory = cast(ArmscanEnvFactory, restored_experiment.env_factory)
# TODO @Carlo: modify here to set different volumes
env_factory.name2volume = env_factory.name2volume

# Create env manually and run policy on it
restored_env = env_factory.create_env(mode=EnvMode.WATCH)
obs, info = restored_env.reset()
for _ in range(5):
obs, *_ = restored_env.step(restored_policy.compute_action(obs))

# Or use the restored experiment to run the policy
restored_experiment.config.train = False
restored_experiment.config.watch = True
restored_experiment.config.persistence_enabled = False
restored_experiment.run()

0 comments on commit 31d3294

Please sign in to comment.