-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathSampling.py
84 lines (60 loc) · 2.88 KB
/
PathSampling.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from pymoo.core.sampling import Sampling
import numpy as np
from math import floor
import random
from PathSolution import *
# from PathProblem import *
'''
TODO
Redo the path sampling i.e. start at -1. cell return to -1.cell (start and end the tours at BS)
Visit each cell (0.-63.) at least "min_visits" times at most "max_visits" times
UPDATES tick
DONE not yet
'''
class AdaptiveRelaySampling(Sampling):
def __init__(self) -> None:
super().__init__()
def _do(self, problem, n_samples, **kwargs):
# X = np.full((n_samples, problem.n_var), 0, dtype=int)
X = np.full((n_samples, problem.n_var), None, dtype=PathSolution)
cells = np.arange(problem.info.number_of_cells)
for i in range(n_samples):
X[i] = np.random.choice(cells, size=problem.info.number_of_drones-1, replace=False)
return X
class PathSampling(Sampling):
def __init__(self) -> None:
super().__init__()
def _do(self, problem, n_samples, **kwargs):
# X = np.full((n_samples, problem.n_var), 0, dtype=int)
X = np.full((n_samples, 1), None, dtype=PathSolution)
for i in range(n_samples):
# Random path
path = np.random.permutation(problem.info.min_visits * problem.info.number_of_cells)%problem.info.number_of_cells
# print(f"path: {min(path), max(path)}")
# Random start points
start_points = sorted(random.sample([i for i in range(1, len(path))], problem.info.number_of_drones - 1))
start_points.insert(0, 0)
# print(f"Path: {path}\nStart Points: {start_points}")
# print("----------------------------------------------------------------------------------------------------")
# print(f"Sample {i}")
# print("----------------------------------------------------------------------------------------------------")
# print(f"Path: {path}\nStart Points: {start_points}")
# print(f"Path: {path}")
# print("Start Points: ", start_points)
X[i, :] = PathSolution(path, start_points, problem.info, calculate_pathplan=False, calculate_tbv=False, calculate_connectivity=False, calculate_disconnectivity=False)
return X
'''
start_points = []
cpd = problem.info.Nc * problem.info.min_visits // problem.info.Nd # Cells per Drone
cpd_bias = 2 # Cells per Drone Bias
for d in range(problem.info.Nd - 1):
if d == 0:
start_points.append(random.randrange(cpd - cpd_bias, cpd + cpd_bias)) # Generate random index
else:
start_points.append(start_points[-1] + random.randrange(cpd - cpd_bias, cpd + cpd_bias))
start_points.insert(0, 0)
'''
'''
start_points = sorted(random.sample([i for i in range(1,len(path))], problem.info.Nd-1))
start_points.insert(0, 0)
'''