소스 검색

Added _rand_subset method to MiniGridEnv

Maxime Chevalier-Boisvert 7 년 전
부모
커밋
0ad70c61dd
1개의 변경된 파일17개의 추가작업 그리고 0개의 파일을 삭제
  1. 17 0
      gym_minigrid/minigrid.py

+ 17 - 0
gym_minigrid/minigrid.py

@@ -808,6 +808,23 @@ class MiniGridEnv(gym.Env):
         idx = self._rand_int(0, len(lst))
         idx = self._rand_int(0, len(lst))
         return lst[idx]
         return lst[idx]
 
 
+    def _rand_subset(self, iterable, num_elems):
+        """
+        Sample a random subset of distinct elements of a list
+        """
+
+        lst = list(iterable)
+        assert num_elems <= len(lst)
+
+        out = []
+
+        while len(out) < num_elems:
+            elem = self._rand_elem(lst)
+            lst.remove(elem)
+            out.append(elem)
+
+        return out
+
     def _rand_color(self):
     def _rand_color(self):
         """
         """
         Generate a random color name (string)
         Generate a random color name (string)