-
Notifications
You must be signed in to change notification settings - Fork 19
/
GenerateBigGraph.py
80 lines (73 loc) · 2.98 KB
/
GenerateBigGraph.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
from NodeAndEdge import *
import random
def generate_big_graph(common_graph, node_num, request_num, depot_num):
"""
:param common_graph: origin graph
:param node_num: the number of node
:param request_num: the number of request
:param depot_num: the number of depot
:return:
"""
random.seed(111)
uncommon_node_num = 2 * request_num + depot_num + 2
assert uncommon_node_num <= node_num, "uncommon_node_num must be less than node_num!"
node_serials = random.sample(range(0, node_num), uncommon_node_num)
pick = node_serials[:request_num]
delivery = node_serials[request_num:2 * request_num]
depots = node_serials[2 * request_num:2 * request_num + depot_num]
start = node_serials[-2] # starting point of car
destination = node_serials[-1] # destination of car
deadline = random.sample(range(200, 300), request_num)
capacity_required = random.sample(range(5, 20), request_num)
requests_temp = list(zip(pick, delivery, deadline, capacity_required))
requests = {}
for i, re in enumerate(requests_temp):
request = Request(i, re[0], re[1], re[2], re[3])
common_graph[re[0]].type = Pick(re[2], re[3], i)
common_graph[re[1]].type = Delivery(re[2], -re[3], i)
requests[i] = request
for depot in depots:
R = random.randint(100, 200)
common_graph[depot].type = Depot(R)
battery_size = random.randint(20000000, 30000000)
initial_energy = random.uniform(0.2, 0.9) * battery_size
capacity = random.randint(50, 100)
common_graph[start].type = Start(battery_size, initial_energy, capacity)
common_graph[destination].type = Destination()
return common_graph, requests
def generate_common_graph(node_num, lower_bound, high_bound):
"""
:param lower_bound: the lower bound of coordinate of node
:param high_bound: the high bound of coordinate of node
:return:
"""
# 生成一个普通的大图,图中各个点并没有属性
# 参数: 节点数量 坐标范围下界 坐标范围上界
result = []
coordinate_list = []
x = []
y = []
for i in range(node_num):
x.append(random.uniform(lower_bound, high_bound))
y.append(random.uniform(lower_bound, high_bound))
coordinates = list(zip(x, y))
for i, coordinate in enumerate(coordinates):
type = Commom()
node = Node(i, coordinate, type)
result.append(node)
roads = []
for i in range(node_num - 1):
to = random.sample(range(i + 1, node_num), int((node_num - i - 1) / 2) + 1)
for j in to:
roads.append((i, j))
for road in roads:
t1 = result[road[0]]
t2 = result[road[1]]
t1_co = t1.coordinate
t2_co = t2.coordinate
length = ((t1_co[0] - t2_co[0]) ** 2 + (t1_co[1] - t2_co[1]) ** 2) ** 0.5
time = length
energy = length
result[road[0]].add_road(road[1], length, time, energy)
result[road[1]].add_road(road[0], length, time, energy)
return result