forked from facebookresearch/habitat-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Agent Abstraction [Part 1 of Multi-Agent RL] (facebookresearch#1169)
* Added TrainerAgent * VER changes * Consolidated into Agent * Integrated VER trainer * Fixed CI * Added pop play trainer * Refactored agent access interface * Functioning * Pop play running fine * Pre-commit fixes * Updated naming * removed multi-agent * Removed unnecessary MA file * Added docstring * PR comments * Syntax in VER * Addressed PR comments * CI tests * Swap to val for tests * Fixed test
- Loading branch information
Showing
17 changed files
with
790 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import attr | ||
import gym.spaces as spaces | ||
|
||
|
||
@attr.s(auto_attribs=True, slots=True) | ||
class EnvironmentSpec: | ||
""" | ||
Stores information about the spaces of an environment. | ||
:property obs_space: Observation space of the environment. | ||
:property action_space: The potentially flattened version of the environment action space. | ||
:property orig_action_space: The non-flattened version of the environment action space. | ||
""" | ||
|
||
observation_space: spaces.Space | ||
action_space: spaces.Space | ||
orig_action_space: spaces.Space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import abc | ||
|
||
import torch | ||
|
||
|
||
class Storage(abc.ABC): | ||
""" | ||
Storage interface. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def insert( | ||
self, | ||
next_observations=None, | ||
next_recurrent_hidden_states=None, | ||
actions=None, | ||
action_log_probs=None, | ||
value_preds=None, | ||
rewards=None, | ||
next_masks=None, | ||
buffer_index: int = 0, | ||
**kwargs, | ||
): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def to(self, device) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def insert_first_observations(self, batch) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def advance_rollout(self, buffer_index: int = 0) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def compute_returns( | ||
self, next_value: torch.Tensor, use_gae: bool, gamma: float, tau: float | ||
) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def after_update(self) -> None: | ||
pass | ||
|
||
def get_last_step(self): | ||
pass | ||
|
||
def get_current_step(self, env_slice, buffer_index): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
habitat-baselines/habitat_baselines/rl/ppo/agent_access_mgr.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple | ||
|
||
import gym.spaces as spaces | ||
|
||
from habitat_baselines.common.env_spec import EnvironmentSpec | ||
from habitat_baselines.common.storage import Storage | ||
from habitat_baselines.rl.ppo.policy import Policy | ||
from habitat_baselines.rl.ppo.updater import Updater | ||
|
||
if TYPE_CHECKING: | ||
from omegaconf import DictConfig | ||
|
||
|
||
class AgentAccessMgr(ABC): | ||
""" | ||
Consists of: | ||
- Policy: How actions are selected from observations. | ||
- Data Storage: How data collected from the environment is stored. | ||
- Updater: How the Policy is updated. | ||
""" | ||
|
||
@abstractmethod | ||
def __init__( | ||
self, | ||
config: "DictConfig", | ||
env_spec: EnvironmentSpec, | ||
is_distrib: bool, | ||
device, | ||
resume_state: Optional[Dict[str, Any]], | ||
num_envs: int, | ||
percent_done_fn: Callable[[], float], | ||
lr_schedule_fn: Optional[Callable[[float], float]] = None, | ||
): | ||
pass | ||
|
||
@property | ||
@abstractmethod | ||
def nbuffers(self) -> int: | ||
""" | ||
Number of storage buffers. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def post_init(self, create_rollouts_fn: Optional[Callable] = None) -> None: | ||
""" | ||
Called after the constructor. Sets up the rollout storage. | ||
:param create_rollouts_fn: Override behavior for creating the | ||
rollout storage. Default behavior for this and the call signature is | ||
`default_create_rollouts`. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@property | ||
@abstractmethod | ||
def policy_action_space(self) -> spaces.Space: | ||
""" | ||
The action space the policy acts in. This can be different from the | ||
environment action space for hierarchical policies. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@property | ||
@abstractmethod | ||
def rollouts(self) -> Storage: | ||
""" | ||
Gets the current rollout storage. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@property | ||
@abstractmethod | ||
def actor_critic(self) -> Policy: | ||
""" | ||
Gets the current policy | ||
""" | ||
raise NotImplementedError() | ||
|
||
@property | ||
@abstractmethod | ||
def updater(self) -> Updater: | ||
""" | ||
Gets the current policy updater. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def get_resume_state(self) -> Dict[str, Any]: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def get_save_state(self) -> Dict[str, Any]: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def eval(self) -> None: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def train(self) -> None: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def load_ckpt_state_dict(self, ckpt: Dict) -> None: | ||
""" | ||
Loads a state dict for evaluation. The difference from | ||
`load_state_dict` is that this will not load the policy state if the | ||
policy does not request it. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def load_state_dict(self, state: Dict) -> None: | ||
raise NotImplementedError() | ||
|
||
@property | ||
@abstractmethod | ||
def hidden_state_shape(self) -> Tuple[int]: | ||
""" | ||
The shape of the tensor to track the hidden state, such as the RNN hidden state. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def after_update(self) -> None: | ||
""" | ||
Must be called by the trainer after the updater has called `update` and | ||
the rollout `after_update` is called. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def pre_rollout(self) -> None: | ||
""" | ||
Called before a rollout is collected. | ||
""" | ||
raise NotImplementedError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.