|
| 1 | +import unittest |
| 2 | + |
| 3 | +from tensorforce import Agent, Environment |
| 4 | + |
| 5 | +class TestTensorforce(unittest.TestCase): |
| 6 | + # based on https://github.com/tensorforce/tensorforce/tree/master#quickstart-example-code. |
| 7 | + def test_quickstart(self): |
| 8 | + environment = Environment.create( |
| 9 | + environment='gym', level='CartPole', max_episode_timesteps=500 |
| 10 | + ) |
| 11 | + |
| 12 | + agent = Agent.create( |
| 13 | + agent='tensorforce', |
| 14 | + environment=environment, # alternatively: states, actions, (max_episode_timesteps) |
| 15 | + memory=1000, |
| 16 | + update=dict(unit='timesteps', batch_size=32), |
| 17 | + optimizer=dict(type='adam', learning_rate=3e-4), |
| 18 | + policy=dict(network='auto'), |
| 19 | + objective='policy_gradient', |
| 20 | + reward_estimation=dict(horizon=1) |
| 21 | + ) |
| 22 | + |
| 23 | + # Train for a single episode. |
| 24 | + states = environment.reset() |
| 25 | + actions = agent.act(states=states) |
| 26 | + states, terminal, reward = environment.execute(actions=actions) |
| 27 | + |
| 28 | + self.assertEqual(4, len(states)) |
| 29 | + self.assertFalse(terminal) |
| 30 | + self.assertEqual(1, reward) |
0 commit comments