diff options
| author | JonZhao <[email protected]> | 2019-05-23 21:07:31 +0800 |
|---|---|---|
| committer | JonZhao <[email protected]> | 2019-05-23 21:07:31 +0800 |
| commit | cc5e7266bc21e24d45ebd5d9d4a56f53cb98cb56 (patch) | |
| tree | 2bf045b0c16ae89fb5be5ba8dace84581f23f5df /vrptw_base.py | |
| download | VRPTW-ACO-python-cc5e7266bc21e24d45ebd5d9d4a56f53cb98cb56.tar.gz VRPTW-ACO-python-cc5e7266bc21e24d45ebd5d9d4a56f53cb98cb56.tar.bz2 VRPTW-ACO-python-cc5e7266bc21e24d45ebd5d9d4a56f53cb98cb56.zip | |
init project
Diffstat (limited to 'vrptw_base.py')
| -rw-r--r-- | vrptw_base.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/vrptw_base.py b/vrptw_base.py new file mode 100644 index 0000000..37b73d3 --- /dev/null +++ b/vrptw_base.py @@ -0,0 +1,43 @@ +import numpy as np + + +class Node: + def __init__(self, x, y, demand, earliest_time, latest_time, service_time): + super() + self.x = x + self.y = y + self.demand = demand + self.earliest_time = earliest_time + self.latest_time = latest_time + self.service_time = service_time + + +class Graph: + def __init__(self, file_path): + super() + # node_num 结点个数 + # node_dist_mat 节点之间的距离(矩阵) + # pheromone_mat 节点之间路径上的信息度浓度 + self.node_num, self.nodes, self.node_dist_mat = self.create_from_file(file_path) + + def create_from_file(self, file_path): + # 从文件中读取服务点、客户的位置 + with open(file_path, 'rt') as f: + node_list = list(line.split() for line in f) + node_num = len(node_list) + nodes = list(Node(float(item[0]), float(item[1]), float(item[2]), float(item[3]), float(item[4]), float(item[5])) for item in node_list) + + # 创建距离矩阵 + node_dist_mat = np.zeros((node_num, node_num)) + for i in range(node_num): + node_a = nodes[i] + for j in range(i, node_num): + node_b = nodes[j] + node_dist_mat[i][j] = Graph.calculate_dist(node_a, node_b) + node_dist_mat[j][i] = node_dist_mat[i][j] + + return node_num, nodes, node_dist_mat + + @staticmethod + def calculate_dist(node_a, node_b): + return np.linalg.norm((node_a.x - node_b.x, node_a.y - node_b.y))
\ No newline at end of file |
