Skip to content

Commit 39eedfa

Browse files
authored
Add clear() function to class SimRandom (#401)
* Add SimRandom.clear() * Minor * Remove commented codes * Lint error
1 parent 56fcfa2 commit 39eedfa

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

maro/data_lib/cim/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55
from typing import List, Union
66

77
# we keep 4 random generator to make the result is reproduce-able with same seed(s), no matter if agent passed actions
8-
from maro.simulator.utils import random
9-
108
ORDER_INIT_RAND_KEY = "order_init"
119
ROUTE_INIT_RAND_KEY = "route_init"
1210
ORDER_NUM_RAND_KEY = "order_number"
1311
BUFFER_TICK_RAND_KEY = "buffer_time"
1412

1513
DATA_CONTAINER_INIT_SEED_LIMIT = 4096
1614

17-
random.create_instance(ORDER_INIT_RAND_KEY)
18-
random.create_instance(ROUTE_INIT_RAND_KEY)
19-
random.create_instance(ORDER_NUM_RAND_KEY)
20-
random.create_instance(BUFFER_TICK_RAND_KEY)
21-
2215

2316
def clip(min_val: Union[int, float], max_val: Union[int, float], value: Union[int, float]) -> Union[int, float]:
2417
"""Clip value between specified range

maro/simulator/utils/sim_random.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def reset_seed(self, key: str) -> None:
8686
rand = self._rand_instances[key]
8787
rand.seed(self._seed_dict[key])
8888

89+
def clear(self) -> None:
90+
"""Clear all existing random keys.
91+
"""
92+
self._rand_instances = OrderedDict()
93+
self._seed_dict = {}
94+
self._seed = int(time.time())
95+
8996

9097
random = SimRandom()
9198
"""Random utility for simulator, same with original random module."""

tests/cim/test_cim_scenario.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import yaml
1010

11+
from maro.simulator.utils import random
12+
1113
os.environ["MARO_STREAMIT_ENABLED"] = "true"
1214
os.environ["MARO_STREAMIT_EXPERIMENT_NAME"] = "cim_testing"
1315

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

42+
random.clear()
43+
4044
def _init_env(self, backend_name: str) -> None:
4145
os.environ["DEFAULT_BACKEND_NAME"] = backend_name
4246
self._env = Env(

0 commit comments

Comments
 (0)