%% Create Deterministic Actor from Custom Basis Function
% Create an observation specification object (or alternatively use getObservationInfo to extract the specification object from an environment). For this example, define the observation space as a continuous four-dimensional space, so that a single observation is a column vector containing 3 doubles.

obsInfo = rlNumericSpec([3 1]);

%
% The deterministic actor does not support discrete action spaces. Therefore, create a continuous action space specification object (or alternatively use getActionInfo to extract the specification object from an environment). For this example, define the action space as a continuous two-dimensional space, so that a single action is a column vector containing 2 doubles.

actInfo = rlNumericSpec([2 1]);

%
% Create a custom basis function. Each element is a function of the observations defined by obsInfo.

myBasisFcn = @(myobs) [myobs(2)^2; myobs(1); 2*myobs(2)+myobs(1); -myobs(3)]

%
% The output of the actor is the vector W'*myBasisFcn(myobs), which is the action taken as a result of the given observation. The weight matrix W contains the learnable parameters and must have as many rows as the length of the basis function output and as many columns as the dimension of the action space.
% Define an initial parameter matrix.

W0 = rand(4,2);

%
% Create the actor. The first argument is a two-element cell containing both the handle to the custom function and the initial weight matrix. The second and third arguments are, respectively, the observation and action specification objects.

actor = rlDeterministicActorRepresentation({myBasisFcn,W0},obsInfo,actInfo)

%
% To check your actor, use the getAction function to return the action from a given observation, using the current parameter matrix.

a = getAction(actor,{[1 2 3]'});
a{1}

% You can now use the actor (along with an critic) to create a suitable continuous action space agent.
% Copyright 2019 The MathWorks, Inc.