當前位置: 首頁>>代碼示例>>Python>>正文


Python policy_evaluator.PolicyEvaluator類代碼示例

本文整理匯總了Python中ray.rllib.evaluation.policy_evaluator.PolicyEvaluator的典型用法代碼示例。如果您正苦於以下問題:Python PolicyEvaluator類的具體用法?Python PolicyEvaluator怎麽用?Python PolicyEvaluator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了PolicyEvaluator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testMultiAgentSampleRoundRobin

 def testMultiAgentSampleRoundRobin(self):
     act_space = gym.spaces.Discrete(2)
     obs_space = gym.spaces.Discrete(10)
     ev = PolicyEvaluator(
         env_creator=lambda _: RoundRobinMultiAgent(5, increment_obs=True),
         policy_graph={
             "p0": (MockPolicyGraph, obs_space, act_space, {}),
         },
         policy_mapping_fn=lambda agent_id: "p0",
         batch_steps=50)
     batch = ev.sample()
     self.assertEqual(batch.count, 50)
     # since we round robin introduce agents into the env, some of the env
     # steps don't count as proper transitions
     self.assertEqual(batch.policy_batches["p0"].count, 42)
     self.assertEqual(batch.policy_batches["p0"]["obs"].tolist()[:10], [
         one_hot(0, 10),
         one_hot(1, 10),
         one_hot(2, 10),
         one_hot(3, 10),
         one_hot(4, 10),
     ] * 2)
     self.assertEqual(batch.policy_batches["p0"]["new_obs"].tolist()[:10], [
         one_hot(1, 10),
         one_hot(2, 10),
         one_hot(3, 10),
         one_hot(4, 10),
         one_hot(5, 10),
     ] * 2)
     self.assertEqual(batch.policy_batches["p0"]["rewards"].tolist()[:10],
                      [100, 100, 100, 100, 0] * 2)
     self.assertEqual(batch.policy_batches["p0"]["dones"].tolist()[:10],
                      [False, False, False, False, True] * 2)
     self.assertEqual(batch.policy_batches["p0"]["t"].tolist()[:10],
                      [4, 9, 14, 19, 24, 5, 10, 15, 20, 25])
開發者ID:robertnishihara,項目名稱:ray,代碼行數:35,代碼來源:test_multi_agent_env.py

示例2: testCustomRNNStateValues

    def testCustomRNNStateValues(self):
        h = {"some": {"arbitrary": "structure", "here": [1, 2, 3]}}

        class StatefulPolicyGraph(PolicyGraph):
            def compute_actions(self,
                                obs_batch,
                                state_batches,
                                prev_action_batch=None,
                                prev_reward_batch=None,
                                episodes=None,
                                **kwargs):
                return [0] * len(obs_batch), [[h] * len(obs_batch)], {}

            def get_initial_state(self):
                return [{}]  # empty dict

        ev = PolicyEvaluator(
            env_creator=lambda _: gym.make("CartPole-v0"),
            policy_graph=StatefulPolicyGraph,
            batch_steps=5)
        batch = ev.sample()
        self.assertEqual(batch.count, 5)
        self.assertEqual(batch["state_in_0"][0], {})
        self.assertEqual(batch["state_out_0"][0], h)
        self.assertEqual(batch["state_in_0"][1], h)
        self.assertEqual(batch["state_out_0"][1], h)
開發者ID:robertnishihara,項目名稱:ray,代碼行數:26,代碼來源:test_multi_agent_env.py

示例3: testCompleteEpisodes

 def testCompleteEpisodes(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: MockEnv(10),
         policy_graph=MockPolicyGraph,
         batch_steps=5,
         batch_mode="complete_episodes")
     batch = ev.sample()
     self.assertEqual(batch.count, 10)
開發者ID:jamescasbon,項目名稱:ray,代碼行數:8,代碼來源:test_policy_evaluator.py

示例4: testExternalEnvHorizonNotSupported

 def testExternalEnvHorizonNotSupported(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: SimpleServing(MockEnv(25)),
         policy_graph=MockPolicyGraph,
         episode_horizon=20,
         batch_steps=10,
         batch_mode="complete_episodes")
     self.assertRaises(ValueError, lambda: ev.sample())
開發者ID:robertnishihara,項目名稱:ray,代碼行數:8,代碼來源:test_external_env.py

示例5: testExternalEnvBadActions

 def testExternalEnvBadActions(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: SimpleServing(MockEnv(25)),
         policy_graph=BadPolicyGraph,
         sample_async=True,
         batch_steps=40,
         batch_mode="truncate_episodes")
     self.assertRaises(Exception, lambda: ev.sample())
開發者ID:robertnishihara,項目名稱:ray,代碼行數:8,代碼來源:test_external_env.py

示例6: testAsync

 def testAsync(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: gym.make("CartPole-v0"),
         sample_async=True,
         policy_graph=MockPolicyGraph)
     batch = ev.sample()
     for key in ["obs", "actions", "rewards", "dones", "advantages"]:
         self.assertIn(key, batch)
     self.assertGreater(batch["advantages"][0], 1)
開發者ID:jamescasbon,項目名稱:ray,代碼行數:9,代碼來源:test_policy_evaluator.py

示例7: testExternalEnvTruncateEpisodes

 def testExternalEnvTruncateEpisodes(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: SimpleServing(MockEnv(25)),
         policy_graph=MockPolicyGraph,
         batch_steps=40,
         batch_mode="truncate_episodes")
     for _ in range(3):
         batch = ev.sample()
         self.assertEqual(batch.count, 40)
