How to generate random numbers in RL simulation

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

Answers (1)

Pratyush
Pratyush on 21 Aug 2024
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 Comment
Aaron Bramhasta
Aaron Bramhasta on 22 Aug 2024
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!

Sign in to comment.

Categories

Find more on SimEvents in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!