a-ragab-h-m commited on
Commit
7eb6b8d
·
verified ·
1 Parent(s): f75943a

Create build_data.py

Browse files
Files changed (1) hide show
  1. build_data.py +84 -0
build_data.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datetime import datetime
3
+ import sys
4
+
5
+ import torch
6
+ import numpy as np
7
+
8
+ from torch.utils.data import Dataset
9
+
10
+ # Ensure project directory is included
11
+
12
+ class Raw_VRP_Data(object):
13
+
14
+ def __init__(self, dataset_size=1000, num_nodes=50, num_depots=1):
15
+
16
+ self.dataset_size = dataset_size
17
+ self.num_nodes = num_nodes
18
+ num_cars = num_nodes
19
+ self.num_depots = num_depots
20
+
21
+ # fleet data
22
+ launch_time = torch.zeros(self.dataset_size, num_cars, 1)
23
+ car_start_node = torch.randint(low=0, high=num_depots, size=(self.dataset_size, num_cars, 1))
24
+
25
+ fleet = {
26
+ 'start_time': launch_time,
27
+ 'car_start_node': car_start_node,
28
+ }
29
+
30
+ # graph data
31
+ a = torch.arange(num_nodes).reshape(1, 1, -1).repeat(self.dataset_size, num_cars, 1)
32
+ b = car_start_node.repeat(1, 1, num_nodes)
33
+ depot = ((a == b).sum(dim=1) > 0).float().unsqueeze(2)
34
+
35
+ start_times = (torch.rand(self.dataset_size, num_nodes, 1) * 2 + 3) * (1 - depot)
36
+ end_times = start_times + (0.1 + 0.5 * torch.rand(self.dataset_size, num_nodes, 1)) * (1 - depot)
37
+
38
+ node_positions = torch.rand(dataset_size, num_nodes, 2)
39
+ distance_matrix = self.compute_distance_matrix(node_positions)
40
+ time_matrix = distance_matrix.clone()
41
+
42
+ graph = {
43
+ 'start_times': start_times,
44
+ 'end_times': end_times,
45
+ 'depot': depot,
46
+ 'node_vector': node_positions,
47
+ 'distance_matrix': distance_matrix,
48
+ 'time_matrix': time_matrix
49
+ }
50
+
51
+ self.data = {
52
+ 'fleet': fleet,
53
+ 'graph': graph
54
+ }
55
+
56
+ def compute_distance_matrix(self, node_positions):
57
+ x = node_positions.unsqueeze(1).repeat(1, self.num_nodes, 1, 1)
58
+ y = node_positions.unsqueeze(2).repeat(1, 1, self.num_nodes, 1)
59
+ distance = (((x - y)**2).sum(dim=3))**0.5
60
+ return distance
61
+
62
+ def get_data(self):
63
+ return self.data
64
+
65
+ def save_data(self, fp):
66
+ torch.save(self.data, fp)
67
+
68
+
69
+ if __name__ == '__main__':
70
+
71
+ size = 50000
72
+ num_nodes = 30
73
+ num_depots = 1
74
+
75
+ start = datetime.now()
76
+
77
+ raw_data = Raw_VRP_Data(dataset_size=size, num_nodes=num_nodes, num_depots=num_depots)
78
+
79
+ path = os.path.join(os.getcwd(), 'VRP_data.pt')
80
+ raw_data.save_data(path)
81
+
82
+ end = datetime.now()
83
+ duration = (end - start).seconds
84
+ print(f"Data generation completed in {duration} seconds.")