模拟退火算法(Simulate Anneal,SA)是一种通用概率演算法,用来在一个大的搜寻空间内找寻命题的最优解。模拟退火是由S.Kirkpatrick, C.D.Gelatt和M.P.Vecchi在1983年所发明的。V.Černý在1985年也独立发明此演算法。模拟退火算法是解决TSP问题的有效方法之一。
模拟退火的出发点是基于物理中固体物质的退火过程与一般组合优化问题之间的相似性。模拟退火算法是一种通用的优化算法,其物理退火过程由加温过程、等温过程、冷却过程这三部分组成。(by 百度百科)
本文讨论用python实现简单模拟退火法,来求函数在某个区间范围内的最大/最小值。
1. 模拟退火算法
基本流程图:
下面是模拟退火算法的主程序simAnneal.py,实现了算一维,二维及多维函数在给定区间内的最大/最小值。
# -*- coding: utf-8 -*-
'''
=========================================
| kiterun |
| 2017/08/11 |
| kiterun@126.com |
=========================================
'''
from random import random
import math
import sys
class SimAnneal(object):
'''
Simulated annealing algorithm
'''
def __init__(self, target_text='min'):
self.target_text = target_text
def newVar(self, oldList, T):
'''
:old : list
:return : list, new solutions based on old solutions
:T : current temperature
'''
newList = [i + (random()*2-1) for i in oldList]
return newList
def juge(self, func, new, old, T):
'''
matropolise conditions: to get the maximun or minmun
:new : new solution data from self.newX
:old : old solution data
:T : current temperature
'''
dE = func(new) - func(old) if self.target_text == 'max' else func(old) - func(new)
if dE >= 0:
x, ans = new, func(new)
else:
if math.exp(dE/T) > random():
x, ans = new,func(new)
else:
x, ans = old, func(old)
return [x, ans]
class OptSolution(object):
'''
find the optimal solution.
'''
def __init__(self, temperature0=100, temDelta=0.98,
temFinal=1e-8, Markov_chain=2000,
result=0, val_nd=[0]):
# initial temperature
self.temperature0 = temperature0
# step factor for decreasing temperature
self.temDelta = temDelta
# the final temperature
self.temFinal = temFinal
# the Markov_chain length (inner loops numbers)
self.Markov_chain = Markov_chain
# the final result
self.result = result
# the initial coordidate values: 1D [0], 2D [0,0] ...
self.val_nd = val_nd
def mapRange<