代码拉取完成,页面将自动刷新
同步操作将从 whigg/quantum-routing 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from configs import NUM_ROADS, NUM_CARS, LAMBDA_VAL
def construct_tsp_matrix(dist_matrix):
size = len(dist_matrix)
return add_uniqueness_constraint(size,
add_existence_constraint(size, size,
add_costs_tsp(dist_matrix,
initialize_diagonal_elements(size, size)
)
)
)
def construct_traffic_matrix(share_pairs):
return add_existence_constraint(NUM_ROADS, NUM_CARS,
add_costs_traffic(share_pairs, NUM_ROADS,
initialize_diagonal_elements(NUM_ROADS, NUM_CARS)
)
)
def initialize_diagonal_elements(group_size, number_of_groups):
Q = {}
for current_group in range(group_size):
for current_city in range(number_of_groups):
offset = current_group * group_size
q = offset + current_city
Q[(q, q)] = 0
return Q
def add_costs_tsp(dist_matrix, matrix):
Q = matrix
size = len(dist_matrix)
for current_group in range(size):
for current_city in range(size):
for next_city in range(size):
if current_city == next_city:
continue
offset1 = current_group * size
offset2 = ((current_group + 1) % size) * size
q1 = offset1 + current_city
q2 = offset2 + next_city
Q[(q1, q2)] = dist_matrix[current_city][next_city]
return Q
def add_costs_traffic(share_pairs, num_roads, matrix):
Q = matrix
for share_pair in share_pairs:
car1_data = share_pair[0]
car1 = car1_data[0]
road1 = car1_data[1]
car2_data = share_pair[1]
car2 = car2_data[0]
road2 = car2_data[1]
q1 = (car1 - 1) * num_roads + (road1 - 1)
q2 = (car2 - 1) * num_roads + (road2 - 1)
Q[(q1, q1)] += 1
Q[(q2, q2)] += 1
if (q1, q2) not in Q.keys():
Q[(q1, q2)] = 2
else:
Q[(q1, q2)] += 2
return Q
def add_existence_constraint(group_size, number_of_groups, matrix):
Q = matrix
for current_group in range(group_size):
for current_city in range(number_of_groups):
group_offset = current_group * group_size
q1 = group_offset + current_city
Q[(q1, q1)] += -LAMBDA_VAL
for other_city in range(number_of_groups):
q2 = group_offset + other_city
if current_city != other_city:
Q[(q1, q2)] = 2 * LAMBDA_VAL
return Q
def add_uniqueness_constraint(size, matrix):
Q = matrix
for current_city in range(size):
for current_group in range(size):
offset1 = current_group * size
q1 = offset1 + current_city
Q[(q1, q1)] += -LAMBDA_VAL
for other_group in range(size):
offset2 = other_group * size
q2 = offset2 + current_city
if current_group != other_group:
Q[(q1, q2)] = 2 * LAMBDA_VAL
return Q
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。