DEIndividual.py

 import numpy as np
import ObjFunction class DEIndividual: '''
individual of differential evolution algorithm
''' def __init__(self, vardim, bound):
'''
vardim: dimension of variables
bound: boundaries of variables
'''
self.vardim = vardim
self.bound = bound
self.fitness = 0. def generate(self):
'''
generate a random chromsome for differential evolution algorithm
'''
len = self.vardim
rnd = np.random.random(size=len)
self.chrom = np.zeros(len)
for i in xrange(0, len):
self.chrom[i] = self.bound[0, i] + \
(self.bound[1, i] - self.bound[0, i]) * rnd[i] def calculateFitness(self):
'''
calculate the fitness of the chromsome
'''
self.fitness = ObjFunction.GrieFunc(
self.vardim, self.chrom, self.bound)

DE.py

 import numpy as np
from DEIndividual import DEIndividual
import random
import copy
import matplotlib.pyplot as plt class DifferentialEvolutionAlgorithm: '''
The class for differential evolution algorithm
''' def __init__(self, sizepop, vardim, bound, MAXGEN, params):
'''
sizepop: population sizepop
vardim: dimension of variables
bound: boundaries of variables
MAXGEN: termination condition
param: algorithm required parameters, it is a list which is consisting of [crossover rate CR, scaling factor F]
'''
self.sizepop = sizepop
self.MAXGEN = MAXGEN
self.vardim = vardim
self.bound = bound
self.population = []
self.fitness = np.zeros((self.sizepop, 1))
self.trace = np.zeros((self.MAXGEN, 2))
self.params = params def initialize(self):
'''
initialize the population
'''
for i in xrange(0, self.sizepop):
ind = DEIndividual(self.vardim, self.bound)
ind.generate()
self.population.append(ind) def evaluate(self, x):
'''
evaluation of the population fitnesses
'''
x.calculateFitness() def solve(self):
'''
evolution process of differential evolution algorithm
'''
self.t = 0
self.initialize()
for i in xrange(0, self.sizepop):
self.evaluate(self.population[i])
self.fitness[i] = self.population[i].fitness
best = np.max(self.fitness)
bestIndex = np.argmax(self.fitness)
self.best = copy.deepcopy(self.population[bestIndex])
self.avefitness = np.mean(self.fitness)
self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness
self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness
print("Generation %d: optimal function value is: %f; average function value is %f" % (
self.t, self.trace[self.t, 0], self.trace[self.t, 1]))
while (self.t < self.MAXGEN - 1):
self.t += 1
for i in xrange(0, self.sizepop):
vi = self.mutationOperation(i)
ui = self.crossoverOperation(i, vi)
xi_next = self.selectionOperation(i, ui)
self.population[i] = xi_next
for i in xrange(0, self.sizepop):
self.evaluate(self.population[i])
self.fitness[i] = self.population[i].fitness
best = np.max(self.fitness)
bestIndex = np.argmax(self.fitness)
if best > self.best.fitness:
self.best = copy.deepcopy(self.population[bestIndex])
self.avefitness = np.mean(self.fitness)
self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness
self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness
print("Generation %d: optimal function value is: %f; average function value is %f" % (
self.t, self.trace[self.t, 0], self.trace[self.t, 1])) print("Optimal function value is: %f; " %
self.trace[self.t, 0])
print "Optimal solution is:"
print self.best.chrom
self.printResult() def selectionOperation(self, i, ui):
'''
selection operation for differential evolution algorithm
'''
xi_next = copy.deepcopy(self.population[i])
xi_next.chrom = ui
self.evaluate(xi_next)
if xi_next.fitness > self.population[i].fitness:
return xi_next
else:
return self.population[i] def crossoverOperation(self, i, vi):
'''
crossover operation for differential evolution algorithm
'''
k = np.random.random_integers(0, self.vardim - 1)
ui = np.zeros(self.vardim)
for j in xrange(0, self.vardim):
pick = random.random()
if pick < self.params[0] or j == k:
ui[j] = vi[j]
else:
ui[j] = self.population[i].chrom[j]
return ui def mutationOperation(self, i):
'''
mutation operation for differential evolution algorithm
'''
a = np.random.random_integers(0, self.sizepop - 1)
while a == i:
a = np.random.random_integers(0, self.sizepop - 1)
b = np.random.random_integers(0, self.sizepop - 1)
while b == i or b == a:
b = np.random.random_integers(0, self.sizepop - 1)
c = np.random.random_integers(0, self.sizepop - 1)
while c == i or c == b or c == a:
c = np.random.random_integers(0, self.sizepop - 1)
vi = self.population[c].chrom + self.params[1] * \
(self.population[a].chrom - self.population[b].chrom)
for j in xrange(0, self.vardim):
if vi[j] < self.bound[0, j]:
vi[j] = self.bound[0, j]
if vi[j] > self.bound[1, j]:
vi[j] = self.bound[1, j]
return vi def printResult(self):
'''
plot the result of the differential evolution algorithm
'''
x = np.arange(0, self.MAXGEN)
y1 = self.trace[:, 0]
y2 = self.trace[:, 1]
plt.plot(x, y1, 'r', label='optimal value')
plt.plot(x, y2, 'g', label='average value')
plt.xlabel("Iteration")
plt.ylabel("function value")
plt.title("Differential Evolution Algorithm for function optimization")
plt.legend()
plt.show()

