Hi Federico,
I understand that the “train” command is being used to train one or more reinforcement learning agents and the training time is long. The speed of the training can be improved by using parallel computing. You can refer to the following documentation to configure the training options to utilize parallel computing resources:
You can also refer to the following documentation to understand more about training agents using the Parallel Computing Toolbox:
Please refer to the following alternative solution that utilizes callbacks:
I am assuming that the post-processing needs to be done on the output of the “train” command “trainStats”. Since no code has been provided, I have utilized the example code for the “train” command to reproduce the issue. You can run the following line in the MATLAB command window to open the example code:
openExample('rl/TrainAReinforcementLearningAgentExample')
It is indeed true that stopping the training midway does not save the “trainStats” output of the “train” command. The output is, however, saved in the base workspace when the “Stop Training” button available on the Training Monitor is used.
Please refer to the following steps to stop the training in the example code and save the training stats without using the monitor:
The solution utilizes a custom function to stop the training. The “StopTrainingCriteria” property of the training options object “trainOpts” can be utilized to manually stop the training, along with the terminating condition of the training agent. A global variable “stopTraining” is used to stop the training from a GUI without using the Training Monitor. The modifications in the example code are as follows:
- Set the “StopTrainingCriteria” property of the “trainOpts” object to “Custom”.
- Set the “StopTrainingValue”property to the custom function handle“@myTerminationFcn”.
- Create a custom function named “myTerminationFcn”. The function’s input argument is the “trainStats” output of the “train” command. The output argument “flag” stops the training when set to “true". The stopping criteria for the training agent is specified in a “if” clause along with the “stopTraining” global variable that is used to manually stop the training. The function is as follows:
function flag = myTerminationFcn(trainingInfo)
if (trainingInfo.AverageReward(end) >= 495 || stopTraining)
- Create a Simple GUI with a Button to stop the training by clicking a button. This allows you to manually stop the training when required without using the Training Monitor. The GUI is created by the following function:
function createStopButton()
hFig = figure('Name', 'Stop Training', 'NumberTitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none', ...
'Position', [100, 100, 200, 100]);
uicontrol('Style', 'pushbutton', 'String', 'Stop Training', ...
'Position', [50, 30, 100, 40], ...
'Callback', @stopButtonCallback);
function stopButtonCallback(~, ~)
disp('Training stopped.');
- The above GUI can be called by running the following line in the MATLAB command window, before the training is started:
- After setting the "plots" property of “trainOpts” object to "none", the training can be started. The GUI allows the training to be stopped at any time. The “trainStats” output of the “train” command is saved to the base workspace when the training is stopped midway.
The modified example codes has been attached as a zip file with this answer. I hope this helps resolves your issue.