Actor-Critic

Classical Actor-Critic Methods

class COPDAC_Q(mdp_info, policy, mu, alpha_theta, alpha_omega, alpha_v, value_function_features=None)[source]

Bases: Agent

Compatible off-policy deterministic actor-critic algorithm. “Deterministic Policy Gradient Algorithms”. Silver D. et al.. 2014.

__init__(mdp_info, policy, mu, alpha_theta, alpha_omega, alpha_v, value_function_features=None)[source]

Constructor.

Parameters:
  • mu (Regressor) – regressor that describe the deterministic policy to be learned i.e., the deterministic mapping between state and action.

  • alpha_theta ([float, Parameter]) – learning rate for policy update;

  • alpha_omega ([float, Parameter]) – learning rate for the advantage function;

  • alpha_v ([float, Parameter]) – learning rate for the value function;

  • value_function_features (Features, None) – features used by the value function approximator;

fit(dataset)[source]

Fit step.

Parameters:

dataset (Dataset) – the dataset.

_add_save_attr(**attr_dict)

Add attributes that should be saved for an agent. For every attribute, it is necessary to specify the method to be used to save and load. Available methods are: numpy, mushroom, torch, json, pickle, primitive and none. The primitive method can be used to store primitive attributes, while the none method always skip the attribute, but ensure that it is initialized to None after the load. The mushroom method can be used with classes that implement the Serializable interface. All the other methods use the library named. If a “!” character is added at the end of the method, the field will be saved only if full_save is set to True.

Parameters:

**attr_dict – dictionary of attributes mapped to the method that should be used to save and load them.

_agent_preprocess(state)

Applies all the agent’s preprocessors to the state.

Parameters:

state (Array) – the state where the agent is;

Returns:

The preprocessed state.

_post_load()

This method can be overwritten to implement logic that is executed after the loading of the agent.

_update_agent_preprocessor(state)

Updates the stats of all the agent’s preprocessors given the state.

Parameters:

state (Array) – the state where the agent is;

add_agent_preprocessor(preprocessor)

Add preprocessor to the agent’s preprocessor list. The preprocessors are applied in order.

Parameters:

preprocessor (object) – state preprocessors to be applied to state variables before feeding them to the agent.

add_core_preprocessor(preprocessor)

Add preprocessor to the core’s preprocessor list. The preprocessors are applied in order.

Parameters:

preprocessor (object) – state preprocessors to be applied to state variables before feeding them to the agent.

copy()
Returns:

A deepcopy of the agent.

property core_preprocessors

Access to core’s state preprocessors stored in the agent.

draw_action(state, policy_state=None)

Return the action to execute in the given state. It is the action returned by the policy or the action set by the algorithm (e.g. in the case of SARSA).

Parameters:
  • state – the state where the agent is;

  • policy_state – the policy internal state.

Returns:

The action to be executed.

episode_start(initial_state, episode_info)

Called by the Core when a new episode starts.

Parameters:
  • initial_state (Array) – vector representing the initial state of the environment.

  • episode_info (dict) – a dictionary containing the information at reset, such as context.

Returns:

A tuple containing the policy initial state and, optionally, the policy parameters

episode_start_vectorized(initial_states, episode_info, start_mask)

Called by the VectorCore when a new episode starts.

Parameters:
  • initial_states (Array) – the initial states of the environment.

  • episode_info (dict) – a dictionary containing the information at reset, such as context;

  • start_mask (Array) – boolean mask to select the environments that are starting a new episode

Returns:

A tuple containing the policy initial states and, optionally, the policy parameters

classmethod load(path)

Load and deserialize the agent from the given location on disk.

Parameters:

path (Path, string) – Relative or absolute path to the agents save location.

Returns:

The loaded agent.

save(path, full_save=False)

Serialize and save the object to the given path on disk.

Parameters:
  • path (Path, str) – Relative or absolute path to the object save location;

  • full_save (bool) – Flag to specify the amount of data to save for MushroomRL data structures.

save_zip(zip_file, full_save, folder='')

Serialize and save the agent to the given path on disk.

Parameters:
  • zip_file (ZipFile) – ZipFile where te object needs to be saved;

  • full_save (bool) – flag to specify the amount of data to save for MushroomRL data structures;

  • folder (string, '') – subfolder to be used by the save method.

set_logger(logger)

Setter that can be used to pass a logger to the algorithm

Parameters:

logger (Logger) – the logger to be used by the algorithm.

