<commit_before>import numpy as np

import torch
import torch.optim as optim

from mushroom.algorithms.agent import Agent
from mushroom.policy import Policy
from mushroom.approximators import Regressor
from mushroom.approximators.parametric import TorchApproximator
from mushroom.utils.replay_memory import ReplayMemory

from copy import deepcopy
from itertools import chain


class ReparametrizationAC(Agent):
    """
    Base class for algorithms that uses the reparametrization trick, such as
    SAC, DDPG and TD3.

    """
    def __init__(self, policy, mdp_info, actor_optimizer, parameters):
        if actor_optimizer is not None:
            self._optimizer = actor_optimizer['class'](parameters,
                                                       **actor_optimizer['params'])

        super().__init__(policy, mdp_info)

    def _optimize_actor_parameters(self, loss):
        """
        Method used to update actor parameters to maximize a given loss.

        Args:
            loss (torch.tensor): the loss computed by the algorithm.

        """
        self._optimizer.zero_grad()
        loss.backward()
        self._optimizer.step()


class DDPG(ReparametrizationAC):
    """
    Deep Deterministic Policy Gradient algorithm.<commit_msg>- a2c stub<commit_after>import numpy as np

import torch
import torch.optim as optim

from mushroom.algorithms.agent import Agent
from mushroom.policy import Policy
from mushroom.approximators import Regressor
from mushroom.approximators.parametric import TorchApproximator
from mushroom.utils.replay_memory import ReplayMemory

from copy import deepcopy
from itertools import chain


class ReparametrizationAC(Agent):
    """
    Base class for algorithms that uses the reparametrization trick, such as
    SAC, DDPG and TD3.

    """
    def __init__(self, policy, mdp_info, actor_optimizer, parameters):
        if actor_optimizer is not None:
            self._optimizer = actor_optimizer['class'](parameters,
                                                       **actor_optimizer['params'])

        super().__init__(policy, mdp_info)

    def _optimize_actor_parameters(self, loss):
        """
        Method used to update actor parameters to maximize a given loss.

        Args:
            loss (torch.tensor): the loss computed by the algorithm.

        """
        self._optimizer.zero_grad()
        loss.backward()
        self._optimizer.step()


class A2C(ReparametrizationAC):
    """
    Advantage Actor Critic algorithm.

    """


class DDPG(ReparametrizationAC):
    """
    Deep Deterministic Policy Gradient algorithm.
{'fraction_non_alphanumeric': 0.0718125960061444, 'fraction_numerical': 0.0015360983102918587, 'mean_word_length': 2.842182890855457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6957416', 'n_tokens_mistral': 714, 'n_tokens_neox': 675, 'n_words': 215}