%% Validate Simulink Environment
% This example shows how to validate a Simulink environment.
%
% 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_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.

env = rlSimulinkEnv('rlwatertank','rlwatertank/RL Agent',obsInfo,actInfo);

% Now you use validateEnvironment to check whether the model is configured correctly.
% 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 example. In this case, load the agent from the rlWaterTankDDPGAgent.mat file.

load rlWaterTankDDPGAgent

% Now, run validateEnvironment again.

validateEnvironment(env)

% Copyright 2019 The MathWorks, Inc.