-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add step_hooks argument to train_agent with tests
- Loading branch information
Showing
2 changed files
with
70 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from __future__ import unicode_literals | ||
from __future__ import print_function | ||
from __future__ import division | ||
from __future__ import absolute_import | ||
from builtins import * # NOQA | ||
from future import standard_library | ||
standard_library.install_aliases() | ||
import tempfile | ||
import unittest | ||
|
||
import mock | ||
|
||
import chainerrl | ||
|
||
|
||
class TestTrainAgent(unittest.TestCase): | ||
|
||
def test(self): | ||
|
||
outdir = tempfile.mkdtemp() | ||
|
||
agent = mock.Mock() | ||
env = mock.Mock() | ||
# Reaches the terminal state after five actions | ||
env.reset.side_effect = [('state', 0)] | ||
env.step.side_effect = [ | ||
(('state', 1), 0, False, {}), | ||
(('state', 2), 0, False, {}), | ||
(('state', 3), -0.5, False, {}), | ||
(('state', 4), 0, False, {}), | ||
(('state', 5), 1, True, {}), | ||
] | ||
hook = mock.Mock() | ||
|
||
chainerrl.experiments.train_agent( | ||
agent=agent, | ||
env=env, | ||
steps=5, | ||
outdir=outdir, | ||
step_hooks=[hook]) | ||
|
||
self.assertEqual(agent.act_and_train.call_count, 5) | ||
self.assertEqual(agent.stop_episode_and_train.call_count, 1) | ||
|
||
self.assertEqual(env.reset.call_count, 1) | ||
self.assertEqual(env.step.call_count, 5) | ||
|
||
self.assertEqual(hook.call_count, 5) | ||
# A hook receives (env, agent, step) | ||
for i, call in enumerate(hook.call_args_list): | ||
args, kwargs = call | ||
self.assertEqual(args[0], env) | ||
self.assertEqual(args[1], agent) | ||
# step starts with 1 | ||
self.assertEqual(args[2], i + 1) |