本文主要是介绍差分进化算法 (DE) Python实现及源码解读,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
scikit-opt是一个一个封装了7种启发式算法的 Python 代码库(差分进化算法、遗传算法、粒子群算法、模拟退火算法、蚁群算法、鱼群算法、免疫优化算法)。源码具有很大的学习价值。这篇文章主要是根据差分进化(DE)算法的原理解读一下源码。
DE算法及源码解读
设函数,有三个未知解
def obj_func(p):x1, x2, x3 = preturn x1 ** 2 + x2 ** 2 + x3 ** 2
初始解形状
self.X = None # shape = (size_pop, n_dim)
生成初始解 (种群)(DE继承类GeneticAlgorithmBase)lb, ub为x的上下限,新解为约束条件上下界内均匀分布的向量
class DE(GeneticAlgorithmBase):def __init__(self, func, n_dim, F=0.5,size_pop=50, max_iter=200, prob_mut=0.3,lb=-1, ub=1,constraint_eq=tuple(), constraint_ueq=tuple()):super().__init__(func, n_dim, size_pop, max_iter, prob_mut,constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)self.F = Fself.V, self.U = None, Noneself.lb, self.ub = np.array(lb) * np.ones(self.n_dim), np.array(ub) * np.ones(self.n_dim)self.crtbp()def crtbp(self):# create the populationself.X = np.random.uniform(low=self.lb, high=self.ub, size=(self.size_pop, self.n_dim))return self.X
变异,产生新解向量V,如果V超出解的边界,保留原初始解。这里采用了最经典的变异方法,可以改进。
def mutation(self):'''V[i]=X[r1]+F(X[r2]-X[r3]),where i, r1, r2, r3 are randomly generated'''X = self.X# i is not needed,# and TODO: r1, r2, r3 should not be equalrandom_idx = np.random.randint(0, self.size_pop, size=(self.size_pop, 3))r1, r2, r3 = random_idx[:, 0], random_idx[:, 1], random_idx[:, 2]# 这里F用固定值,为了防止早熟,可以换成自适应值self.V = X[r1, :] + self.F * (X[r2, :] - X[r3, :])# the lower & upper bound still works in mutationmask = np.random.uniform(low=self.lb, high=self.ub, size=(self.size_pop, self.n_dim))self.V = np.where(self.V < self.lb, mask, self.V)self.V = np.where(self.V > self.ub, mask, self.V)return self.V
交叉操作,对初始解X和变异解V进行概率交叉,生成解向量U,这里的self.prob_mut为CR
def crossover(self):'''if rand < prob_crossover, use V, else use X'''mask = np.random.rand(self.size_pop, self.n_dim) < self.prob_mutself.U = np.where(mask, self.V, self.X)return self.U
选择操作,根据函数值大小选择是否保留U
def selection(self):'''greedy selection'''X = self.X.copy()f_X = self.x2y().copy()self.X = U = self.Uf_U = self.x2y()self.X = np.where((f_X < f_U).reshape(-1, 1), X, U)return self.X
迭代,继续变异,选择
def run(self, max_iter=None):self.max_iter = max_iter or self.max_iterfor i in range(self.max_iter):self.mutation()self.crossover()self.selection()# record the best onesgeneration_best_index = self.Y.argmin()self.generation_best_X.append(self.X[generation_best_index, :].copy())self.generation_best_Y.append(self.Y[generation_best_index])self.all_history_Y.append(self.Y)global_best_index = np.array(self.generation_best_Y).argmin()self.best_x = self.generation_best_X[global_best_index]self.best_y = self.func(np.array([self.best_x]))return self.best_x, self.best_y
这篇关于差分进化算法 (DE) Python实现及源码解读的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!