Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rllib] Added evaluation script to RLLib #1295

Merged
merged 12 commits into from
Dec 11, 2017
Next Next commit
Added eval script
  • Loading branch information
pschafhalter committed Dec 8, 2017
commit c25094bbbfd6c370682202653a498c50200cb734
53 changes: 53 additions & 0 deletions python/ray/rllib/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you do the import future stuff from train.py as well here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed in the latest commit

import argparse
import gym
import ray

from dqn import DQNAgent
from agent import get_agent_class


EXAMPLE_USAGE = """
example usage:
./train.py /tmp/ray/checkpoint_dir/checkpoint-0 --run DQN --env CartPole-v0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should say ./eval.py right?

"""


parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Evaluates a reinforcement learning agent given a checkpoint.",
epilog=EXAMPLE_USAGE)

parser.add_argument("checkpoint", type=str, help="Checkpoint which to evaluate.")
required_named = parser.add_argument_group("required named arguments")
required_named.add_argument(
"--run", type=str, required=True,
help="The algorithm or model to train. This may refer to the name "
"of a built-on algorithm (e.g. RLLib's DQN or PPO), or a "
"user-defined trainable function or class registered in the "
"tune registry.")
required_named.add_argument(
"--env", type=str, required=True, help="The gym environment to use.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need --config?

parser.add_argument(
"--hide", default=False, action="store_const", const=True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--no-render

help="Surpress rendering of the environment.")


if __name__ == "__main__":
args = parser.parse_args()
ray.init()

cls = get_agent_class(args.run)
agent = cls(env=args.env)
agent._restore(args.checkpoint)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent.restore is the proper way


env = gym.make(args.env)
state = env.reset()
done = False
while not done:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider doing it in an infinite loop.

action = agent.compute_action(state)
state, reward, done, _ = env.step(action)
if not args.hide:
env.render()