開發者ID:robertnishihara,項目名稱:ray,代碼行數:9,代碼來源:test_external_env.py

示例8: testCompleteEpisodesPacking

 def testCompleteEpisodesPacking(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: MockEnv(10),
         policy_graph=MockPolicyGraph,
         batch_steps=15,
         batch_mode="complete_episodes")
     batch = ev.sample()
     self.assertEqual(batch.count, 20)
     self.assertEqual(
         batch["t"].tolist(),
         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
開發者ID:jamescasbon,項目名稱:ray,代碼行數:11,代碼來源:test_policy_evaluator.py

示例9: testExternalEnvOffPolicy

 def testExternalEnvOffPolicy(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: SimpleOffPolicyServing(MockEnv(25), 42),
         policy_graph=MockPolicyGraph,
         batch_steps=40,
         batch_mode="complete_episodes")
     for _ in range(3):
         batch = ev.sample()
         self.assertEqual(batch.count, 50)
         self.assertEqual(batch["actions"][0], 42)
         self.assertEqual(batch["actions"][-1], 42)
開發者ID:robertnishihara,項目名稱:ray,代碼行數:11,代碼來源:test_external_env.py

示例10: testBaselinePerformance

 def testBaselinePerformance(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: gym.make("CartPole-v0"),
         policy_graph=MockPolicyGraph,
         batch_steps=100)
     start = time.time()
     count = 0
     while time.time() - start < 1:
         count += ev.sample().count
     print()
     print("Samples per second {}".format(count / (time.time() - start)))
     print()
開發者ID:jamescasbon,項目名稱:ray,代碼行數:12,代碼來源:test_policy_evaluator.py

示例11: testFilterSync

 def testFilterSync(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: gym.make("CartPole-v0"),
         policy_graph=MockPolicyGraph,
         sample_async=True,
         observation_filter="ConcurrentMeanStdFilter")
     time.sleep(2)
     ev.sample()
     filters = ev.get_filters(flush_after=True)
     obs_f = filters["default"]
     self.assertNotEqual(obs_f.rs.n, 0)
     self.assertNotEqual(obs_f.buffer.n, 0)
開發者ID:jamescasbon,項目名稱:ray,代碼行數:12,代碼來源:test_policy_evaluator.py

示例12: testBatchesLargerWhenVectorized

 def testBatchesLargerWhenVectorized(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: MockEnv(episode_length=8),
         policy_graph=MockPolicyGraph,
         batch_mode="truncate_episodes",
         batch_steps=4,
         num_envs=4)
     batch = ev.sample()
     self.assertEqual(batch.count, 16)
     result = collect_metrics(ev, [])
     self.assertEqual(result["episodes_this_iter"], 0)
     batch = ev.sample()
     result = collect_metrics(ev, [])
     self.assertEqual(result["episodes_this_iter"], 4)
開發者ID:jamescasbon,項目名稱:ray,代碼行數:14,代碼來源:test_policy_evaluator.py

示例13: testGetFilters

 def testGetFilters(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: gym.make("CartPole-v0"),
         policy_graph=MockPolicyGraph,
         sample_async=True,
         observation_filter="ConcurrentMeanStdFilter")
     self.sample_and_flush(ev)
     filters = ev.get_filters(flush_after=False)
     time.sleep(2)
     filters2 = ev.get_filters(flush_after=False)
     obs_f = filters["default"]
     obs_f2 = filters2["default"]
     self.assertGreaterEqual(obs_f2.rs.n, obs_f.rs.n)
     self.assertGreaterEqual(obs_f2.buffer.n, obs_f.buffer.n)
開發者ID:jamescasbon,項目名稱:ray,代碼行數:14,代碼來源:test_policy_evaluator.py

示例14: testMultiAgentSampleWithHorizon

 def testMultiAgentSampleWithHorizon(self):
     act_space = gym.spaces.Discrete(2)
     obs_space = gym.spaces.Discrete(2)
     ev = PolicyEvaluator(
         env_creator=lambda _: BasicMultiAgent(5),
         policy_graph={
             "p0": (MockPolicyGraph, obs_space, act_space, {}),
             "p1": (MockPolicyGraph, obs_space, act_space, {}),
         },
         policy_mapping_fn=lambda agent_id: "p{}".format(agent_id % 2),
         episode_horizon=10,  # test with episode horizon set
         batch_steps=50)
     batch = ev.sample()
     self.assertEqual(batch.count, 50)
開發者ID:robertnishihara,項目名稱:ray,代碼行數:14,代碼來源:test_multi_agent_env.py

示例15: testMetrics

 def testMetrics(self):
     ev = PolicyEvaluator(
         env_creator=lambda _: MockEnv(episode_length=10),
         policy_graph=MockPolicyGraph,
         batch_mode="complete_episodes")
     remote_ev = PolicyEvaluator.as_remote().remote(
         env_creator=lambda _: MockEnv(episode_length=10),
         policy_graph=MockPolicyGraph,
         batch_mode="complete_episodes")
     ev.sample()
     ray.get(remote_ev.sample.remote())
     result = collect_metrics(ev, [remote_ev])
     self.assertEqual(result["episodes_this_iter"], 20)
     self.assertEqual(result["episode_reward_mean"], 10)
開發者ID:jamescasbon,項目名稱:ray,代碼行數:14,代碼來源:test_policy_evaluator.py


注:本文中的ray.rllib.evaluation.policy_evaluator.PolicyEvaluator類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。