資源簡介
Python 粒子群和遺傳解決函數最值,模擬退火和蟻群解決TSP。

代碼片段和文件信息
import?random
import?math
import?matplotlib.pyplot?as?plt
import?numpy?as?np
pop_size?=?30
chorm_length?=?10
Max_value?=?2
pc?=?0.6
pm?=?0.015
pop?=?[]
pop_X?=?[]??#?對應十進制數
pop_fit?=?[]
bestvalue?=?[]
meanvalue?=?[]
Var?=?[]
def?encode():
????for?i?in?range(pop_size):
????????pop_i?=?[]
????????for?j?in?range(chorm_length):
????????????pop_i.append(random.randint(0?1))
????????pop.append(pop_i)
def?decode():
????pop_X.clear()
????for?i?in?range(pop_size):
????????value?=?0
????????for?j?in?range(chorm_length):
????????????value?+=?pop[i][j]*(2**(chorm_length?-?1?-?j))
????????pop_X.append(value?*?Max_value?/?(2**chorm_length?-?1))
def?cal_fit():
????decode()
????pop_fit.clear()
????fit_sum?=?0
????value?=?[]
????for?i?in?range(pop_size):
????????f_value?=?pop_X[i]?*?math.sin(10?*?3.1415?*?pop_X[i])?+?2
????????if?f_value?>?0.0:
????????????pop_fit.append(f_value)
????????????value.append(f_value)
????????else:
????????????pop_fit.append(0.0)
????????????value.append(0.0)
????????fit_sum?+=?pop_fit[i]
????for?i?in?range(pop_size):
????????pop_fit[i]?=?pop_fit[i]?/?fit_sum
????for?i?in?range(1?pop_size):
????????pop_fit[i]?+=?pop_fit[i?-?1]
????pop_fit[-1]?=?1
????return?value??#?返回初始版本的值
def?selection():
????random_selection?=?[]
????for?i?in?range(pop_size):
????????random_selection.append(random.random())
????random_selection.sort()
????new?=?[]
????selection_id?=?0
????global?pop
????for?i?in?range(pop_size):
????????while?selection_id?????????????new.append(pop[i])
????????????selection_id?+=?1
????pop?=?new
def?cross():
????for?i?in?range(0?pop_size?-?1?2):
????????if?random.random()?????????????point?=?random.randint(0?chorm_length?-?1)
????????????t1?=?[]
????????????t2?=?[]
????????????t1.extend(pop[i][0:point])
????????????t1.extend(pop[i?+?1][point:])
????????????t2.extend(pop[i?+?1][0:point])
????????????t2.extend(pop[i][point:])
????????????pop[i]?=?t1
????????????pop[i+1]?=?t2
def?abrupt():
????for?i?in?range(pop_size):
????????if?random.random()?????????????point?=?random.randint(0?chorm_length?-?1)
????????????if?pop[i][point]?==?0:
????????????????pop[i][point]?=?1
????????????else:
????????????????pop[i][point]?=?0
encode()
N?=?100
for?step?in?range(N):
????decode()
????eachvalue?=?cal_fit()
????meanvalue.append(sum(eachvalue)/pop_size)
????bestvalue.append(max(eachvalue))
????Var.append(np.var(eachvalue))
????plt.scatter([step?+?1]?*?pop_size?eachvalue)
????selection()
????cross()
????abrupt()
print(bestvalue)
plt.plot(range(1?N?+?1)?bestvalue?color=‘c‘?label=‘Best??‘+str(bestvalue[-1]))
plt.plot(range(1?N?+?1)?meanvalue?color=‘b‘?label=‘Mean??‘+str(meanvalue[-1]))
plt.plot(range(1?N?+?1)?Var?color=‘r‘?label=‘Var??‘+str(Var[-1]))
#?plt.title(‘X?-?Y‘)
#?plt.xlabel(‘X‘)
#?plt.ylabel(‘Y‘)
plt.legend()
plt.show()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-06-03?10:06??Algorithm\
?????文件???????28666??2018-06-02?15:29??Algorithm\GA?100.png
?????文件???????31569??2018-06-02?15:30??Algorithm\GA?1000.png
?????文件????????3025??2018-06-03?10:03??Algorithm\GA.py
?????文件????????2994??2018-06-03?09:21??Algorithm\模擬退火.py
?????文件???????41369??2018-06-03?08:29??Algorithm\粒子群???當前最優?平均?方差.png
?????文件???????28854??2018-06-02?23:33??Algorithm\粒子群.png
?????文件????????3252??2018-06-03?09:48??Algorithm\粒子群.py
?????文件???????92733??2018-06-02?15:33??Algorithm\蟻群?100.png
?????文件??????100638??2018-06-02?15:39??Algorithm\蟻群?1000.png
?????文件????????5528??2018-06-03?09:16??Algorithm\蟻群.py
評論
共有 條評論