import math import random from pfsp_simulated_annealing.simulate import Simulate import os def main(): print(os.getcwd()) # 读取输入文件 try: with open('data/input.txt', 'r', encoding='utf-8') as f: lines = f.readlines() # 提取 n 和 m n, m = map(int, lines[0].split()) # 提取 p p = [] for line in lines[1:]: data = list(map(int, line.split())) # 跳过机器序号,从索引1开始,每隔两个元素提取一个元素(加工时间) for j in range(1, m + 1): p.append(data[2 * j - 1]) # 提取加工时间 print(f"工件数n: {n}, 机器数m: {m}") # 打印 n 和 m print(f"p: {p}") # 打印 p except FileNotFoundError: print("文件未找到!") return # 初始化模拟退火 sim = Simulate(n, m, p) sim.random_reset() current_t = 1061109567 count, count1 = 0, 0 trace = [] T = 10000.0 i = 0 while True: print(f"当前第{i}次计算, 当前耗时{current_t}") T *= 0.999 sim.random_exchange() sim.reset_for_new_round() new_t = sim.f() if new_t < current_t: #如果耗时更少,那就接收 current_t = new_t count = 0 count1 = 0 else: # 使用math.exp计算接受概率 if random.random() < math.exp((current_t - new_t) / T): if current_t == new_t: count1 += 1 else: count1 = 0 current_t = new_t count = 0 else: sim.roll_back() count += 1 if current_t == new_t: count1 += 1 if count > 100 or count1 > 100: #count > 100 || count1 > 100作为终止条件。 print("总完成时间为:", current_t) sim.output_data() with open('data/trace.txt', 'w') as f: f.write('\t'.join(map(str, trace))) return trace.append(current_t) i += 1 if __name__ == "__main__": main()