Deploy trained policy to simulink model

2 次查看(过去 30 天)
I am trying to deploy a trained policy of the reinforcement learning toolbox to a simulink model. This model has to be compatible with code generation (not just normal & accelerator mode). So using an Interpreted function block as stated here (https://www.mathworks.com/matlabcentral/answers/485994-reinforcement-learning-how-to-use-a-trained-policy-as-a-controller-block-in-simulink?s_tid=answers_rc1-1_p1_BOTH) is not suitable. But I am stuck at this point with several errors. Can please anyone provide an example on how to do this? Also the matlab documentation is laacking here, just showing how to compile a mex file of the evaluatePolicy function. Therefore, I run the follwing lines,
load('Agent3524.mat','saved_agent')
generatePolicyFunction(saved_agent)
and get my eveluatePolicy function,
function action1 = evaluatePolicy(observation1)
%#codegen
% Reinforcement Learning Toolbox
% Generated on: 20-Feb-2020 17:30:58
action1 = localEvaluate(observation1);
end
%% Local Functions
function action1 = localEvaluate(observation1)
persistent policy
if isempty(policy)
policy = coder.loadDeepLearningNetwork('agentData.mat','policy');
end
action1 = predict(policy,observation1);
end
I have a simple Simulink model:
with the matlab function block code:
function y = fcn(u)
y = evaluatePolicy(u);
end
Now I get a bunch of error messages, and I dont know how to solve them:
Undefined function or variable 'dltargets'.
P-code function 'DeepLearningNetwork.p' produced an error.
Component:MATLAB Function | Category:Coder error
Function call failed.
Function 'loadDeepLearningNetwork.m' (#31.3569.3643), line 100, column 15:
"coder.DeepLearningNetwork(coder.const(matfile), coder.const(''), param{:})"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function call failed.
Function 'evaluatePolicy.m' (#29.286.341), line 13, column 11:
"coder.loadDeepLearningNetwork('agentData.mat','policy')"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Persistent variable 'policy' must be assigned before it is used. The only exception is a check using 'isempty(policy)' that can be performed prior to assignment.
Function 'evaluatePolicy.m' (#29.365.371), line 15, column 19:
"policy"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function call failed.
Function 'evaluatePolicy.m' (#29.140.167), line 7, column 11:
"localEvaluate(observation1)"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function call failed.
Function 'MATLAB Function' (#23.29.46), line 3, column 9:
"evaluatePolicy(u)"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'test/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'test/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'test/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Model error
An error occurred while propagating data type 'double' through 'test/MATLAB Function'.
Component:Simulink | Category:Model error
Any help is appreciated! Thanks!

采纳的回答

Emmanouil Tzorakoleftherakis
Hello,
Looks like the dimensions cannot be determined automatically. If you double click the MATLAB Fcn block and then click "Edit Data", you can specify the input and output dimensions of the block, i.e. for y and u. It would also be helpful to initialize y before calling evaluatePolicy with a vector that has the same dimensions as the policy output.
Just making sure, you also need to have installed the support package MATLAB Coder for Deep Learning and the mkl-dnn library.
Please let me know if you still have trouble after changing the above.
  6 个评论
Sviatoslav Klos
Sviatoslav Klos 2020-3-3
Hello.
I would like to ask related question.
Emmanouil Tzorakoleftherakis, would you also working on capability of code generation from a critic? Will it be in 2020a?
Since in documentation of the documentation here (https://www.mathworks.com/help/reinforcement-learning/ref/rl.agent.rldqnagent.generatepolicyfunction.html) it is the example of DQN agent policy generation that up to me is not possible for now.
Emmanouil Tzorakoleftherakis
Yes the development team is actively working on addressing this limitation.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by