一種多群體智能優化算法,其核心思想是通過兩個分工明確的群體——精英群和探索群——協同工作,平衡算法的全局探索與局部開發能力,從而提高收斂精度并避免早熟收斂。
一 核心概念
在傳統優化算法(如粒子群優化、遺傳算法)中,單一的搜索策略往往難以兼顧搜索廣度和精度。例如:
全局探索不足:易陷入局部最優。
局部開發不夠:難以精細調優解的質量。
EEDSCO的突破:
分工明確:精英群專注局部開發(利用好解),探索群負責全局探索(發現新區域)。
動態協同:兩群通過信息交互,共享搜索經驗,實現互補。
二 算法步驟
步驟一:初始化
將總群體隨機分配為兩個子群:
精英群(Elite Swarm):由適應度高的個體組成,負責深度開發。
探索群(Exploration Swarm):適應度較低或隨機生成的個體,負責廣域搜索。
設定參數:兩群比例、信息交換頻率、收斂閾值等。
# 初始化群體self.population = np.random.uniform(-10, 10, (pop_size, dim))self.fitness = np.array([sphere_func(ind) for ind in self.population])# 初始劃分精英群和探索群self.split_swarms()def split_swarms(self):"""根據適應度劃分精英群和探索群"""sorted_indices = np.argsort(self.fitness)elite_size = int(self.pop_size * self.elite_ratio)self.elite_swarm = self.population[sorted_indices[:elite_size]] # 精英群:適應度最好的一部分self.explore_swarm = self.population[sorted_indices[elite_size:]] # 探索群:其余個體
self.dim = dim # 問題維度
self.pop_size = pop_size # 總群體大小
self.elite_ratio = elite_ratio # 精英群比例
self.max_iter = max_iter # 最大迭代次數
self.exchange_freq = 5 # 信息交換頻率
self.global_best = None # 全局最優解
self.convergence = [] # 收斂曲線記錄
步驟二:協同迭代
While (未達到終止條件):# 精英群操作:局部開發精英群更新策略(如梯度下降、小步長變異)更新精英群個體并保留歷史最優解# 探索群操作:全局探索探索群更新策略(如大變異幅度、隨機跳躍)探索新區域并記錄潛在優質解# 信息交互if 達到信息交換頻率:精英群接收探索群中發現的高質量解探索群引入精英群的引導方向# 動態調整根據收斂程度調整群體比例或搜索范圍
?代碼示例:
# ---- 精英群操作: 局部開發 ----def update_elite(self):"""精英群: 小步長高斯擾動 + 梯度引導"""for i in range(len(self.elite_swarm)):# 高斯變異(局部微調)mutation = np.random.normal(0, 0.1, self.dim) # 標準差較小candidate = self.elite_swarm[i] + mutation# 梯度方向引導(模擬梯度下降)gradient = 2 * self.elite_swarm[i] # Sphere函數梯度解析解candidate = candidate - 0.05 * gradient# 更新精英個體if sphere_func(candidate) < self.fitness[i]:self.elite_swarm[i] = candidate# ---- 探索群操作: 全局探索 ----def update_explore(self):"""探索群: Lévy飛行 + 隨機重啟"""for i in range(len(self.explore_swarm)):# 以一定概率進行隨機重啟(跳出局部區域)if np.random.rand() < 0.1:self.explore_swarm[i] = np.random.uniform(-10, 10, self.dim)continue# Lévy飛行生成步長(長尾分布,允許大跳躍)step = np.random.standard_cauchy(self.dim) * 0.5candidate = self.explore_swarm[i] + step# 確保不越界candidate = np.clip(candidate, -10, 10)# 更新探索個體if sphere_func(candidate) < sphere_func(self.explore_swarm[i]):self.explore_swarm[i] = candidate# ---- 信息交互機制 ----def exchange_information(self):"""精英群與探索群交互:遷移最優解"""# 探索群中前10%個體遷入精英群explore_fitness = np.array([sphere_func(x) for x in self.explore_swarm])top_k = int(0.1 * len(self.explore_swarm))best_indices = np.argsort(explore_fitness)[:top_k]# 精英群移除適應度最差的個體,騰出空間elite_fitness = np.array([sphere_func(x) for x in self.elite_swarm])worst_idx = np.argmax(elite_fitness)# 替換操作self.elite_swarm[worst_idx] = self.explore_swarm[best_indices[0]]self.explore_swarm = np.delete(self.explore_swarm, best_indices[0], axis=0)
三 關鍵策略
3.1 精英群的深度開發
策略:
小范圍變異(如高斯變異)。
梯度方向跟蹤(適用于連續優化問題)。
模擬退火的鄰域搜索(組合優化場景)。
特點:
避免“過開發”:通過適應度方差檢測早熟,必要時重置部分個體。
3.2 探索群的廣域搜索
策略:
Lévy飛行(大跨度跳躍,兼顧長距離與短距離搜索)。
隨機重啟(以一定概率重置個體位置)。
反向學習(生成對稱解,擴展搜索空間)。
特點:
強制多樣性:引入排斥機制,避免個體聚集。
3.3 信息交互機制
- 精英←探索:探索群中適應度前N%的個體遷移至精英群。
- 精英→探索:精英群的全局最優解作為“引力點”,引導探索群方向。
- 頻率控制:初期高頻交互提升效率,后期降低頻率避免干擾收斂。
四 參數設置
- 群體比例:通常精英群占20%~40%,可根據問題復雜度調整。
- 信息交換頻率:每5~10代交互一次。
- 探索步長:隨迭代次數指數衰減,平衡早期探索與后期收斂。
- 自適應機制:
- 若精英群適應度長期不變,增大探索群比例。
- 若探索群發現更優解,觸發精英群重置。
五 適用場景
適用場景:
多模態優化(如Rastrigin函數)。
高維復雜問題(如神經網絡超參數優化)。
實際工程問題(如物流路徑規劃、電力系統調度)。
優勢:
全局最優概率高:兩群互補降低漏解風險。
收斂速度快:精英群的局部開發快速提升解質量。
魯棒性強:動態參數適應不同問題。
六 代碼示例
import numpy as np
import matplotlib.pyplot as plt# === 目標函數: Sphere函數 (最小化) ===
def sphere_func(x):return np.sum(x**2)# === EEDSCO算法類 ===
class EEDSCO:def __init__(self, dim=2, pop_size=50, elite_ratio=0.3, max_iter=100):# 參數設置self.dim = dim # 問題維度self.pop_size = pop_size # 總群體大小self.elite_ratio = elite_ratio # 精英群比例self.max_iter = max_iter # 最大迭代次數self.exchange_freq = 5 # 信息交換頻率self.global_best = None # 全局最優解self.convergence = [] # 收斂曲線記錄# 初始化群體self.population = np.random.uniform(-10, 10, (pop_size, dim))self.fitness = np.array([sphere_func(ind) for ind in self.population])# 初始劃分精英群和探索群self.split_swarms()def split_swarms(self):"""根據適應度劃分精英群和探索群"""sorted_indices = np.argsort(self.fitness)elite_size = int(self.pop_size * self.elite_ratio)self.elite_swarm = self.population[sorted_indices[:elite_size]] # 精英群:適應度最好的一部分self.explore_swarm = self.population[sorted_indices[elite_size:]] # 探索群:其余個體# ---- 精英群操作: 局部開發 ----def update_elite(self):"""精英群: 小步長高斯擾動 + 梯度引導"""for i in range(len(self.elite_swarm)):# 高斯變異(局部微調)mutation = np.random.normal(0, 0.1, self.dim) # 標準差較小candidate = self.elite_swarm[i] + mutation# 梯度方向引導(模擬梯度下降)gradient = 2 * self.elite_swarm[i] # Sphere函數梯度解析解candidate = candidate - 0.05 * gradient# 更新精英個體if sphere_func(candidate) < self.fitness[i]:self.elite_swarm[i] = candidate# ---- 探索群操作: 全局探索 ----def update_explore(self):"""探索群: Lévy飛行 + 隨機重啟"""for i in range(len(self.explore_swarm)):# 以一定概率進行隨機重啟(跳出局部區域)if np.random.rand() < 0.1:self.explore_swarm[i] = np.random.uniform(-10, 10, self.dim)continue# Lévy飛行生成步長(長尾分布,允許大跳躍)step = np.random.standard_cauchy(self.dim) * 0.5candidate = self.explore_swarm[i] + step# 確保不越界candidate = np.clip(candidate, -10, 10)# 更新探索個體if sphere_func(candidate) < sphere_func(self.explore_swarm[i]):self.explore_swarm[i] = candidate# ---- 信息交互機制 ----def exchange_information(self):"""精英群與探索群交互:遷移最優解"""# 探索群中前10%個體遷入精英群explore_fitness = np.array([sphere_func(x) for x in self.explore_swarm])top_k = int(0.1 * len(self.explore_swarm))best_indices = np.argsort(explore_fitness)[:top_k]# 精英群移除適應度最差的個體,騰出空間elite_fitness = np.array([sphere_func(x) for x in self.elite_swarm])worst_idx = np.argmax(elite_fitness)# 替換操作self.elite_swarm[worst_idx] = self.explore_swarm[best_indices[0]]self.explore_swarm = np.delete(self.explore_swarm, best_indices[0], axis=0)# ---- 主優化循環 ----def optimize(self):# 初始全局最優self.global_best = self.elite_swarm[0]best_fitness = sphere_func(self.global_best)self.convergence.append(best_fitness)for iter in range(self.max_iter):# 更新兩個子群self.update_elite() # 精英群局部開發self.update_explore() # 探索群全局探索# 合并群體并更新全局最優combined_pop = np.vstack([self.elite_swarm, self.explore_swarm])current_best = combined_pop[np.argmin([sphere_func(x) for x in combined_pop])]if sphere_func(current_best) < best_fitness:self.global_best = current_best.copy()best_fitness = sphere_func(current_best)self.convergence.append(best_fitness)# 周期性信息交互if iter % self.exchange_freq == 0:self.exchange_information()return self.global_best, self.convergence# === 算法測試與可視化 ===
if __name__ == "__main__":eedsco = EEDSCO(dim=10, pop_size=50, max_iter=100)best_solution, convergence = eedsco.optimize()print(f"全局最優解: {best_solution}")print(f"最優適應度: {sphere_func(best_solution)}")# 繪制收斂曲線plt.plot(convergence)plt.title("EEDSCO收斂曲線")plt.xlabel("迭代次數")plt.ylabel("適應度")plt.yscale('log')plt.show()
輸出為:
但是這個輸出的效果不是很理想,可以通過修改參數來優化。