-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
66 lines (61 loc) · 2.23 KB
/
utils.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
57
58
59
60
61
62
63
64
65
import random
import numpy as np
def make_n_box_ranges(
num_particle_groups,
size,
domain,
size_random_level,
min_interval,
):
"""
Generates n non-overlapping box ranges in the given domain.
Parameters
----------
num_particle_groups: int
The number of box ranges to generate
size: List of float
The size of each box range in each dimension
domain: List of tuples
The domain in which to generate the box ranges, represented as a list of tuples
where each tuple contains the start and end of the domain in each dimension
size_random_level: float
The level of randomization to apply to each box size
min_interval: float
The minimum interval to be maintained between boxes in each dimension
dimensions: int, optional (default=2)
The number of dimensions in the domain
Returns
-------
boxes: List of lists
A list of generated box ranges, represented as a list of lists, where each inner list
contains tuples representing the start and end of the box range in each dimension.
"""
dimensions = len(domain)
boxes = []
attempt = 0
max_attempts = 100000
while len(boxes) < num_particle_groups:
random_size = size * np.random.uniform(1 - size_random_level, 1 + size_random_level, 1)
box = []
for i in range(dimensions):
start = random.uniform(
domain[i][0], domain[i][1] - random_size[i] - min_interval)
end = start + random_size[i]
box.append((start, end))
overlap = False
for existing_box in boxes:
overlap_count = 0
for i in range(dimensions):
if (existing_box[i][0] - min_interval <= box[i][1]) and (box[i][0] <= existing_box[i][1] + min_interval):
overlap_count += 1
if overlap_count >= 2:
overlap = True
break
if overlap:
break
if not overlap:
boxes.append(box)
attempt += 1
if attempt > max_attempts:
raise Exception(f"Could not generate non-overlapping boxes after {max_attempts} attempts")
return boxes