validateEnvironment

Validate custom reinforcement learning environment

Description

example

validateEnvironment(env) validates a reinforcement learning environment. This function is useful when:

  • You are using a custom environment for which you supplied your own step and reset functions, such as an environment created using rlCreateEnvTemplate.

  • You are using an environment you created from a Simulink® model using rlSimulinkEnv.

validateEnvironment resets the environment, generates an initial observation and action, and simulates the environment for one or two steps (see Algorithms). If there are no errors during these operations, validation is successful, and validateEnvironment returns no result. If errors occur, these errors appear in the MATLAB® command window. Use the errors to determine what to change in your observation specification, action specification, custom functions, or Simulink model.

Examples

collapse all

Create and validate and environment for the rlwatertank model, which represents a control system containing a reinforcement learning agent. (For details about this model, see Create Simulink Environment and Train Agent.)

Open the model.

open_system('rlwatertank')

Create observation and action specifications for the environment.

obsInfo = rlNumericSpec([3 1],...
    'LowerLimit',[-inf -inf 0  ]',...
    'UpperLimit',[ inf  inf inf]');
obsInfo.Name = 'observations';
obsInfo.Description = 'integrated error, error, and measured height';
numObservations = obsInfo.Dimension(1);

actInfo = rlNumericSpec([1 1]);
actInfo.Name = 'flow';
numActions = numel(actInfo);

Create an environment from the model. Then, use validateEnvironment to check whether the model is configured correctly.

env = rlSimulinkEnv('rlwatertank','rlwatertank/RL Agent',obsInfo,actInfo);
validateEnvironment(env)
Error using rl.env.SimulinkEnvWithAgent/validateEnvironment (line 187)
Simulink environment validation requires an agent in the MATLAB base workspace 
or in a data dictionary linked to the model. Specify the agent in the Simulink model.

validateEnvironment attempts to compile the model, initialize the environment and the agent, and simulate the model. In this case, the RL Agent block is configured to use an agent called agent, but no such variable exists in the MATLAB workspace. Thus, the function returns an error indicating the problem.

Create an appropriate agent for this system using the commands detailed in the Create Simulink Environment and Train Agent. Then, run validateEnvironment again.

validateEnvironment(env)

This time, the initialization and two-step simulation runs without error. Therefore, validateEnvironment returns nothing, and validation is successful. validateEnvironment leaves the model itself unchanged, restoring all model parameters to their original values.

Input Arguments

collapse all

Environment to validate, specified as a reinforcement learning environment object, such as:

  • A custom MATLAB environment you create with rlCreateEnvTemplate. In this case, validateEnvironment checks that the observations and actions generated during simulation of the environment are consistent in size, data type, and value range with the observation specification and action specification. It also checks that your custom step and reset functions run without error. (When you create a custom environment using rlFunctionEnv, the software runs validateEnvironment automatically.)

  • A custom Simulink environment you create using rlSimulinkEnv. If you use a Simulink environment, you must also have an agent defined and associated with the RL Agent block in the model. For a Simulink model, validateEnvironment checks that the model compiles and runs without error. The function does not dirty your model.

For more information about creating and configuring environments, see:

Algorithms

collapse all

validateEnvironment works by running a brief simulation of the environment and making sure that the generated signals match the observation and action specifications you provided when you created the environment.

MATLAB Environments

For MATLAB environments, validation includes the following steps.

  1. Reset the environment using the reset function associated with the environment.

  2. Obtain the first observation and check whether it is consistent with the dimension, data type, and range of values in the observation specification.

  3. Generate a test action based on the dimension, data type, and range of values in the action specification.

  4. Simulate the environment for one step using the generated action and the step function associated with the environment.

  5. Obtain the new observation signal and check whether it is consistent with the dimension, data type, and range of values in the observation specification.

If any of these operations generates an error, validateEnvironment returns the error. If validateEnvironment returns no result, then validation is successful.

Simulink Environments

For Simulink environments, validation includes the following steps.

  1. Reset the environment.

  2. Simulate the model for two time steps.

If any of these operations generates an error, validateEnvironment returns the error. If validateEnvironment returns no result, then validation is successful.

validateEnvironment performs these steps without dirtying the model, and leaves all model parameters in the state they were in when you called the function.

Introduced in R2019a