-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from lajd/merge-to-master
Merge to master
- Loading branch information
Showing
145 changed files
with
4,849 additions
and
2,088 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
**/ray_tunings/ | ||
**/visual_tunings/ | ||
**/environments/ | ||
**/unity-environment.log | ||
**/unity-environment.log |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,65 +1,52 @@ | ||
from abc import abstractmethod | ||
import torch | ||
import numpy as np | ||
from typing import Tuple | ||
from agents.policies.base_policy import Policy | ||
from torch.optim.lr_scheduler import _LRScheduler | ||
from tools.rl_constants import Experience, Action | ||
from typing import Tuple, Union | ||
from tools.rl_constants import Experience, ExperienceBatch, BrainSet, Action | ||
from tools.parameter_capture import ParameterCapture | ||
|
||
|
||
class Agent(torch.nn.Module): | ||
class Agent: | ||
""" An agent which received state & reward from, and interacts with, and environment""" | ||
def __init__(self, state_shape: Tuple[int, ...], action_size: int, policy: Policy, optimizer: torch.optim.Optimizer, lr_scheduler: _LRScheduler): | ||
super().__init__() | ||
def __init__(self, state_shape: Union[Tuple[int, ...], int], action_size: int): | ||
self.state_shape = state_shape | ||
self.action_size = action_size | ||
|
||
self.policy: Policy = policy | ||
self.optimizer: optimizer = optimizer | ||
self.lr_scheduler: _LRScheduler = lr_scheduler | ||
|
||
self.warmup = False | ||
self.t_step = 0 | ||
self.episode_counter = 0 | ||
self.param_capture = ParameterCapture() | ||
self.training = True | ||
|
||
def set_mode(self, mode: str): | ||
""" Set the mode of the agent """ | ||
if mode == 'train': | ||
self.train() | ||
self.policy.train = True | ||
elif mode.startswith('eval'): | ||
self.eval() | ||
self.policy.eval() # Make the policy greedy | ||
else: | ||
raise ValueError("only modes `train`, `evaluate` are supported") | ||
|
||
def preprocess_state(self, state: torch.Tensor): | ||
return state | ||
def set_warmup(self, warmup: bool): | ||
self.warmup = warmup | ||
|
||
@abstractmethod | ||
def save(self, *args, **kwargs) -> dict: | ||
"""Save the agent model""" | ||
def set_mode(self, mode: str): | ||
pass | ||
|
||
@abstractmethod | ||
def load(self, *args, **kwargs): | ||
""" Load the agent model """ | ||
pass | ||
def preprocess_state(self, state): | ||
""" Perform any state preprocessing """ | ||
return state | ||
|
||
@abstractmethod | ||
def get_action(self, state: np.array) -> Action: | ||
def get_action(self, state: np.array, *args, **kwargs) -> Action: | ||
"""Determine an action given an environment state""" | ||
pass | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_random_action(self, *args) -> Action: | ||
pass | ||
def get_random_action(self, *args, **kwargs) -> Action: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def step(self, experience: Experience, **kwargs) -> None: | ||
"""Take a step in the environment, encompassing model learning and memory population""" | ||
pass | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def step_episode(self, episode: int) -> None: | ||
def step_episode(self, episode: int, *args) -> None: | ||
"""Perform any end-of-episode updates""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def learn(self, experience_batch: ExperienceBatch): | ||
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
Oops, something went wrong.