def test_pickleable(self):
     env = GridWorldEnv(desc="8x8")
     round_trip = pickle.loads(pickle.dumps(env))
     assert round_trip
     assert round_trip.start_state == env.start_state
     step_env(round_trip)
     round_trip.close()
     env.close()
示例#2
0
 def test_does_not_modify_action(self):
     env = GridWorldEnv(desc='8x8')
     a = env.action_space.sample()
     a_copy = a
     env.reset()
     env.step(a)
     assert a == a_copy
     env.close()
 def test_does_not_modify_action(self):
     env = GridWorldEnv(desc="8x8")
     a = env.action_space.sample()
     a_copy = a
     env.reset()
     env.step(a)
     self.assertEqual(a, a_copy)
     env.close()
def test_obtain_samples(ray_local_session_fixture):
    del ray_local_session_fixture
    env = GridWorldEnv(desc='4x4')
    policy = ScriptedPolicy(
        scripted_actions=[2, 2, 1, 0, 3, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1])
    algo = Mock(env_spec=env.spec, policy=policy, max_episode_length=16)

    assert ray.is_initialized()
    workers = WorkerFactory(seed=100,
                            max_episode_length=algo.max_episode_length,
                            n_workers=8)
    sampler1 = RaySampler.from_worker_factory(workers, policy, env)
    sampler2 = LocalSampler.from_worker_factory(workers, policy, env)
    eps1 = sampler1.obtain_samples(0, 1000,
                                   tuple(algo.policy.get_param_values()))
    eps2 = sampler2.obtain_samples(0, 1000,
                                   tuple(algo.policy.get_param_values()))

    assert eps1.observations.shape[0] >= 1000
    assert eps1.actions.shape[0] >= 1000
    assert (sum(eps1.rewards[:eps1.lengths[0]]) == sum(
        eps2.rewards[:eps2.lengths[0]]) == 1)

    true_obs = np.array([0, 1, 2, 6, 10, 14])
    true_actions = np.array([2, 2, 1, 1, 1, 2])
    true_rewards = np.array([0, 0, 0, 0, 0, 1])
    start = 0
    for length in eps1.lengths:
        observations = eps1.observations[start:start + length]
        actions = eps1.actions[start:start + length]
        rewards = eps1.rewards[start:start + length]
        assert np.array_equal(observations, true_obs)
        assert np.array_equal(actions, true_actions)
        assert np.array_equal(rewards, true_rewards)
        start += length
    sampler1.shutdown_worker()
    sampler2.shutdown_worker()
    env.close()