Main Content

rlContinuousDeterministicRewardFunction

Deterministic reward function approximator object for neural network-based environment

Since R2022a

    Description

    When creating a neural network-based environment using rlNeuralNetworkEnvironment, you can specify the reward function approximator using an rlContinuousDeterministicRewardFunction object. Do so when you do not know a ground-truth reward signal for your environment but you expect the reward signal to be deterministic.

    The reward function approximator object uses a deep neural network as internal approximation model to predict the reward signal for the environment given one of the following input combinations.

    • Observations, actions, and next observations

    • Observations and actions

    • Actions and next observations

    • Next observations

    To specify a stochastic reward function, use an rlContinuousGaussianRewardFunction object.

    Creation

    Description

    example

    rwdFcnAppx = rlContinuousDeterministicRewardFunction(net,observationInfo,actionInfo,Name=Value) creates the deterministic reward function approximator object rwdFcnAppx using the deep neural network net and sets the ObservationInfo and ActionInfo properties.

    When creating a reward function you must specify the names of the deep neural network inputs using one of the following combinations of name-value pair arguments.

    You can also specify the UseDevice property using and an optional name-value pair argument. For example, to use a GPU for prediction, specify UseDevice="gpu".

    Input Arguments

    expand all

    Deep neural network with a scalar output value, specified as a dlnetwork object.

    The input layer names for this network must match the input names specified using the ObservationInputNames, ActionInputNames, and NextObservationInputNames. The dimensions of the input layers must match the dimensions of the corresponding observation and action specifications in ObservationInfo and ActionInfo, respectively.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: ObservationInputNames="velocity"

    Observation input layer names, specified as a string or string array. Specify ObservationInputNames when you expect the reward signal to depend on the current environment observation.

    The number of observation input names must match the length of ObservationInfo and the order of the names must match the order of the specifications in ObservationInfo.

    Action input layer names, specified as a string or string array. Specify ActionInputNames when you expect the reward signal to depend on the current action value.

    The number of action input names must match the length of ActionInfo and the order of the names must match the order of the specifications in ActionInfo.

    Next observation input layer names, specified as a string or string array. Specify NextObservationInputNames when you expect the reward signal to depend on the next environment observation.

    The number of next observation input names must match the length of ObservationInfo and the order of the names must match the order of the specifications in ObservationInfo.

    Properties

    expand all

    Observation specifications, specified as an rlNumericSpec object or an array of such objects. Each element in the array defines the properties of an environment observation channel, such as its dimensions, data type, and name.

    When you create the approximator object, the constructor function sets the ObservationInfo property to the input argument observationInfo.

    You can extract observationInfo from an existing environment, function approximator, or agent using getObservationInfo. You can also construct the specifications manually using rlNumericSpec.

    Example: [rlNumericSpec([2 1]) rlNumericSpec([1 1])]

    Action specifications, specified either as an rlFiniteSetSpec (for discrete action spaces) or rlNumericSpec (for continuous action spaces) object. This object defines the properties of the environment action channel, such as its dimensions, data type, and name.

    Note

    Only one action channel is allowed.

    When you create the approximator object, the constructor function sets the ActionInfo property to the input argument actionInfo.

    You can extract ActionInfo from an existing environment or agent using getActionInfo. You can also construct the specifications manually using rlFiniteSetSpec or rlNumericSpec.

    Example: rlNumericSpec([2 1])

    Normalization method, returned as an array in which each element (one for each input channel defined in the observationInfo and actionInfo properties, in that order) is one of the following values:

    • "none" — Do not normalize the input of the function approximator object.

    • "rescale-zero-one" — Normalize the input by rescaling it to the interval between 0 and 1. The normalized input Y is (UMin)./(UpperLimitLowerLimit), where U is the nonnormalized input. Note that nonnormalized input values lower than LowerLimit result in normalized values lower than 0. Similarly, nonnormalized input values higher than UpperLimit result in normalized values higher than 1. Here, UpperLimit and LowerLimit are the corresponding properties defined in the specification object of the input channel.

    • "rescale-symmetric" — Normalize the input by rescaling it to the interval between –1 and 1. The normalized input Y is 2(ULowerLimit)./(UpperLimitLowerLimit) – 1, where U is the nonnormalized input. Note that nonnormalized input values lower than LowerLimit result in normalized values lower than –1. Similarly, nonnormalized input values higher than UpperLimit result in normalized values higher than 1. Here, UpperLimit and LowerLimit are the corresponding properties defined in the specification object of the input channel.

    Note

    When you specify the Normalization property of rlAgentInitializationOptions, normalization is applied only to the approximator input channels corresponding to rlNumericSpec specification objects in which both the UpperLimit and LowerLimit properties are defined. After you create the agent, you can use setNormalizer to assign normalizers that use any normalization method. For more information on normalizer objects, see rlNormalizer.

    Example: "rescale-symmetric"

    Computation device used to perform operations such as gradient computation, parameter update and prediction during training and simulation, specified as either "cpu" or "gpu".

    The "gpu" option requires both Parallel Computing Toolbox™ software and a CUDA® enabled NVIDIA® GPU. For more information on supported GPUs see GPU Computing Requirements (Parallel Computing Toolbox).

    You can use gpuDevice (Parallel Computing Toolbox) to query or select a local GPU device to be used with MATLAB®.

    Note

    Training or simulating an agent on a GPU involves device-specific numerical round-off errors. These errors can produce different results compared to performing the same operations using a CPU.

    To speed up training by using parallel processing over multiple cores, you do not need to use this argument. Instead, when training your agent, use an rlTrainingOptions object in which the UseParallel option is set to true. For more information about training using multicore processors and GPUs for training, see Train Agents Using Parallel Computing and GPUs.

    Example: "gpu"

    Learnable parameters of the approximation object, specified as a cell array of dlarray objects. This property contains the learnable parameters of the approximation model used by the approximator object.

    Example: {dlarray(rand(256,4)),dlarray(rand(256,1))}

    State of the approximation object, specified as a cell array of dlarray objects. For dlnetwork-based models, this property contains the Value column of the State property table of the dlnetwork model. The elements of the cell array are the state of the recurrent neural network used in the approximator (if any), as well as the state for the batch normalization layer (if used).

    For model types that are not based on a dlnetwork object, this property is an empty cell array, since these model types do not support states.

    Example: {dlarray(rand(256,1)),dlarray(rand(256,1))}

    Object Functions

    rlNeuralNetworkEnvironmentEnvironment model with deep neural network transition models

    Examples

    collapse all

    Create an environment interface and extract observation and action specifications. Alternatively, you can create specifications using rlNumericSpec and rlFiniteSetSpec.

    env = rlPredefinedEnv("CartPole-Continuous");
    obsInfo = getObservationInfo(env);
    actInfo = getActionInfo(env);

    To approximate the reward function, create a deep neural network. For this example, the network has two input layers, one for the current action and one for the next observations. The single output layer contains a scalar, which represents the value of the predicted reward.

    Define each network path as an array of layer objects. Get the dimensions of the observation and action spaces from the environment specifications, and specify a name for the input layers, so you can later explicitly associate them with the appropriate environment channel.

    actionPath = featureInputLayer( ...
        actInfo.Dimension(1), ...
        Name="action");
    
    nextStatePath = featureInputLayer( ...
        obsInfo.Dimension(1), ...
        Name="nextState");
    
    commonPath = [concatenationLayer(1,2,Name="concat")
        fullyConnectedLayer(64)
        reluLayer
        fullyConnectedLayer(64)
        reluLayer
        fullyConnectedLayer(64)
        reluLayer
        fullyConnectedLayer(1)];

    Assemble dlnetwork object.

    net = dlnetwork();
    net = addLayers(net,nextStatePath);
    net = addLayers(net,actionPath);
    net = addLayers(net,commonPath);

    Connect layers.

    net = connectLayers(net,"nextState","concat/in1");
    net = connectLayers(net,"action","concat/in2");

    Plot network.

    plot(net)

    Initialize network and display the number of weights.

    net = initialize(net);
    summary(net)
       Initialized: true
    
       Number of learnables: 8.7k
    
       Inputs:
          1   'nextState'   4 features
          2   'action'      1 features
    

    Create a deterministic transition function object.

    rwdFcnAppx = rlContinuousDeterministicRewardFunction(...
        net,obsInfo,actInfo,...
        ActionInputNames="action", ...
        NextObservationInputNames="nextState");

    Using this reward function object, you can predict the next reward value based on the current action and next observation. For example, predict the reward for a random action and next observation. Since, for this example, only the action and the next observation influence the reward, use an empty cell array for the current observation.

    act = rand(actInfo.Dimension);
    nxtobs = rand(obsInfo.Dimension);
    reward = predict(rwdFcnAppx, {}, {act}, {nxtobs})
    reward = single
        0.1034
    

    To predict the reward, you can also use evaluate.

    reward_ev = evaluate(rwdFcnAppx, {act,nxtobs} )
    reward_ev = 1x1 cell array
        {[0.1034]}
    
    

    Version History

    Introduced in R2022a