stop()

Method used to stop an agent. Useful when dealing with real world environments, simulators, or to cleanup environments internals after a core learn/evaluate to enforce consistency.

class StochasticAC(mdp_info, policy, alpha_theta, alpha_v, lambda_par=0.9, value_function_features=None)[source]

Bases: Agent

Stochastic Actor critic in the episodic setting as presented in: “Model-Free Reinforcement Learning with Continuous Action in Practice”. Degris T. et al.. 2012.

__init__(mdp_info, policy, alpha_theta, alpha_v, lambda_par=0.9, value_function_features=None)[source]

Constructor.

Parameters:
  • alpha_theta ([float, Parameter]) – learning rate for policy update;

  • alpha_v ([float, Parameter]) – learning rate for the value function;

  • lambda_par ([float, Parameter], .9) – trace decay parameter;

  • value_function_features (Features, None) – features used by the value function approximator.

episode_start(initial_state, episode_info)[source]

Called by the Core when a new episode starts.

Parameters:
  • initial_state (Array) – vector representing the initial state of the environment.

  • episode_info (dict) – a dictionary containing the information at reset, such as context.

Returns:

A tuple containing the policy initial state and, optionally, the policy parameters

fit(dataset)[source]

Fit step.

Parameters:

dataset (Dataset) – the dataset.

_add_save_attr(**attr_dict)

Add attributes that should be saved for an agent. For every attribute, it is necessary to specify the method to be used to save and load. Available methods are: numpy, mushroom, torch, json, pickle, primitive and none. The primitive method can be used to store primitive attributes, while the none method always skip the attribute, but ensure that it is initialized to None after the load. The mushroom method can be used with classes that implement the Serializable interface. All the other methods use the library named. If a “!” character is added at the end of the method, the field will be saved only if full_save is set to True.

Parameters:

**attr_dict – dictionary of attributes mapped to the method that should be used to save and load them.

_agent_preprocess(state)

Applies all the agent’s preprocessors to the state.

Parameters:

state (Array) – the state where the agent is;

Returns:

The preprocessed state.

_post_load()

This method can be overwritten to implement logic that is executed after the loading of the agent.

_update_agent_preprocessor(state)

Updates the stats of all the agent’s preprocessors given the state.

Parameters:

state (Array) – the state where the agent is;

add_agent_preprocessor(preprocessor)

Add preprocessor to the agent’s preprocessor list. The preprocessors are applied in order.

Parameters:

preprocessor (object) – state preprocessors to be applied to state variables before feeding them to the agent.

add_core_preprocessor(preprocessor)

Add preprocessor to the core’s preprocessor list. The preprocessors are applied in order.

Parameters:

preprocessor (object) – state preprocessors to be applied to state variables before feeding them to the agent.

copy()
Returns:

A deepcopy of the agent.

property core_preprocessors

Access to core’s state preprocessors stored in the agent.

draw_action(state, policy_state=None)

Return the action to execute in the given state. It is the action returned by the policy or the action set by the algorithm (e.g. in the case of SARSA).

Parameters:
  • state – the state where the agent is;

  • policy_state – the policy internal state.

Returns:

The action to be executed.

episode_start_vectorized(initial_states, episode_info, start_mask)

Called by the VectorCore when a new episode starts.

Parameters:
  • initial_states (Array) – the initial states of the environment.

  • episode_info (dict) – a dictionary containing the information at reset, such as context;

  • start_mask (Array) – boolean mask to select the environments that are starting a new episode

Returns:

A tuple containing the policy initial states and, optionally, the policy parameters

classmethod load(path)

Load and deserialize the agent from the given location on disk.

Parameters:

path (Path, string) – Relative or absolute path to the agents save location.

Returns:

The loaded agent.

save(path, full_save=False)

Serialize and save the object to the given path on disk.

Parameters:
  • path (Path, str) – Relative or absolute path to the object save location;

  • full_save (bool) – Flag to specify the amount of data to save for MushroomRL data structures.

save_zip(zip_file, full_save, folder='')

Serialize and save the agent to the given path on disk.

Parameters:
  • zip_file (ZipFile) – ZipFile where te object needs to be saved;

  • full_save (bool) – flag to specify the amount of data to save for MushroomRL data structures;

  • folder (string, '') – subfolder to be used by the save method.

set_logger(logger)

Setter that can be used to pass a logger to the algorithm

Parameters:

logger (Logger) – the logger to be used by the algorithm.

