修改蒙特卡洛的输出
This commit is contained in:
parent
9599215e2e
commit
61be8ad37c
@ -45,13 +45,15 @@ for iteration in range(num_iterations):
|
||||
C = 1
|
||||
|
||||
# 生成随机的行、列分割边界
|
||||
horiz = [np.clip(np.floor(random.random() * 10) /10, 0.0, 0.9) for _ in range(R)]
|
||||
horiz = [np.clip(np.floor(random.random() * 10) / 10, 0.0, 0.9)
|
||||
for _ in range(R)]
|
||||
horiz = sorted(set(horiz))
|
||||
horiz = horiz if horiz else []
|
||||
row_boundaries = [0] + horiz + [1]
|
||||
row_boundaries = [boundary * H for boundary in row_boundaries]
|
||||
|
||||
vert = [np.clip(np.floor(random.random() * 10) /10, 0.0, 0.9) for _ in range(C)]
|
||||
vert = [np.clip(np.floor(random.random() * 10) / 10, 0.0, 0.9)
|
||||
for _ in range(C)]
|
||||
vert = sorted(set(vert))
|
||||
vert = vert if vert else []
|
||||
col_boundaries = [0] + vert + [1]
|
||||
@ -108,10 +110,11 @@ for iteration in range(num_iterations):
|
||||
# ---------------------------
|
||||
# 随机将所有矩形任务分配给 k 个系统(车-机-巢)
|
||||
# ---------------------------
|
||||
system_tasks = {i: [] for i in range(k)}
|
||||
for rect in rectangles:
|
||||
system = random.randint(0, k - 1)
|
||||
system_tasks[system].append(rect)
|
||||
car_paths = [[] for _ in range(k)]
|
||||
for i in range(len(row_boundaries) - 1):
|
||||
for j in range(len(col_boundaries) - 1):
|
||||
car_idx = random.randint(0, k - 1)
|
||||
car_paths[car_idx].append(i * (len(col_boundaries) - 1) + j)
|
||||
|
||||
# ---------------------------
|
||||
# 对于每个系统,计算该系统的总完成时间 T_k:
|
||||
@ -121,18 +124,19 @@ for iteration in range(num_iterations):
|
||||
region_center = (H / 2.0, W / 2.0)
|
||||
T_k_list = []
|
||||
for i in range(k):
|
||||
tasks = system_tasks[i]
|
||||
tasks.sort(key=lambda r: math.hypot(r['center'][0] - region_center[0],
|
||||
r['center'][1] - region_center[1]))
|
||||
total_flight_time = sum(task['flight_time'] for task in tasks)
|
||||
if tasks:
|
||||
car_path = car_paths[i]
|
||||
car_path.sort(key=lambda r: math.dist(
|
||||
rectangles[r]['center'], region_center))
|
||||
total_flight_time = sum(
|
||||
rectangles[point]['flight_time'] for point in car_path)
|
||||
if car_path:
|
||||
# 车辆从区域中心到第一个任务中心
|
||||
car_time = math.dist(tasks[0]['center'],
|
||||
car_time = math.dist(rectangles[car_path[0]]['center'],
|
||||
region_center) * car_time_factor
|
||||
# 依次经过任务中心
|
||||
for j in range(len(tasks) - 1):
|
||||
prev_center = tasks[j]['center']
|
||||
curr_center = tasks[j + 1]['center']
|
||||
for j in range(len(car_path) - 1):
|
||||
prev_center = rectangles[car_path[j]]['center']
|
||||
curr_center = rectangles[car_path[j + 1]]['center']
|
||||
car_time += math.dist(curr_center,
|
||||
prev_center) * car_time_factor
|
||||
# 回到区域中心
|
||||
@ -141,7 +145,7 @@ for iteration in range(num_iterations):
|
||||
car_time = 0
|
||||
|
||||
# 机巢的计算时间
|
||||
total_bs_time = sum(task['bs_time'] for task in tasks)
|
||||
total_bs_time = sum(rectangles[point]['bs_time'] for point in car_path)
|
||||
T_k = max(total_flight_time + car_time, total_bs_time)
|
||||
T_k_list.append(T_k)
|
||||
|
||||
@ -152,7 +156,7 @@ for iteration in range(num_iterations):
|
||||
if T_max < best_T:
|
||||
best_T = T_max
|
||||
best_solution = {
|
||||
'system_tasks': system_tasks,
|
||||
'car_paths': car_paths,
|
||||
'T_k_list': T_k_list,
|
||||
'T_max': T_max,
|
||||
'iteration': iteration,
|
||||
@ -169,40 +173,14 @@ for iteration in range(num_iterations):
|
||||
# 输出最佳方案
|
||||
# ---------------------------
|
||||
if best_solution is not None:
|
||||
print("最佳 T (各系统中最长的完成时间):", best_solution['T_max'])
|
||||
print(best_solution['iteration'], "次模拟后找到最佳方案:")
|
||||
print("分区情况:")
|
||||
print("行分段数:", best_solution['R'])
|
||||
print("列分段数:", best_solution['C'])
|
||||
print("行分割边界:", best_solution['row_boundaries'])
|
||||
print("列分割边界:", best_solution['col_boundaries'])
|
||||
print("每辆车的运行轨迹情况:")
|
||||
car_paths = []
|
||||
for i in range(k):
|
||||
num_tasks = len(best_solution['system_tasks'][i])
|
||||
print(
|
||||
f"系统 {i}: 完成时间 T = {best_solution['T_k_list'][i]}, 飞行任务数量: {num_tasks}")
|
||||
tasks = best_solution['system_tasks'][i]
|
||||
tasks.sort(key=lambda r: math.hypot(r['center'][0] - region_center[0],
|
||||
r['center'][1] - region_center[1]))
|
||||
if tasks:
|
||||
print(
|
||||
f"轨迹路线: 区域中心({region_center[0]:.1f}, {region_center[1]:.1f})", end="")
|
||||
current_pos = region_center
|
||||
car_path = []
|
||||
for j, task in enumerate(tasks, 1):
|
||||
current_pos = task['center']
|
||||
car_path.append(current_pos)
|
||||
print(
|
||||
f" -> 任务{j}({current_pos[0]:.1f}, {current_pos[1]:.1f})", end="")
|
||||
print(" -> 区域中心")
|
||||
car_paths.append(car_path)
|
||||
print("最佳 T:", best_solution['T_max'])
|
||||
print("最佳路径:", best_solution['car_paths'])
|
||||
|
||||
# 保存分区边界和车辆轨迹到JSON文件
|
||||
output_data = {
|
||||
'row_boundaries': [boundary / H for boundary in best_solution['row_boundaries']],
|
||||
'col_boundaries': [boundary / W for boundary in best_solution['col_boundaries']],
|
||||
'car_paths': car_paths
|
||||
'car_paths': best_solution['car_paths']
|
||||
}
|
||||
with open(f'./solutions/mtkl_{params_file}.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(output_data, f, ensure_ascii=False, indent=4)
|
||||
|
@ -11,10 +11,12 @@
|
||||
],
|
||||
"car_paths": [
|
||||
[
|
||||
0, 2
|
||||
0,
|
||||
2
|
||||
],
|
||||
[
|
||||
1, 3
|
||||
1,
|
||||
3
|
||||
]
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user