This PR adds strict typing to the output of `update` and `learn` in all policies. This will likely be the last large refactoring PR before the next release (0.6.0, not 1.0.0), so it requires some attention. Several difficulties were encountered on the path to that goal: 1. The policy hierarchy is actually "broken" in the sense that the keys of dicts that were output by `learn` did not follow the same enhancement (inheritance) pattern as the policies. This is a real problem and should be addressed in the near future. Generally, several aspects of the policy design and hierarchy might deserve a dedicated discussion. 2. Each policy needs to be generic in the stats return type, because one might want to extend it at some point and then also extend the stats. Even within the source code base this pattern is necessary in many places. 3. The interaction between learn and update is a bit quirky, we currently handle it by having update modify special field inside TrainingStats, whereas all other fields are handled by learn. 4. The IQM module is a policy wrapper and required a TrainingStatsWrapper. The latter relies on a bunch of black magic. They were addressed by: 1. Live with the broken hierarchy, which is now made visible by bounds in generics. We use type: ignore where appropriate. 2. Make all policies generic with bounds following the policy inheritance hierarchy (which is incorrect, see above). We experimented a bit with nested TrainingStats classes, but that seemed to add more complexity and be harder to understand. Unfortunately, mypy thinks that the code below is wrong, wherefore we have to add `type: ignore` to the return of each `learn` ```python T = TypeVar("T", bound=int) def f() -> T: return 3 ``` 3. See above 4. Write representative tests for the `TrainingStatsWrapper`. Still, the black magic might cause nasty surprises down the line (I am not proud of it)... Closes #933 --------- Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de> Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""Policy package."""
|
|
# isort:skip_file
|
|
|
|
from tianshou.policy.base import BasePolicy, TrainingStats
|
|
from tianshou.policy.random import RandomPolicy
|
|
from tianshou.policy.modelfree.dqn import DQNPolicy
|
|
from tianshou.policy.modelfree.bdq import BranchingDQNPolicy
|
|
from tianshou.policy.modelfree.c51 import C51Policy
|
|
from tianshou.policy.modelfree.rainbow import RainbowPolicy
|
|
from tianshou.policy.modelfree.qrdqn import QRDQNPolicy
|
|
from tianshou.policy.modelfree.iqn import IQNPolicy
|
|
from tianshou.policy.modelfree.fqf import FQFPolicy
|
|
from tianshou.policy.modelfree.pg import PGPolicy
|
|
from tianshou.policy.modelfree.a2c import A2CPolicy
|
|
from tianshou.policy.modelfree.npg import NPGPolicy
|
|
from tianshou.policy.modelfree.ddpg import DDPGPolicy
|
|
from tianshou.policy.modelfree.ppo import PPOPolicy
|
|
from tianshou.policy.modelfree.trpo import TRPOPolicy
|
|
from tianshou.policy.modelfree.td3 import TD3Policy
|
|
from tianshou.policy.modelfree.sac import SACPolicy
|
|
from tianshou.policy.modelfree.redq import REDQPolicy
|
|
from tianshou.policy.modelfree.discrete_sac import DiscreteSACPolicy
|
|
from tianshou.policy.imitation.base import ImitationPolicy
|
|
from tianshou.policy.imitation.bcq import BCQPolicy
|
|
from tianshou.policy.imitation.cql import CQLPolicy
|
|
from tianshou.policy.imitation.td3_bc import TD3BCPolicy
|
|
from tianshou.policy.imitation.discrete_bcq import DiscreteBCQPolicy
|
|
from tianshou.policy.imitation.discrete_cql import DiscreteCQLPolicy
|
|
from tianshou.policy.imitation.discrete_crr import DiscreteCRRPolicy
|
|
from tianshou.policy.imitation.gail import GAILPolicy
|
|
from tianshou.policy.modelbased.psrl import PSRLPolicy
|
|
from tianshou.policy.modelbased.icm import ICMPolicy
|
|
from tianshou.policy.multiagent.mapolicy import MultiAgentPolicyManager
|
|
|
|
__all__ = [
|
|
"BasePolicy",
|
|
"RandomPolicy",
|
|
"DQNPolicy",
|
|
"BranchingDQNPolicy",
|
|
"C51Policy",
|
|
"RainbowPolicy",
|
|
"QRDQNPolicy",
|
|
"IQNPolicy",
|
|
"FQFPolicy",
|
|
"PGPolicy",
|
|
"A2CPolicy",
|
|
"NPGPolicy",
|
|
"DDPGPolicy",
|
|
"PPOPolicy",
|
|
"TRPOPolicy",
|
|
"TD3Policy",
|
|
"SACPolicy",
|
|
"REDQPolicy",
|
|
"DiscreteSACPolicy",
|
|
"ImitationPolicy",
|
|
"BCQPolicy",
|
|
"CQLPolicy",
|
|
"TD3BCPolicy",
|
|
"DiscreteBCQPolicy",
|
|
"DiscreteCQLPolicy",
|
|
"DiscreteCRRPolicy",
|
|
"GAILPolicy",
|
|
"PSRLPolicy",
|
|
"ICMPolicy",
|
|
"MultiAgentPolicyManager",
|
|
"TrainingStats",
|
|
]
|