stop()

Method used to stop an agent. Useful when dealing with real world environments, simulators, or to cleanup environments internals after a core learn/evaluate to enforce consistency.

class StochasticAC_AVG(mdp_info, policy, alpha_theta, alpha_v, alpha_r, lambda_par=0.9, value_function_features=None)[source]

Bases: StochasticAC

Stochastic Actor critic in the average reward setting as presented in: “Model-Free Reinforcement Learning with Continuous Action in Practice”. Degris T. et al.. 2012.

__init__(mdp_info, policy, alpha_theta, alpha_v, alpha_r, lambda_par=0.9, value_function_features=None)[source]

Constructor.

Parameters:

alpha_r (Parameter) – learning rate for the reward trace.

_add_save_attr(**attr_dict)

Add attributes that should be saved for an agent. For every attribute, it is necessary to specify the method to be used to save and load. Available methods are: numpy, mushroom, torch, json, pickle, primitive and none. The primitive method can be used to store primitive attributes, while the none method always skip the attribute, but ensure that it is initialized to None after the load. The mushroom method can be used with classes that implement the Serializable interface. All the other methods use the library named. If a “!” character is added at the end of the method, the field will be saved only if full_save is set to True.

Parameters:

**attr_dict – dictionary of attributes mapped to the method that should be used to save and load them.

_agent_preprocess(state)

Applies all the agent’s preprocessors to the state.

Parameters:

state (Array) – the state where the agent is;

Returns:

The preprocessed state.

_post_load()

This method can be overwritten to implement logic that is executed after the loading of the agent.

_update_agent_preprocessor(state)

Updates the stats of all the agent’s preprocessors given the state.

Parameters:

state (Array) – the state where the agent is;

add_agent_preprocessor(preprocessor)

Add preprocessor to the agent’s preprocessor list. The preprocessors are applied in order.

Parameters:

preprocessor (object) – state preprocessors to be applied to state variables before feeding them to the agent.

add_core_preprocessor(preprocessor)

Add preprocessor to the core’s preprocessor list. The preprocessors are applied in order.

Parameters:

preprocessor (object) – state preprocessors to be applied to state variables before feeding them to the agent.

copy()
Returns:

A deepcopy of the agent.

property core_preprocessors

Access to core’s state preprocessors stored in the agent.

draw_action(state, policy_state=None)

Return the action to execute in the given state. It is the action returned by the policy or the action set by the algorithm (e.g. in the case of SARSA).

Parameters:
  • state – the state where the agent is;

  • policy_state – the policy internal state.

Returns:

The action to be executed.

episode_start(initial_state, episode_info)

Called by the Core when a new episode starts.

Parameters:
  • initial_state (Array) – vector representing the initial state of the environment.

  • episode_info (dict) – a dictionary containing the information at reset, such as context.

Returns:

A tuple containing the policy initial state and, optionally, the policy parameters

episode_start_vectorized(initial_states, episode_info, start_mask)

Called by the VectorCore when a new episode starts.

Parameters:
  • initial_states (Array) – the initial states of the environment.

  • episode_info (dict) – a dictionary containing the information at reset, such as context;

  • start_mask (Array) – boolean mask to select the environments that are starting a new episode

Returns:

A tuple containing the policy initial states and, optionally, the policy parameters

fit(dataset)

Fit step.

Parameters:

dataset (Dataset) – the dataset.

classmethod load(path)

Load and deserialize the agent from the given location on disk.

Parameters:

path (Path, string) – Relative or absolute path to the agents save location.

Returns:

The loaded agent.

save(path, full_save=False)

Serialize and save the object to the given path on disk.

Parameters:
  • path (Path, str) – Relative or absolute path to the object save location;

  • full_save (bool) – Flag to specify the amount of data to save for MushroomRL data structures.

save_zip(zip_file, full_save, folder='')

Serialize and save the agent to the given path on disk.

Parameters:
  • zip_file (ZipFile) – ZipFile where te object needs to be saved;

  • full_save (bool) – flag to specify the amount of data to save for MushroomRL data structures;

  • folder (string, '') – subfolder to be used by the save method.

set_logger(logger)

Setter that can be used to pass a logger to the algorithm

Parameters:

logger (Logger) – the logger to be used by the algorithm.

stop()

Method used to stop an agent. Useful when dealing with real world environments, simulators, or to cleanup environments internals after a core learn/evaluate to enforce consistency.

Deep Actor-Critic Methods