How to generate random numbers in RL simulation

조회 수: 7 (최근 30일)
Aaron Bramhasta
Aaron Bramhasta 2024년 8월 18일
댓글: Aaron Bramhasta 2024년 8월 22일
Dear Matlab Experts, I am currently modelling a reinforcement learning agent integrated into a simevents system. My model is able to run, however there is supposed to be a random number generator in the entity server block "mission". If I unplug the RL agent and run the simevents model only, the random numbers are generated randomly as intended, however if I run the complete model with the RL agent, the results always gives the same values and not random at all. I understand stand that matlab rng is deterministic and depends on a predefined seed, how do you think should I proceed to gain actual random numbers for my model? Thank you in advance. Attached is my model.

답변 (1개)

Pratyush
Pratyush 2024년 8월 21일
Hi Aaron,
To ensure randomness in a SimEvents model integrated with a reinforcement learning (RL) agent in MATLAB, follow these steps:
  • Use "rng('shuffle')" before running your simulation to set a non-fixed seed based on the current time.
  • Ensure SimEvents blocks use a non-fixed seed for random number generation.
  • Ensure any randomness in the RL agent also uses a non-fixed seed.
  • Check that no part of your code unintentionally resets the RNG.
  • Use logging to monitor RNG states throughout the simulation.
  • Ensure each parallel worker has a unique RNG seed.
These steps help maintain randomness across simulations.
  댓글 수: 1
Aaron Bramhasta
Aaron Bramhasta 2024년 8월 22일
Hi Pratyush,
Thank you for your reply, I have an update from my side that I would like to share with you:
  1. I tried implementing the method of adding an "rng('shuffle');" line of code into my matlab live script.
  2. I also added "rng('shuffle');" into the model parameter of simulink in initfcn section, to have a random seed before initialization of each simulation.
  3. The number that is generated uses a simple line of code "rng1 = randi([0, 100]);" and "rng2 = randi([1, 2]);"
  4. The rng is treated as an entity (entity.data.rng1 & entity.data.rng2) that passes through my simevents blocks for logging purposes.
  5. So far, the results are yet to be random. The simulation seems to converge to the same results each iteration. (the figure below is the example of a random number (rng1) generated, the results are the same for many iterations attempted)
There is also another methods I tried that failed:
  1. adding "rng('shuffle');" line of code into the entity server block, where the random number is required: resulting error of JIT incompability.
I've also attached the update of my model for your reference. Let me know what you think.
Thank you so much in advance!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 SimEvents에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by