This PR adds a new method for getting actions from an env's observation and info. This is useful for standard inference and stands in contrast to batch-based methods that are currently used in training and evaluation. Without this, users have to do some kind of gymnastics to actually perform inference with a trained policy. I have also added a test for the new method. In future PRs, this method should be included in the examples (in the the "watch" section). To add this required improving multiple typing things and, importantly, _simplifying the signature of `forward` in many policies!_ This is a **breaking change**, but it will likely affect no users. The `input` parameter of forward was a rather hacky mechanism, I believe it is good that it's gone now. It will also help with #948 . The main functional change is the addition of `compute_action` to `BasePolicy`. Other minor changes: - improvements in typing - updated PR and Issue templates - Improved handling of `max_action_num` Closes #981
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from typing import Any, cast
|
|
|
|
import numpy as np
|
|
|
|
from tianshou.data import Batch
|
|
from tianshou.data.batch import BatchProtocol
|
|
from tianshou.data.types import ActBatchProtocol, ObsBatchProtocol, RolloutBatchProtocol
|
|
from tianshou.policy import BasePolicy
|
|
|
|
|
|
class RandomPolicy(BasePolicy):
|
|
"""A random agent used in multi-agent learning.
|
|
|
|
It randomly chooses an action from the legal action.
|
|
"""
|
|
|
|
def forward(
|
|
self,
|
|
batch: ObsBatchProtocol,
|
|
state: dict | BatchProtocol | np.ndarray | None = None,
|
|
**kwargs: Any,
|
|
) -> ActBatchProtocol:
|
|
"""Compute the random action over the given batch data.
|
|
|
|
The input should contain a mask in batch.obs, with "True" to be
|
|
available and "False" to be unavailable. For example,
|
|
``batch.obs.mask == np.array([[False, True, False]])`` means with batch
|
|
size 1, action "1" is available but action "0" and "2" are unavailable.
|
|
|
|
:return: A :class:`~tianshou.data.Batch` with "act" key, containing
|
|
the random action.
|
|
|
|
.. seealso::
|
|
|
|
Please refer to :meth:`~tianshou.policy.BasePolicy.forward` for
|
|
more detailed explanation.
|
|
"""
|
|
mask = batch.obs.mask # type: ignore
|
|
logits = np.random.rand(*mask.shape)
|
|
logits[~mask] = -np.inf
|
|
result = Batch(act=logits.argmax(axis=-1))
|
|
return cast(ActBatchProtocol, result)
|
|
|
|
def learn(self, batch: RolloutBatchProtocol, *args: Any, **kwargs: Any) -> dict[str, float]:
|
|
"""Since a random agent learns nothing, it returns an empty dict."""
|
|
return {}
|