成功编写了遗传算法后,我现在正在编写一个模拟退火程序来与 GA 进行比较,但似乎无法让它达到任何一种最优,更不用说全局最优了。
经过一些测试,我认为问题似乎是将变量传递回while循环。出于测试目的,我已将代码更改为基本上只接受能量增量低于前一个解决方案的解决方案,因此我应该期望存储最佳解决方案的变量仅显示较低的数字,但实际上是波动的并且总是返回最多最近的解决方案是否更好。任何帮助将非常感激。
while temperature >0.01:
solutionlength = len(Solution)
NeighbourSolution = Solution
switch1 = i % solutionlength
i +=random.randint(0,100)
switch2 = i % solutionlength
NeighbourSolution[switch1], NeighbourSolution[switch2] = NeighbourSolution[switch2], NeighbourSolution[switch1]
EnergyOld = Solution.get_changeover_times()
EnergyNew = NeighbourSolution.get_changeover_times()
EnergyDelta = EnergyNew - EnergyOld
if EnergyDelta < 0:
acceptanceprob = 1
else:
acceptanceprob = 0 #math.exp(-EnergyDelta/temperature)
if acceptanceprob > 0: #random.random():
Solution = NeighbourSolution
if Solution.get_changeover_times() < bestsolution.get_changeover_times():
bestsolution = Solution
print (bestsolution.get_changeover_times())
temperature -= coolingrate