混沌優(yōu)化算法(COA):從理論到實踐的探索之旅
混沌理論揭示了確定性系統(tǒng)中隱藏的復(fù)雜性和不可預(yù)測性,而混沌優(yōu)化算法正是借鑒了混沌系統(tǒng)對初始條件的敏感性、遍歷性和內(nèi)在的隨機性,通過模擬混沌動態(tài)過程來探索優(yōu)化問題的解空間。
圖片
這種算法不僅能夠有效避免陷入局部最優(yōu),還能在全局范圍內(nèi)高效搜索,展現(xiàn)出強大的適應(yīng)性和靈活性。
今天,我們要揭開這個神秘而強大的智能優(yōu)化算法的面紗——混沌優(yōu)化算法。
一、混沌理論的魅力
提到混沌,你可能會想起蝴蝶效應(yīng)——一只蝴蝶在巴西扇動翅膀,可能會在美國德克薩斯州引發(fā)一場龍卷風(fēng)。
圖片
這個看似荒誕不經(jīng)的比喻,正是混沌理論的核心特征之一:初始條件的敏感性。
混沌系統(tǒng)對初始條件極為敏感,即使微小的差異也會在迭代過程中被迅速放大,最終導(dǎo)致截然不同的結(jié)果。
圖片
但混沌并非完全無序。它具有一種獨特的“偽隨機性”和“遍歷性”。
混沌系統(tǒng)雖然看似雜亂無章,但其運動軌跡卻能在整個可行空間內(nèi)均勻分布,且不會重復(fù)經(jīng)過同一個點。
這種特性使得混沌系統(tǒng)在搜索過程中能夠高效地探索整個解空間,避免陷入局部最優(yōu)。
圖片
▲ Logistic混沌映射的分叉圖
混沌映射是混沌理論在優(yōu)化算法中的重要應(yīng)用。常見的混沌映射有Logistic映射和Tent映射。
Logistic映射是一個簡單的非線性方程,其迭代過程卻能產(chǎn)生復(fù)雜的混沌行為。
Tent映射則以其線性分段的特性,展現(xiàn)出快速的遍歷性和良好的隨機性。
這些混沌映射為混沌優(yōu)化算法提供了強大的動力源泉。
二、混沌優(yōu)化算法的原理與流程
混沌優(yōu)化算法的核心思想是將混沌變量引入優(yōu)化問題的變量空間,利用混沌運動的遍歷性來搜索全局最優(yōu)解。
它通過混沌映射生成混沌變量,這些變量在迭代過程中不斷變化,從而驅(qū)動優(yōu)化變量在解空間中進行高效的搜索。
混沌優(yōu)化算法的實現(xiàn)步驟如下:
1.初始化混沌變量
首先,根據(jù)優(yōu)化問題的規(guī)模和維度,初始化混沌變量。
這些變量通常在[0,1]區(qū)間內(nèi)均勻分布,通過混沌映射進行迭代更新。
圖片
2.混沌變量迭代

3.優(yōu)化搜索

