%% Create Environment from Simulink Model
% This example shows how to use createIntegratedEnv to create an environment object starting from a Simulink model that implements the system with which the agent. Such a system is often referred to as plant, open-loop system, or reference system, while the whole (integrated) system including the agent is often referred to as the closed-loop system.
% For this example, use the flying robot model described in Train DDPG Agent to Control a Flying Robot as the reference (open-loop) system.
% Open the flying robot model.

open_system('rlFlyingRobotEnv')

% Initialize the state variables and sample time.

% initial model state variables
theta0 = 0;
x0 = -15;
y0 = 0;

% sample time
Ts = 0.4;

% Create the Simulink model IntegratedEnv containing the flying robot model connected in a closed loop to the agent block. The function also returns the reinforcement learning environment object env to be used for training.

env = createIntegratedEnv('rlFlyingRobotEnv','IntegratedEnv')

% The function can also return the block path to the RL Agent block in the new integrated model, as well as the observation and action specifications for the reference model.

[~,agentBlk,observationInfo,actionInfo] = createIntegratedEnv('rlFlyingRobotEnv','IntegratedEnv')

% Returning the block path and specifications is useful in cases in which you need to modify descriptions, limits, or names in observationInfo and actionInfo. After modifying the specifications, you can then create an environment from the integrated model IntegratedEnv using the rlSimulinkEnv function.
% Copyright 2019 The MathWorks, Inc.