MATLAB: Deploy trained policy to simulink model

reinforcement learningReinforcement Learning Toolboxsimulink

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!

Best Answer

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.