-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapGenerator.py
76 lines (65 loc) · 2.71 KB
/
MapGenerator.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
import math
import random
class MapGenerator:
# m : the number of stations
# stations : locations of stations [tuple of 2 real numbers and name (x, y, name)] == map info
# dists : matrix which has the distance info
def __init__(self, m = 20, typ = 'nomal', upp = True):
self.m = m
if typ == 'nomal' :
self.stations = []
for j in range(self.m) :
sta = (random.random()*100, random.random()*100, j)
while sta in self.stations :
sta = (random.random() * 100, random.random() * 100, j)
self.stations.append(sta)
# To ensure all stations are different
if typ == 'clust' :
self.stations = []
for j in range(self.m//2):
sta = (random.random() * 30, random.random() * 30, j)
self.stations.append(sta)
# To ensure all stations are different
for j in range(self.m//2, self.m):
sta = (random.random() * 30 + 70, random.random() * 30 + 70, j)
self.stations.append(sta)
self.depot = (50, 50, -1)
self.distdepot = []
for j in range(self.m):
self.distdepot.append(math.sqrt((self.stations[j][0]-self.depot[0])**2
+(self.stations[j][1]-self.depot[1])**2))
self.dists = self.getDists()
if upp : self.upper() # all dist be synchronized whit tick
pass
def __str__(self):
ret = ""
ret += "The number of stations : {m}\n".format(m = self.m)
ret += "Depot: {c}\n".format(c = self.depot)
for coord in self.stations: ret += "{c}\n".format(c = coord)
ret += "------------------------------------\n"
return ret
def getDists(self):
m = self.m # number of sta
dists = [[None] * m for i in range(m)]
for i in range(m) :
for j in range(m) :
d = self.getDistance(i, j)
dists[i][j] = d
return dists
def getDistance(self, x, y):
# get euclidean distance between station x and station y
return math.sqrt((self.stations[x][0]-self.stations[y][0])**2
+(self.stations[x][1]-self.stations[y][1])**2)
def getLocDist(self, loc, x):
return math.sqrt((self.stations[x][0] - loc[0]) ** 2
+ (self.stations[x][1] - loc[1]) ** 2)
def upper(self):
for distt in self.dists :
i = 0
while i < len(distt) :
distt[i] = math.ceil(distt[i])
i += 1
j =0
while j < len(self.distdepot) :
self.distdepot[j] = math.ceil(self.distdepot[j])
j += 1