simulate trained reinforcement learning agents within specified environment -凯发k8网页登录
simulate trained reinforcement learning agents within specified environment
since r2019a
description
simulates one or more reinforcement learning agents within an environment, using default
simulation options.experience
= sim(env
,agents
)
performs the same simulation as the previous syntax.experience
= sim(agents
,env
)
examples
simulate reinforcement learning environment
simulate a reinforcement learning environment with an agent configured for that environment. for this example, load an environment and agent that are already configured. the environment is a discrete cart-pole environment created with rlpredefinedenv
. the agent is a policy gradient (rlpgagent
) agent. for more information about the environment and agent used in this example, see .
rng(0) % for reproducibility load rlsimexample.mat env
env = cartpolediscreteaction with properties: gravity: 9.8000 masscart: 1 masspole: 0.1000 length: 0.5000 maxforce: 10 ts: 0.0200 thetathresholdradians: 0.2094 xthreshold: 2.4000 rewardfornotfalling: 1 penaltyforfalling: -5 state: [4x1 double]
agent
agent = rlpgagent with properties: agentoptions: [1x1 rl.option.rlpgagentoptions] useexplorationpolicy: 1 observationinfo: [1x1 rl.util.rlnumericspec] actioninfo: [1x1 rl.util.rlfinitesetspec] sampletime: 0.1000
typically, you train the agent using train
and simulate the environment to test the performance of the trained agent. for this example, simulate the environment using the agent you loaded. configure simulation options, specifying that the simulation run for 100 steps.
simopts = rlsimulationoptions(maxsteps=100);
for the predefined cart-pole environment used in this example. you can use plot
to generate a visualization of the cart-pole system. when you simulate the environment, this plot updates automatically so that you can watch the system evolve during the simulation.
plot(env)
simulate the environment.
experience = sim(env,agent,simopts)
experience = struct with fields:
observation: [1x1 struct]
action: [1x1 struct]
reward: [1x1 timeseries]
isdone: [1x1 timeseries]
simulationinfo: [1x1 struct]
the output structure experience
records the observations collected from the environment, the action and reward, and other data collected during the simulation. each field contains a timeseries
object or a structure of timeseries
data objects. for instance, experience.action
is a timeseries
containing the action imposed on the cart-pole system by the agent at each step of the simulation.
experience.action
ans = struct with fields:
cartpoleaction: [1x1 timeseries]
simulate simulink environment with multiple agents
simulate an environment created for the simulink® model used in the example train multiple agents to perform collaborative task, using the agents trained in that example.
load the agents in the matlab® workspace.
load rlcollaborativetaskagents
create an environment for the rlcollaborativetask
simulink® model, which has two agent blocks. since the agents used by the two blocks (agenta
and agentb
) are already in the workspace, you do not need to pass their observation and action specifications to create the environment.
env = rlsimulinkenv( ... "rlcollaborativetask", ... ["rlcollaborativetask/agent a","rlcollaborativetask/agent b"]);
load the parameters that are needed by the rlcollaborativetask
simulink® model to run.
rlcollaborativetaskparams
simulate the agents against the environment, saving the experiences in xpr
.
xpr = sim(env,[agenta agentb]);
plot actions of both agents.
subplot(2,1,1); plot(xpr(1).action.forces) subplot(2,1,2); plot(xpr(2).action.forces)
input arguments
env
— environment
reinforcement learning environment object
environment in which the agents act, specified as one of the following kinds of reinforcement learning environment object:
a predefined matlab® or simulink® environment created using
rlpredefinedenv
.a custom matlab environment you create with functions such as
rlfunctionenv
orrlcreateenvtemplate
. this kind of environment does not support training multiple agents at the same time.a simulink environment you create using
createintegratedenv
. this kind of environment does not support training multiple agents at the same time.a custom simulink environment you create using
rlsimulinkenv
. this kind of environment supports training multiple agents at the same time, and allows you to use multi-rate execution, so that each agent has its own execution rate.a custom matlab environment you create using
rlmultiagentfunctionenv
orrlturnbasedfunctionenv
. this kind of environment supports training multiple agents at the same time. in anrlmultiagentfunctionenv
environment all agents execute in the same step, while in anrlmultiagentfunctionenv
environment agents execute in turns.
for more information about creating and configuring environments, see:
when env
is a simulink environment, the environment object acts an interface so that
sim
calls the (compiled) simulink model to generate experiences for the agents.
agents
— agents
reinforcement learning agent object | array of agent objects
agents to simulate, specified as a reinforcement learning agent object, such as
rlacagent
or
, or
as an array of such objects.
if env
is a multi-agent environment, specify agents as an
array. the order of the agents in the array must match the agent order used to create
env
.
for more information about how to create and configure agents for reinforcement learning, see reinforcement learning agents.
simopts
— simulation options
rlsimulationoptions
object
simulation options, specified as an rlsimulationoptions
object. use this argument to specify options such
as:
number of steps per simulation
number of simulations to run
for details, see rlsimulationoptions
.
output arguments
experience
— simulation results
structure | structure array
simulation results, returned as a structure or structure array. the number of rows
in the array is equal to the number of simulations specified by the
numsimulations
option of rlsimulationoptions
.
the number of columns in the array is the number of agents. the fields of each
experience
structure are as follows.
observation
— observations
structure
observations collected from the environment, returned as a structure with fields corresponding to the observations specified in the environment. each field contains a of length n 1, where n is the number of simulation steps.
to obtain the current observation and the next observation for a given
simulation step, use code such as the following, assuming one of the fields of
observation
is
obs1
.
obs = getsamples(experience.observation.obs1,1:n); nextobs = getsamples(experience.observation.obs1,2:n 1);
sim
to generate experiences for training.
action
— actions
structure
actions computed by the agent, returned as a structure with fields corresponding to the action signals specified in the environment. each field contains a of length n, where n is the number of simulation steps.
reward
— rewards
timeseries
reward at each step in the simulation, returned as a of length n, where n is the number of simulation steps.
isdone
— flag indicating termination of episode
timeseries
flag indicating termination of the episode, returned as a of a scalar logical signal. this flag is set at each step by the environment, according to conditions you specify for episode termination when you configure the environment. when the environment sets this flag to 1, simulation terminates.
simulationinfo
— information collected during simulation
structure | vector of simulink.simulationoutput
objects
information collected during simulation, returned as one of the following:
for matlab environments, a structure containing the field
simulationerror
. this structure contains any errors that occurred during simulation.for simulink environments, a
simulink.simulationoutput
object containing simulation data. recorded data includes any signals and states that the model is configured to log, simulation metadata, and any errors that occurred.
version history
introduced in r2019a
see also
functions
objects
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
select a web site
choose a web site to get translated content where available and see local events and offers. based on your location, we recommend that you select: .
you can also select a web site from the following list:
how to get best site performance
select the china site (in chinese or english) for best site performance. other mathworks country sites are not optimized for visits from your location.
americas
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)