將轉(zhuǎn)換后的優(yōu)化變量代入目標(biāo)函數(shù),計算其適應(yīng)度值。
根據(jù)適應(yīng)度值的大小,選擇最優(yōu)解作為當(dāng)前迭代的候選解。
4.終止條件判斷
最后,當(dāng)達到預(yù)設(shè)的迭代次數(shù)或適應(yīng)度值收斂時,算法終止,輸出全局最優(yōu)解。
三、混沌優(yōu)化算法的案例演示
為了更好地理解混沌優(yōu)化算法的工作原理,我們以Ackley函數(shù)為例進行優(yōu)化。
1.問題定義
Ackley函數(shù)是一個經(jīng)典的多峰函數(shù),具有復(fù)雜的地形和多個局部最小值,常用于測試優(yōu)化算法的性能。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
import time
import math
# 設(shè)置中文顯示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# Ackley函數(shù)
defackley(x):
a = 20
b = 0.2
c = 2 * np.pi
d = len(x)
sum_sq = sum([xi**2for xi in x])
sum_cos = sum([np.cos(c * xi) for xi in x])
term1 = -a * np.exp(-b * np.sqrt(sum_sq / d))
term2 = -np.exp(sum_cos / d)
return term1 + term2 + a + np.exp(1)2.算法建模
在實現(xiàn)混沌優(yōu)化算法時,我們選擇了Logistic混沌映射作為混沌序列的生成方式。
# Logistic混沌映射
deflogistic_map(x, mu=4.0):
return mu * x * (1 - x)
# 混沌優(yōu)化算法
defchaos_optimization(obj_func, dim, lb, ub, max_iter, chaos_iter=1000):
"""
參數(shù):
- obj_func: 目標(biāo)函數(shù)
- dim: 問題維度
- lb: 下界
- ub: 上界
- max_iter: 最大迭代次數(shù)
- chaos_iter: 混沌迭代次數(shù)
"""
# 初始化混沌變量
chaos_vars = np.random.rand(dim)
# 生成混沌序列
chaos_sequence = []
for _ inrange(chaos_iter):
chaos_vars = logistic_map(chaos_vars)
chaos_sequence.append(chaos_vars.copy())
chaos_sequence = np.array(chaos_sequence)
# 將混沌序列映射到搜索空間
search_points = lb + (ub - lb) * chaos_sequence
# 評估初始解
fitness = np.array([obj_func(p) for p in search_points])
best_idx = np.argmin(fitness)
best_position = search_points[best_idx].copy()
best_fitness = fitness[best_idx]
# 記錄歷史
history = {
'positions': [search_points.copy()],
'best_position': [best_position.copy()],
'best_fitness': [best_fitness],
'current_iter': [0]
}
# 算法開始
print("="*50)
print("混沌優(yōu)化算法(COA)開始運行")
print(f"搜索空間維度: {dim}")
print(f"搜索范圍: [{lb}, {ub}]")
print(f"最大迭代次數(shù): {max_iter}")
print(f"混沌迭代次數(shù): {chaos_iter}")
print(f"初始最佳適應(yīng)度: {best_fitness:.6f}")
print("="*50)
time.sleep(1)
# 二次載波搜索
foriterinrange(max_iter):
# 縮小搜索范圍
current_lb = np.maximum(lb, best_position - (ub - lb) * 0.9**(iter+1))
current_ub = np.minimum(ub, best_position + (ub - lb) * 0.9**(iter+1))
# 生成新的混沌序列
new_chaos_vars = np.random.rand(dim)
new_search_points = []
for _ inrange(chaos_iter):
new_chaos_vars = logistic_map(new_chaos_vars)
new_point = current_lb + (current_ub - current_lb) * new_chaos_vars
new_search_points.append(new_point)
new_search_points = np.array(new_search_points)
# 評估新解
new_fitness = np.array([obj_func(p) for p in new_search_points])
current_best_idx = np.argmin(new_fitness)
current_best_position = new_search_points[current_best_idx].copy()
current_best_fitness = new_fitness[current_best_idx]
# 更新最優(yōu)解
if current_best_fitness < best_fitness:
best_position = current_best_position.copy()
best_fitness = current_best_fitness
# 記錄歷史
history['positions'].append(new_search_points.copy())
history['best_position'].append(best_position.copy())
history['best_fitness'].append(best_fitness)
history['current_iter'].append(iter+1)
# 打印進度
ifiter % 10 == 0oriter == max_iter-1:
print(f"迭代 {iter+1:3d}/{max_iter} | 當(dāng)前最佳適應(yīng)度: {best_fitness:.6f}")
print("="*50)
print("優(yōu)化完成!")
print(f"找到的最佳解: {best_position}")
print(f"最佳適應(yīng)度值: {best_fitness:.6f}")
print("="*50)
time.sleep(1)
print("生成可視化結(jié)果...")
time.sleep(1)
return history3.結(jié)果可視化
為了更直觀地展示混沌優(yōu)化算法的優(yōu)化過程,我們通過Matplotlib繪制了3D曲面圖、2D等高線圖和收斂曲線。
# 參數(shù)設(shè)置
dim = 2
lb = -5
ub = 5
max_iter = 50
chaos_iter = 500
# 運行算法
history = chaos_optimization(ackley, dim, lb, ub, max_iter, chaos_iter)
# 準(zhǔn)備可視化數(shù)據(jù)
x = np.linspace(lb, ub, 100)
y = np.linspace(lb, ub, 100)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
for i inrange(X.shape[0]):
for j inrange(X.shape[1]):
Z[i,j] = ackley([X[i,j], Y[i,j]])
# 創(chuàng)建可視化圖形
fig = plt.figure(figsize=(18, 6), dpi=100)
fig.suptitle('混沌優(yōu)化算法優(yōu)化過程', fontsize=16)
# 統(tǒng)一子圖尺寸
gs = fig.add_gridspec(2, 3, width_ratios=[1, 1, 1], height_ratios=[1, 1])
# 3D曲面圖
ax1 = fig.add_subplot(gs[:, 0], projection='3d')
surf = ax1.plot_surface(X, Y, Z, cmap='viridis', alpha=0.6)
fig.colorbar(surf, ax=ax1, shrink=0.6, aspect=10, label='函數(shù)值')
scatter = ax1.scatter([], [], [], c='red', s=10, alpha=0.5, label='混沌搜索點')
best_scatter = ax1.scatter([], [], [], c='blue', marker='*', s=200, label='最優(yōu)解')
ax1.set_title('3D函數(shù)曲面與混沌搜索', fontsize=12)
ax1.set_xlabel('x1', fontsize=10)
ax1.set_ylabel('x2', fontsize=10)
ax1.set_zlabel('f(x)', fontsize=10)
ax1.legend(loc='upper right', fontsize=8)
# 2D等高線圖
ax2 = fig.add_subplot(gs[:, 1])
contour = ax2.contourf(X, Y, Z, levels=50, cmap='viridis')
fig.colorbar(contour, ax=ax2, shrink=0.6, aspect=10, label='函數(shù)值')
scatter2d = ax2.scatter([], [], c='red', s=10, alpha=0.5, label='混沌搜索點')
best_scatter2d = ax2.scatter([], [], c='blue', marker='*', s=100, label='最優(yōu)解')
search_area = plt.Rectangle((0,0), 0, 0, color='yellow', alpha=0.3, label='當(dāng)前搜索區(qū)域')
ax2.add_patch(search_area)
ax2.set_title('2D等高線與混沌搜索', fontsize=12)
ax2.set_xlabel('x1', fontsize=10)
ax2.set_ylabel('x2', fontsize=10)
ax2.legend(loc='upper right', fontsize=8)
# 收斂曲線
ax3 = fig.add_subplot(gs[0, 2])
convergence_line, = ax3.plot([], [], 'b-', linewidth=2, label='最佳適應(yīng)度')
current_point = ax3.scatter([], [], c='red', s=50, label='當(dāng)前值')
ax3.set_title('適應(yīng)度收斂曲線', fontsize=12)
ax3.set_xlabel('迭代次數(shù)', fontsize=10)
ax3.set_ylabel('適應(yīng)度值', fontsize=10)
ax3.grid(True, linestyle='--', alpha=0.6)
ax3.set_xlim(0, max_iter)
ax3.set_ylim(0, max(history['best_fitness']))
ax3.legend(loc='upper right', fontsize=8)
# 參數(shù)顯示
ax4 = fig.add_subplot(gs[1, 2])
ax4.axis('off')
info_text = ax4.text(0.1, 0.5, '', fontsize=10, bbox=dict(facecolor='white', alpha=0.8))
plt.tight_layout()
# 修改更新函數(shù)
defupdate(frame):
# 只顯示部分點避免過于密集
display_points = history['positions'][frame][::10]
# 更新3D圖
current_z = np.array([ackley(p) for p in display_points])
scatter._offsets3d = (display_points[:,0], display_points[:,1], current_z)
best_pos = history['best_position'][frame]
best_z = ackley(best_pos)
best_scatter._offsets3d = ([best_pos[0]], [best_pos[1]], [best_z])
# 更新2D圖
scatter2d.set_offsets(display_points)
best_scatter2d.set_offsets([best_pos])
# 初始化搜索區(qū)域
current_iter = history['current_iter'][frame]
if current_iter == 0:
# 第一幀使用全局搜索范圍
current_lb = np.array([lb, lb])
current_ub = np.array([ub, ub])
else:
# 后續(xù)幀縮小搜索范圍
current_lb = np.maximum(lb, best_pos - (ub - lb) * 0.9**current_iter)
current_ub = np.minimum(ub, best_pos + (ub - lb) * 0.9**current_iter)
# 更新搜索區(qū)域顯示
search_area.set_xy((current_lb[0], current_lb[1]))
search_area.set_width(current_ub[0] - current_lb[0])
search_area.set_height(current_ub[1] - current_lb[1])
# 更新收斂曲線
x_data = range(current_iter+1)
y_data = history['best_fitness'][:current_iter+1]
convergence_line.set_data(x_data, y_data)
current_point.set_offsets([[current_iter, history['best_fitness'][current_iter]]])
# 更新文本信息
info = f"迭代次數(shù): {current_iter}\n"
info += f"最佳適應(yīng)度: {history['best_fitness'][current_iter]:.6f}\n"
info += f"最佳位置: [{best_pos[0]:.4f}, {best_pos[1]:.4f}]\n"
info += f"搜索區(qū)域: [{current_lb[0]:.2f}, {current_ub[0]:.2f}] x [{current_lb[1]:.2f}, {current_ub[1]:.2f}]\n"
info += f"混沌迭代次數(shù): {chaos_iter}\n"
info += f"當(dāng)前搜索點數(shù): {len(history['positions'][frame])}"
info_text.set_text(info)
return scatter, best_scatter, scatter2d, best_scatter2d, search_area, convergence_line, current_point, info_text
# 創(chuàng)建動畫
ani = FuncAnimation(fig, update, frames=len(history['positions']), interval=500, blit=True)
# 顯示動畫
plt.close()
HTML(ani.to_jshtml())
圖片
結(jié)果顯示|結(jié)果輸出
經(jīng)過50次迭代,混沌優(yōu)化算法成功找到了Ackley函數(shù)的全局最優(yōu)解。
結(jié) 語
混沌優(yōu)化算法以其獨特的混沌理論基礎(chǔ)、強大的全局搜索能力和廣泛的應(yīng)用領(lǐng)域,在智能優(yōu)化領(lǐng)域展現(xiàn)出巨大的潛力。
它不僅能夠解決復(fù)雜的優(yōu)化問題,還能夠與其他優(yōu)化算法相結(jié)合,實現(xiàn)更高效的優(yōu)化搜索。
本文轉(zhuǎn)載自???Fairy Girl???,作者:Fairy Girl

















