import json from math import exp current, best = 0, 0 temperatures = [1, .5, .25] proposals = [1, 0, 2] uniforms = [.2, .9, .3] accepted = [] probabilities = [] for proposal, temperature, threshold in zip(proposals, temperatures, uniforms): # 目标是 x²。下坡概率为 1,上坡概率随温度降低而减小。 change = proposal ** 2 - current ** 2 probability = exp(-max(change, 0) / temperature) probabilities.append(probability) take = threshold < probability accepted.append(take) if take: current = proposal if current ** 2 < best ** 2: best = current assert accepted == [True, True, False] and best == 0 result = {"probabilities": probabilities, "accepted": accepted, "best_position": best, "final_position": current} print("LAB_RESULT " + json.dumps(result, ensure_ascii=False))