-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment.py
56 lines (39 loc) · 1.57 KB
/
environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Implements an Environment class which generates 2D environments with landmarks (or obstacles).
"""
import math
import random
__author__ = "Victor Mawusi Ayi <ayivima@hotmail.com>"
class Environment:
def __init__(self, size, landmark_count):
"""Intializes environment state"""
# Set environment dimensions
if isinstance(size, (int, float)):
self.width = self.height = size
elif isinstance(size, (tuple, list, set)):
self.width, self.height = size
if not all((
isinstance(self.width, (int, float)),
isinstance(self.height, (int, float))
)):
raise ValueError(
"Size must be a sequence with two "
"numbers of type integer or float."
)
if landmark_count >= (self.width * self.height) * 2/3:
raise ValueError(
"landmark_count argument must be less than "
"two-thirds of the area of the environment"
)
self.landmark_count = landmark_count
# Set environment landmarks
self._set_landmarks_()
def _set_landmarks_(self, seed=None):
"""Sets new landmarks"""
random.seed(seed)
randx = lambda: int(random.random() * self.width)
randy = lambda: int(random.random() * self.height)
# Initialize landmarks
self.landmarks = [
(randx(), randy()) for _ in range(self.landmark_count)
]