set a maximum training time for training a PPO agent

6 ビュー (過去 30 日間)
Danial Kazemikia
Danial Kazemikia 2024 年 7 月 30 日
回答済み: Satwik 2024 年 7 月 30 日
In training process of a PPO RL agent, how can I make the code check the elapsed time and stop training if it exceeds the desired threshold. Suppose you want to stop training after a maximum of 30 minutes (1800 seconds).

採用された回答

Satwik
Satwik 2024 年 7 月 30 日
Hi,
I understand that you want to limit the training process of your RL PPO agent based on the elapsed time. According to my knowledge and the documentation for ‘rlTrainingOptions, there is no predefined option in theStopTrainingCriteria property which can achieve this directly. However, a possible workaround is to use the custom stop criteria by specifying ‘StopTrainingValueas a function name or handle. Here is the documentation link for reference:
Below is an example code snippet demonstrating this approach:
% Set the maximum training time (in seconds)
maxTrainingTime = 1800; % 30 minutes
% Custom stop function
function stopTraining = customStopFunction(trainingStats)
persistent startTime;
if isempty(startTime)
startTime = tic;
end
elapsedTime = toc(startTime);
if elapsedTime > maxTrainingTime
stopTraining = true;
disp(['Stopping training after ', num2str(elapsedTime), ' seconds.']);
else
stopTraining = false;
end
end
% Set training options
trainingOptions = rlTrainingOptions(...
'MaxEpisodes', 10000, ...
'MaxStepsPerEpisode', 500, ...
'ScoreAveragingWindowLength', 100, ...
'Verbose', true, ...
'Plots', 'training-progress', ...
'StopOnError', 'off', ...
'SaveAgentCriteria', 'EpisodeReward', ...
'SaveAgentValue', 500, ...
'StopTrainingCriteria', 'Custom', ...
'StopTrainingValue', @customStopFunction);
% Train the agent
trainStats = train(agent, env, trainingOptions); % 'env' is the defined
% environment and 'agent' is the rlPPOAgent.
Here is the reference to the ‘tic’ and ‘toc’ functions used in the ‘customStopFunction to capture time:
I hope this gives you a direction for taking the next steps to achieve the desired result.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeReinforcement Learning についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by