MATLAB Answers

Deploy trained policy to simulink model

19 views (last 30 days)
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!

  0 Comments

Sign in to comment.

Accepted Answer

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 Comments

Show 3 older comments
Emmanouil Tzorakoleftherakis
The steps I mention above are for running simulations with a MATLAB Fcn block (without calling the interpreter) that does inference on a neural network created by Reinforcement Learning Toolbox (or Deep Learning Toolbox). If you also want to generate readable C/C++ code from the model, there is one more step which involves replacing the MATLAB Fcn block with an s-function. This video shows how to do this for GPU code, and we are working on one that does the same with C/C++ code using MATLAB Coder from Reinforcement Learning Toolbox. With s-functions you should be able to generate code from your model assuming a) all the layers you are using support code generation (see here) b) you don't have multiple input layers, i.e., you are generating code from an actor and not from a critic.
The development team is actually working on a more user-friendly way to bring neural networks into Simulink, so think of the s-function approach as a temporary workaround. I hope this helps.
Sviatoslav Klos
Sviatoslav Klos on 3 Mar 2020
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!