运行程序:

 if __name__ == "__main__":

     bound = np.tile([[-600], [600]], 25)
dea = DEA(60, 25, bound, 1000, [0.8, 0.6])
dea.solve()

ObjFunction见简单遗传算法-python实现

差分进化算法-python实现的更多相关文章

  1. 差分进化算法 DE-Differential Evolution

    差分进化算法 (Differential Evolution)   Differential Evolution(DE)是由Storn等人于1995年提出的,和其它演化算法一样,DE是一种模拟生物进化 ...

  2. 标准差分进化算法matlab程序实现(转载)

    标准差分进化算法matlab程序实现 自适应差分演化算法方面的Matlab和C++代码及论文 差分进化算法 DE-Differential Evolution matlab练习程序(差异演化DE) [ ...

  3. 差分进化算法介绍及matlab实现

    引言 差分进化算法是基于群体智能理论的优化算法,是通过群体内个体间的合作与竞争而产生的智能优化搜索算法,它保留了基于种群的全局搜索策略,采用实数编码.基于差分的简单变异操作和"一对一&quo ...

  4. 差分进化算法(DE)的C++面向对象方法实现

    代码来源于网络,写得非常棒 /*DE_test *对相应的Matlab程序进行测试 */ #include <iostream> #include <cmath> #inclu ...

  5. Python遗传和进化算法框架(一)Geatpy快速入门

    https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...

  6. geatpy - 遗传和进化算法相关算子的库函数(python)

    Geatpy The Genetic and Evolutionary Algorithm Toolbox for Python Introduction Website (including doc ...

  7. 【Python Deap库】遗传算法/遗传编程 进化算法基于python DEAP库深度解析讲解

    目录 前言 概述 启发式的理解(重点) 优化问题的定义 个体编码 初始族群的创建 评价 配种选择 锦标赛 轮盘赌选择 随机普遍抽样选择 变异 单点交叉 两点交叉 均匀交叉 部分匹配交叉 突变 高斯突变 ...

  8. 离散的差分进化Discrete DE

    一般的差分算法的变异规则:Xmutation=Xr1+F(Xr2-Xr3),F为缩放因子, 离散差分进化DDE的变异规则:设每个解为K个元素的集合,则Xr2-Xr3:求出Xr2与Xr3有m个共同元素, ...

  9. [Evolutionary Algorithm] 进化算法简介

    进化算法,也被成为是演化算法(evolutionary algorithms,简称EAs),它不是一个具体的算法,而是一个“算法簇”.进化算法的产生的灵感借鉴了大自然中生物的进化操作,它一般包括基因编 ...

随机推荐

  1. Python学习札记(三十九) 面向对象编程 Object Oriented Program 10

    参考:使用枚举类 NOTE #!/usr/bin/env python3 from enum import Enum def main(): Mouth = Enum('Mouth', ('Jan', ...

  2. UVa 11538 象棋中的皇后

    https://vjudge.net/problem/UVA-11538 题意: n×m的棋盘,有多少种方法放置两个相互攻击的皇后? 思路: 这两个皇后互相攻击的方式只有3种,在同一行,在同一列,或在 ...

  3. BZOJ 1875 【SDOI2009】 HH去散步

    题目链接:HH去散步 如果不考虑不能走上一次走的边的话,这道题就是一个矩乘的裸题. 现在有了这个条件其实也很好做.我们平常的矩阵都是按点建的,\(A_{i,j}\)表示从第\(i\)个点走到第\(j\ ...

  4. Android反射机制:手把手教你实现反射

    什么是反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称 ...

  5. android 官方demo地址

    android官方demo地址都放在了github上:https://github.com/googlesamples

  6. thinkphp3.2笔记(5)创建项目 创建模型 实例化

    一 创建项目 1 拷贝框架 目录   public   thinkphp  .htaccess  index.php    [application不用拷贝,会自动生成] 2   public 下面创 ...

  7. STL空间配置器那点事

    STL简介 STL(Standard Template Library,标准模板库),从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其 ...

  8. windows使用git记录

    1.免密码clone远程服务器代码开启ssh 生成私钥公钥 命令:查看自己配置的邮箱 git config user.name git config user.email 生成密钥:邮箱填写上面查看出 ...

  9. 【css】 文本超出2行显示省略号

    首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用省略号显示 white-space:nowrap; // ...

  10. 049——VUE中使用animation与transform实现vue的动画效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...