Skip to content

Commit

Permalink
Add clear() function to class SimRandom (#401)
Browse files Browse the repository at this point in the history
* Add SimRandom.clear()

* Minor

* Remove commented codes

* Lint error
  • Loading branch information
lihuoran committed Sep 26, 2021
1 parent 56fcfa2 commit 39eedfa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 0 additions & 7 deletions maro/data_lib/cim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@
from typing import List, Union

# we keep 4 random generator to make the result is reproduce-able with same seed(s), no matter if agent passed actions
from maro.simulator.utils import random

ORDER_INIT_RAND_KEY = "order_init"
ROUTE_INIT_RAND_KEY = "route_init"
ORDER_NUM_RAND_KEY = "order_number"
BUFFER_TICK_RAND_KEY = "buffer_time"

DATA_CONTAINER_INIT_SEED_LIMIT = 4096

random.create_instance(ORDER_INIT_RAND_KEY)
random.create_instance(ROUTE_INIT_RAND_KEY)
random.create_instance(ORDER_NUM_RAND_KEY)
random.create_instance(BUFFER_TICK_RAND_KEY)


def clip(min_val: Union[int, float], max_val: Union[int, float], value: Union[int, float]) -> Union[int, float]:
"""Clip value between specified range
Expand Down
7 changes: 7 additions & 0 deletions maro/simulator/utils/sim_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def reset_seed(self, key: str) -> None:
rand = self._rand_instances[key]
rand.seed(self._seed_dict[key])

def clear(self) -> None:
"""Clear all existing random keys.
"""
self._rand_instances = OrderedDict()
self._seed_dict = {}
self._seed = int(time.time())


random = SimRandom()
"""Random utility for simulator, same with original random module."""
Expand Down
4 changes: 4 additions & 0 deletions tests/cim/test_cim_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import yaml

from maro.simulator.utils import random

os.environ["MARO_STREAMIT_ENABLED"] = "true"
os.environ["MARO_STREAMIT_EXPERIMENT_NAME"] = "cim_testing"

Expand Down Expand Up @@ -37,6 +39,8 @@ def __init__(self, *args, **kwargs):
self._reload_topology: str = TOPOLOGY_PATH_CONFIG
self._business_engine: Optional[CimBusinessEngine] = None

random.clear()

def _init_env(self, backend_name: str) -> None:
os.environ["DEFAULT_BACKEND_NAME"] = backend_name
self._env = Env(
Expand Down

0 comments on commit 39eedfa

Please sign in to comment.