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. Qt532_WebKit_SSL问题

    1.打开网页"http://www.baidu.com",它会跳转至"https://www.baidu.com/",使用 SSL了,于是 WebView 出现 ...

  2. 【Jmeter】配置不同业务请求比例,应对综合场景压测

    背景 在进行综合场景压测时,遇到了如何实现不同的请求所占比例不同的问题. 有人说将这些请求分别放到单独的线程组下,然后将线程组的线程数按照比例进行配置. 这种方法不是很好,因为服务器对不同的请求处理能 ...

  3. Tensorflow一些常用基本概念与函数(二)

    1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf. ...

  4. 雷林鹏分享:Ruby 数组(Array)

    Ruby 数组(Array) Ruby 数组是任何对象的有序的.整数索引的集合.数组中的每个元素都与一个索引相关,并可通过索引进行获取. 数组的索引从 0 开始,这与 C 或 Java 中一样.一个负 ...

  5. selenium 3.6.0 geckodriver的一次坑

    Traceback (most recent call last):  File "./se3.py", line 16, in <module>    dr=webd ...

  6. [转] oracle 监听

    oracle 监听 启动监听:lsnrctl start 查看监听:lsnrctl status 停止监听:lsnrctl stop 1.oracle 数据服务器包括:实例进程和数据库: 实例进程包括 ...

  7. mongodb 之linux下安装、启动、停止、连接

    今天在linux上面安装了mongodb 1.下载linux的mongodb 2.在目录usr/local下创建文件夹mongodb,把安装包解压到该文件夹中 # mkdir mongodb # ta ...

  8. hdu 1385 floyd字典序

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  9. 个人知识管理系统Version1.0开发记录(05)

    demo controller我们从前面的实现过程可以得出三种普遍使用的信息处理方式:1.操作数据库,对数据进行增删改查,比如运用sqldevloper查看数据信息.2.运用计算机程序语言,对数据进行 ...

  10. 从数组里随机获取N项

    基础知识: 复制数组: (1)循环遍历复制(不推荐) var arry = [1,5,9,7], new_arry = [], n = 0, len = arry.length; for(;n< ...