2023-09-20 09:29:34 +02:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from collections.abc import Sequence
|
2023-09-26 15:35:18 +02:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import TypeAlias
|
2023-09-19 18:53:11 +02:00
|
|
|
|
2023-09-20 09:29:34 +02:00
|
|
|
import numpy as np
|
2023-09-19 18:53:11 +02:00
|
|
|
import torch
|
|
|
|
from torch import nn
|
|
|
|
|
2023-09-21 12:36:27 +02:00
|
|
|
from tianshou.highlevel.env import Environments, EnvType
|
2023-09-26 15:35:18 +02:00
|
|
|
from tianshou.highlevel.optim import OptimizerFactory
|
|
|
|
from tianshou.utils.net import continuous
|
|
|
|
from tianshou.utils.net.common import ActorCritic, Net
|
2023-09-19 18:53:11 +02:00
|
|
|
|
2023-09-26 15:35:18 +02:00
|
|
|
TDevice: TypeAlias = str | int | torch.device
|
2023-09-19 18:53:11 +02:00
|
|
|
|
|
|
|
|
2023-09-20 09:29:34 +02:00
|
|
|
def init_linear_orthogonal(module: torch.nn.Module):
|
|
|
|
"""Applies orthogonal initialization to linear layers of the given module and sets bias weights to 0.
|
2023-09-19 18:53:11 +02:00
|
|
|
|
2023-09-20 09:29:34 +02:00
|
|
|
:param module: the module whose submodules are to be processed
|
2023-09-19 18:53:11 +02:00
|
|
|
"""
|
2023-09-20 09:29:34 +02:00
|
|
|
for m in module.modules():
|
2023-09-19 18:53:11 +02:00
|
|
|
if isinstance(m, torch.nn.Linear):
|
|
|
|
torch.nn.init.orthogonal_(m.weight, gain=np.sqrt(2))
|
|
|
|
torch.nn.init.zeros_(m.bias)
|
|
|
|
|
|
|
|
|
2023-09-26 15:35:18 +02:00
|
|
|
class ContinuousActorType:
|
|
|
|
GAUSSIAN = "gaussian"
|
|
|
|
DETERMINISTIC = "deterministic"
|
|
|
|
|
|
|
|
|
2023-09-19 18:53:11 +02:00
|
|
|
class ActorFactory(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def create_module(self, envs: Environments, device: TDevice) -> nn.Module:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _init_linear(actor: torch.nn.Module):
|
2023-09-20 09:29:34 +02:00
|
|
|
"""Initializes linear layers of an actor module using default mechanisms.
|
|
|
|
|
|
|
|
:param module: the actor module.
|
2023-09-19 18:53:11 +02:00
|
|
|
"""
|
|
|
|
init_linear_orthogonal(actor)
|
|
|
|
if hasattr(actor, "mu"):
|
|
|
|
# For continuous action spaces with Gaussian policies
|
|
|
|
# do last policy layer scaling, this will make initial actions have (close to)
|
|
|
|
# 0 mean and std, and will help boost performances,
|
|
|
|
# see https://arxiv.org/abs/2006.05990, Fig.24 for details
|
|
|
|
for m in actor.mu.modules():
|
|
|
|
if isinstance(m, torch.nn.Linear):
|
|
|
|
m.weight.data.copy_(0.01 * m.weight.data)
|
|
|
|
|
|
|
|
|
2023-09-21 12:36:27 +02:00
|
|
|
class DefaultActorFactory(ActorFactory):
|
2023-09-26 15:35:18 +02:00
|
|
|
"""An actor factory which, depending on the type of environment, creates a suitable MLP-based policy."""
|
|
|
|
|
2023-09-21 12:36:27 +02:00
|
|
|
DEFAULT_HIDDEN_SIZES = (64, 64)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-09-26 15:35:18 +02:00
|
|
|
continuous_actor_type: ContinuousActorType,
|
2023-09-21 12:36:27 +02:00
|
|
|
hidden_sizes: Sequence[int] = DEFAULT_HIDDEN_SIZES,
|
|
|
|
continuous_unbounded=False,
|
|
|
|
continuous_conditioned_sigma=False,
|
|
|
|
):
|
2023-09-26 15:35:18 +02:00
|
|
|
self.continuous_actor_type = continuous_actor_type
|
2023-09-21 12:36:27 +02:00
|
|
|
self.continuous_unbounded = continuous_unbounded
|
|
|
|
self.continuous_conditioned_sigma = continuous_conditioned_sigma
|
|
|
|
self.hidden_sizes = hidden_sizes
|
|
|
|
|
|
|
|
def create_module(self, envs: Environments, device: TDevice) -> nn.Module:
|
|
|
|
env_type = envs.get_type()
|
|
|
|
if env_type == EnvType.CONTINUOUS:
|
2023-09-26 15:35:18 +02:00
|
|
|
match self.continuous_actor_type:
|
|
|
|
case ContinuousActorType.GAUSSIAN:
|
|
|
|
factory = ContinuousActorFactoryGaussian(
|
|
|
|
self.hidden_sizes,
|
|
|
|
unbounded=self.continuous_unbounded,
|
|
|
|
conditioned_sigma=self.continuous_conditioned_sigma,
|
|
|
|
)
|
|
|
|
case ContinuousActorType.DETERMINISTIC:
|
|
|
|
factory = ContinuousActorFactoryDeterministic(self.hidden_sizes)
|
|
|
|
case _:
|
|
|
|
raise ValueError(self.continuous_actor_type)
|
2023-09-21 12:36:27 +02:00
|
|
|
return factory.create_module(envs, device)
|
|
|
|
elif env_type == EnvType.DISCRETE:
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
|
|
|
raise ValueError(f"{env_type} not supported")
|
|
|
|
|
|
|
|
|
2023-09-19 18:53:11 +02:00
|
|
|
class ContinuousActorFactory(ActorFactory, ABC):
|
2023-09-21 12:36:27 +02:00
|
|
|
"""Serves as a type bound for actor factories that are suitable for continuous action spaces."""
|
|
|
|
|
2023-09-19 18:53:11 +02:00
|
|
|
|
2023-09-26 15:35:18 +02:00
|
|
|
class ContinuousActorFactoryDeterministic(ContinuousActorFactory):
|
|
|
|
def __init__(self, hidden_sizes: Sequence[int]):
|
|
|
|
self.hidden_sizes = hidden_sizes
|
|
|
|
|
|
|
|
def create_module(self, envs: Environments, device: TDevice) -> nn.Module:
|
|
|
|
net_a = Net(
|
|
|
|
envs.get_observation_shape(),
|
|
|
|
hidden_sizes=self.hidden_sizes,
|
|
|
|
device=device,
|
|
|
|
)
|
|
|
|
return continuous.Actor(
|
|
|
|
net_a,
|
|
|
|
envs.get_action_shape(),
|
|
|
|
hidden_sizes=(),
|
|
|
|
device=device,
|
|
|
|
).to(device)
|
|
|
|
|
2023-09-19 18:53:11 +02:00
|
|
|
|
2023-09-26 15:35:18 +02:00
|
|
|
class ContinuousActorFactoryGaussian(ContinuousActorFactory):
|
2023-09-20 09:29:34 +02:00
|
|
|
def __init__(self, hidden_sizes: Sequence[int], unbounded=True, conditioned_sigma=False):
|
2023-09-19 18:53:11 +02:00
|
|
|
self.hidden_sizes = hidden_sizes
|
2023-09-20 09:29:34 +02:00
|
|
|
self.unbounded = unbounded
|
|
|
|
self.conditioned_sigma = conditioned_sigma
|
2023-09-19 18:53:11 +02:00
|
|
|
|
|
|
|
def create_module(self, envs: Environments, device: TDevice) -> nn.Module:
|
|
|
|
net_a = Net(
|
2023-09-25 17:56:37 +02:00
|
|
|
envs.get_observation_shape(),
|
2023-09-20 09:29:34 +02:00
|
|
|
hidden_sizes=self.hidden_sizes,
|
|
|
|
activation=nn.Tanh,
|
|
|
|
device=device,
|
2023-09-19 18:53:11 +02:00
|
|
|
)
|
2023-09-26 15:35:18 +02:00
|
|
|
actor = continuous.ActorProb(
|
2023-09-20 09:29:34 +02:00
|
|
|
net_a,
|
|
|
|
envs.get_action_shape(),
|
2023-09-20 13:15:06 +02:00
|
|
|
unbounded=self.unbounded,
|
2023-09-20 09:29:34 +02:00
|
|
|
device=device,
|
|
|
|
conditioned_sigma=self.conditioned_sigma,
|
|
|
|
).to(device)
|
2023-09-19 18:53:11 +02:00
|
|
|
|
|
|
|
# init params
|
2023-09-20 09:29:34 +02:00
|
|
|
if not self.conditioned_sigma:
|
|
|
|
torch.nn.init.constant_(actor.sigma_param, -0.5)
|
2023-09-19 18:53:11 +02:00
|
|
|
self._init_linear(actor)
|
|
|
|
|
|
|
|
return actor
|
|
|
|
|
|
|
|
|
|
|
|
class CriticFactory(ABC):
|
|
|
|
@abstractmethod
|
2023-09-20 09:29:34 +02:00
|
|
|
def create_module(self, envs: Environments, device: TDevice, use_action: bool) -> nn.Module:
|
2023-09-19 18:53:11 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-09-21 12:36:27 +02:00
|
|
|
class DefaultCriticFactory(CriticFactory):
|
|
|
|
"""A critic factory which, depending on the type of environment, creates a suitable MLP-based critic."""
|
|
|
|
|
|
|
|
DEFAULT_HIDDEN_SIZES = (64, 64)
|
|
|
|
|
|
|
|
def __init__(self, hidden_sizes: Sequence[int] = DEFAULT_HIDDEN_SIZES):
|
|
|
|
self.hidden_sizes = hidden_sizes
|
|
|
|
|
|
|
|
def create_module(self, envs: Environments, device: TDevice, use_action: bool) -> nn.Module:
|
|
|
|
env_type = envs.get_type()
|
|
|
|
if env_type == EnvType.CONTINUOUS:
|
|
|
|
factory = ContinuousNetCriticFactory(self.hidden_sizes)
|
|
|
|
return factory.create_module(envs, device, use_action)
|
|
|
|
elif env_type == EnvType.DISCRETE:
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
|
|
|
raise ValueError(f"{env_type} not supported")
|
|
|
|
|
|
|
|
|
2023-09-19 18:53:11 +02:00
|
|
|
class ContinuousCriticFactory(CriticFactory, ABC):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ContinuousNetCriticFactory(ContinuousCriticFactory):
|
2023-09-21 12:36:27 +02:00
|
|
|
def __init__(self, hidden_sizes: Sequence[int]):
|
2023-09-19 18:53:11 +02:00
|
|
|
self.hidden_sizes = hidden_sizes
|
|
|
|
|
2023-09-20 09:29:34 +02:00
|
|
|
def create_module(self, envs: Environments, device: TDevice, use_action: bool) -> nn.Module:
|
|
|
|
action_shape = envs.get_action_shape() if use_action else 0
|
2023-09-19 18:53:11 +02:00
|
|
|
net_c = Net(
|
2023-09-25 17:56:37 +02:00
|
|
|
envs.get_observation_shape(),
|
2023-09-20 09:29:34 +02:00
|
|
|
action_shape=action_shape,
|
|
|
|
hidden_sizes=self.hidden_sizes,
|
|
|
|
concat=use_action,
|
|
|
|
activation=nn.Tanh,
|
|
|
|
device=device,
|
2023-09-19 18:53:11 +02:00
|
|
|
)
|
2023-09-26 15:35:18 +02:00
|
|
|
critic = continuous.Critic(net_c, device=device).to(device)
|
2023-09-19 18:53:11 +02:00
|
|
|
init_linear_orthogonal(critic)
|
|
|
|
return critic
|
2023-09-26 15:35:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ModuleOpt:
|
|
|
|
module: torch.nn.Module
|
|
|
|
optim: torch.optim.Optimizer
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ActorCriticModuleOpt:
|
|
|
|
actor_critic_module: ActorCritic
|
|
|
|
optim: torch.optim.Optimizer
|
|
|
|
|
|
|
|
@property
|
|
|
|
def actor(self):
|
|
|
|
return self.actor_critic_module.actor
|
|
|
|
|
|
|
|
@property
|
|
|
|
def critic(self):
|
|
|
|
return self.actor_critic_module.critic
|
|
|
|
|
|
|
|
|
|
|
|
class ActorModuleOptFactory:
|
|
|
|
def __init__(self, actor_factory: ActorFactory, optim_factory: OptimizerFactory):
|
|
|
|
self.actor_factory = actor_factory
|
|
|
|
self.optim_factory = optim_factory
|
|
|
|
|
|
|
|
def create_module_opt(self, envs: Environments, device: TDevice, lr: float) -> ModuleOpt:
|
|
|
|
actor = self.actor_factory.create_module(envs, device)
|
|
|
|
opt = self.optim_factory.create_optimizer(actor, lr)
|
|
|
|
return ModuleOpt(actor, opt)
|
|
|
|
|
|
|
|
|
|
|
|
class CriticModuleOptFactory:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
critic_factory: CriticFactory,
|
|
|
|
optim_factory: OptimizerFactory,
|
|
|
|
use_action: bool,
|
|
|
|
):
|
|
|
|
self.critic_factory = critic_factory
|
|
|
|
self.optim_factory = optim_factory
|
|
|
|
self.use_action = use_action
|
|
|
|
|
|
|
|
def create_module_opt(self, envs: Environments, device: TDevice, lr: float) -> ModuleOpt:
|
|
|
|
critic = self.critic_factory.create_module(envs, device, self.use_action)
|
|
|
|
opt = self.optim_factory.create_optimizer(critic, lr)
|
|
|
|
return ModuleOpt(critic, opt)
|