修改模拟退火bug

This commit is contained in:
weixin_46229132 2025-03-24 19:28:24 +08:00
parent fe37f7ac0f
commit 6f44d142bc
8 changed files with 22 additions and 110 deletions

View File

@ -30,9 +30,9 @@ if __name__ == "__main__": # 重要:在 Windows 上必须加这一行
# --------------------------- # ---------------------------
# 需要修改的超参数 # 需要修改的超参数
# --------------------------- # ---------------------------
R = 3 R = 1
C = 1 C = 1
params_file = 'params2' params_file = 'params3'
batch_size = 60 # 控制一次最多并行多少个任务 batch_size = 60 # 控制一次最多并行多少个任务
with open(params_file + '.yml', 'r', encoding='utf-8') as file: with open(params_file + '.yml', 'r', encoding='utf-8') as file:

View File

@ -5,7 +5,7 @@ import yaml
class SA_FineTuner: class SA_FineTuner:
def __init__(self, initial_row_cuts, initial_col_cuts, car_paths, max_iterations=1000, initial_temp=100, cooling_rate=0.95): def __init__(self, params_file, initial_row_cuts, initial_col_cuts, car_paths, max_iterations=1000, initial_temp=100, cooling_rate=0.95):
""" """
初始化模拟退火算法 初始化模拟退火算法
:param initial_row_cuts: 初始行切分比例列表例如 [0.1, 0.3, 0.7] :param initial_row_cuts: 初始行切分比例列表例如 [0.1, 0.3, 0.7]
@ -16,7 +16,7 @@ class SA_FineTuner:
:param cooling_rate: 温度下降速率 :param cooling_rate: 温度下降速率
""" """
# 读取参数 # 读取参数
with open('params3.yml', 'r', encoding='utf-8') as file: with open(params_file + '.yml', 'r', encoding='utf-8') as file:
params = yaml.safe_load(file) params = yaml.safe_load(file)
self.H = params['H'] self.H = params['H']
@ -137,9 +137,9 @@ class SA_FineTuner:
second_point = car_path[i + 1] second_point = car_path[i + 1]
car_time += math.dist( car_time += math.dist(
rectangles[first_point]['center'], rectangles[second_point]['center']) * self.car_time_factor rectangles[first_point]['center'], rectangles[second_point]['center']) * self.car_time_factor
car_time += math.dist(rectangles[0]['center'], car_time += math.dist(rectangles[car_path[0]]['center'],
[self.H / 2, self.W / 2]) * self.car_time_factor [self.H / 2, self.W / 2]) * self.car_time_factor
car_time += math.dist(rectangles[-1]['center'], car_time += math.dist(rectangles[car_path[-1]]['center'],
[self.H / 2, self.W / 2]) * self.car_time_factor [self.H / 2, self.W / 2]) * self.car_time_factor
mortorcade_time_lt.append(max(car_time + flight_time, bs_time)) mortorcade_time_lt.append(max(car_time + flight_time, bs_time))
@ -206,15 +206,16 @@ if __name__ == "__main__":
# --------------------------- # ---------------------------
# 需要修改的超参数 # 需要修改的超参数
# --------------------------- # ---------------------------
file_path = r"solutions\trav_ga_params2_parallel.json" solution_path = r"solutions\mtkl_params2.json"
params_file = r"params2"
max_iterations=10000 max_iterations=10000
initial_temp=100 initial_temp=100
cooling_rate=0.95 cooling_rate=0.95
initial_row_cuts, initial_col_cuts, car_paths = load_initial_solution( initial_row_cuts, initial_col_cuts, car_paths = load_initial_solution(
file_path) solution_path)
sa = SA_FineTuner(initial_row_cuts, initial_col_cuts, car_paths, max_iterations, initial_temp, cooling_rate) sa = SA_FineTuner(params_file, initial_row_cuts, initial_col_cuts, car_paths, max_iterations, initial_temp, cooling_rate)
best_row_cuts, best_col_cuts, best_T = sa.run() best_row_cuts, best_col_cuts, best_T = sa.run()
# 输出结果 # 输出结果

View File

@ -17,7 +17,6 @@ num_iterations = 10000
# C = random.randint(0, 3) # 列分段数 # C = random.randint(0, 3) # 列分段数
R = 3 R = 3
C = 1 C = 1
params_file = 'params2' params_file = 'params2'

View File

@ -1,54 +0,0 @@
{
"row_boundaries": [
0.0,
0.3,
0.6,
0.8,
1.0
],
"col_boundaries": [
0.0,
0.5,
1.0
],
"car_paths": [
[
[
22.5,
37.5
],
[
7.5,
37.5
]
],
[
[
7.5,
12.5
],
[
45.0,
37.5
]
],
[
[
22.5,
12.5
],
[
35.0,
12.5
],
[
35.0,
37.5
],
[
45.0,
12.5
]
]
]
}

View File

@ -1,7 +1,7 @@
{ {
"row_boundaries": [ "row_boundaries": [
0.0, 0.0,
0.2, 0.1,
0.4, 0.4,
0.7, 0.7,
1.0 1.0
@ -12,19 +12,19 @@
1.0 1.0
], ],
"car_paths": [ "car_paths": [
[
7,
6
],
[ [
4, 4,
2, 2,
0 0
], ],
[ [
1, 5,
3, 3,
5 1
],
[
7,
6
] ]
] ]
} }

View File

@ -1,34 +0,0 @@
{
"row_boundaries": [
0.0,
0.3,
1.0
],
"col_boundaries": [
0.0,
0.4,
1.0
],
"car_paths": [
[
[
19.5,
21.0
]
],
[
[
4.5,
6.0
],
[
4.5,
21.0
],
[
19.5,
6.0
]
]
]
}

View File

@ -11,12 +11,12 @@
], ],
"car_paths": [ "car_paths": [
[ [
0, 2,
2 0
], ],
[ [
1, 3,
3 1
] ]
] ]
} }

View File

@ -53,7 +53,7 @@ if __name__ == "__main__":
# 需要修改的超参数 # 需要修改的超参数
# --------------------------- # ---------------------------
params_file = 'params2' params_file = 'params2'
solution_file = r'solutions\trav_ga_params2_parallel.json' solution_file = r'solutions\mtkl_params2.json'
with open(params_file + '.yml', 'r', encoding='utf-8') as file: with open(params_file + '.yml', 'r', encoding='utf-8') as file:
params = yaml.safe_load(file) params = yaml.safe_load(file)