克隆选择算法-python实现
CSAIndividual.py
import numpy as np
import ObjFunction class CSAIndividual: '''
individual of clone selection algorithm
''' def __init__(self, vardim, bound):
'''
vardim: dimension of variables
bound: boundaries of variables
'''
self.vardim = vardim
self.bound = bound
self.fitness = 0.
self.trials = 0 def generate(self):
'''
generate a random chromsome for clone selection 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)
CSA.py
import numpy as np
from CSAIndividual import CSAIndividual
import random
import copy
import matplotlib.pyplot as plt class CloneSelectionAlgorithm: '''
the class for clone selection algorithm
''' def __init__(self, sizepop, vardim, bound, MAXGEN, params):
'''
sizepop: population sizepop
vardim: dimension of variables
bound: boundaries of variables
MAXGEN: termination condition
params: algorithm required parameters, it is a list which is consisting of[beta, pm, alpha_max, alpha_min]
'''
self.sizepop = sizepop
self.vardim = vardim
self.bound = bound
self.MAXGEN = MAXGEN
self.params = params
self.population = []
self.fitness = np.zeros(self.sizepop)
self.trace = np.zeros((self.MAXGEN, 2)) def initialize(self):
'''
initialize the population of ba
'''
for i in xrange(0, self.sizepop):
ind = CSAIndividual(self.vardim, self.bound)
ind.generate()
self.population.append(ind) def evaluation(self):
'''
evaluation the fitness of the population
'''
for i in xrange(0, self.sizepop):
self.population[i].calculateFitness()
self.fitness[i] = self.population[i].fitness def solve(self):
'''
the evolution process of the clone selection algorithm
'''
self.t = 0
self.initialize()
self.evaluation()
bestIndex = np.argmax(self.fitness)
self.best = copy.deepcopy(self.population[bestIndex])
while self.t < self.MAXGEN:
self.t += 1
tmpPop = self.reproduction()
tmpPop = self.mutation(tmpPop)
self.selection(tmpPop)
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 - 1, 0] = \
(1 - self.best.fitness) / self.best.fitness
self.trace[self.t - 1, 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 - 1, 0], self.trace[self.t - 1, 1]))
print("Optimal function value is: %f; " % self.trace[self.t - 1, 0])
print "Optimal solution is:"
print self.best.chrom
self.printResult() def reproduction(self):
'''
reproduction
'''
tmpPop = []
for i in xrange(0, self.sizepop):
nc = int(self.params[1] * self.sizepop)
for j in xrange(0, nc):
ind = copy.deepcopy(self.population[i])
tmpPop.append(ind)
return tmpPop def mutation(self, tmpPop):
'''
hypermutation
'''
for i in xrange(0, self.sizepop):
nc = int(self.params[1] * self.sizepop)
for j in xrange(1, nc):
rnd = np.random.random(1)
if rnd < self.params[0]:
# alpha = self.params[
# 2] + self.t * (self.params[3] - self.params[2]) / self.MAXGEN
delta = self.params[2] + self.t * \
(self.params[3] - self.params[3]) / self.MAXGEN
tmpPop[i * nc + j].chrom += np.random.normal(0.0, delta, self.vardim)
# tmpPop[i * nc + j].chrom += alpha * np.random.random(
# self.vardim) * (self.best.chrom - tmpPop[i * nc +
# j].chrom)
for k in xrange(0, self.vardim):
if tmpPop[i * nc + j].chrom[k] < self.bound[0, k]:
tmpPop[i * nc + j].chrom[k] = self.bound[0, k]
if tmpPop[i * nc + j].chrom[k] > self.bound[1, k]:
tmpPop[i * nc + j].chrom[k] = self.bound[1, k]
tmpPop[i * nc + j].calculateFitness()
return tmpPop def selection(self, tmpPop):
'''
re-selection
'''
for i in xrange(0, self.sizepop):
nc = int(self.params[1] * self.sizepop)
best = 0.0
bestIndex = -1
for j in xrange(0, nc):
if tmpPop[i * nc + j].fitness > best:
best = tmpPop[i * nc + j].fitness
bestIndex = i * nc + j
if self.fitness[i] < best:
self.population[i] = copy.deepcopy(tmpPop[bestIndex])
self.fitness[i] = best def printResult(self):
'''
plot the result of clone selection 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("Clone selection algorithm for function optimization")
plt.legend()
plt.show()
运行程序:
if __name__ == "__main__": bound = np.tile([[-600], [600]], 25)
csa = CSA(50, 25, bound, 500, [0.3, 0.4, 5, 0.1])
csa.solve()
ObjFunction见简单遗传算法-python实现。
克隆选择算法-python实现的更多相关文章
- pageRank算法 python实现
一.什么是pagerank PageRank的Page可是认为是网页,表示网页排名,也可以认为是Larry Page(google 产品经理),因为他是这个算法的发明者之一,还是google CEO( ...
- 常见排序算法-Python实现
常见排序算法-Python实现 python 排序 算法 1.二分法 python 32行 right = length- : ] ): test_list = [,,,,,, ...
- kmp算法python实现
kmp算法python实现 kmp算法 kmp算法用于字符串的模式匹配,也就是找到模式字符串在目标字符串的第一次出现的位置比如abababc那么bab在其位置1处,bc在其位置5处我们首先想到的最简单 ...
- KMP算法-Python版
KMP算法-Python版 传统法: 从左到右一个个匹配,如果这个过程中有某个字符不匹配,就跳回去,将模式串向右移动一位.这有什么难的? 我们可以 ...
- 压缩感知重构算法之IRLS算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之OLS算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之CoSaMP算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之IHT算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之SP算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
随机推荐
- 在WPF控件上添加Windows窗口式调整大小行为
起因 项目上需要对Canvas中的控件添加调整大小功能,即能在控件的四个角和四条边上可进行相应的拖动,类似Windows窗口那种.于是在参考以前同事写的代码基础上,完成了该功能. 代码实现 Adorn ...
- C# Reflection Type/MethodInfo
C#反射 在C#的反射中,可以通过Type来执行类中的某个方法,也可以通过MethodInfo来执行方法 三种调用方法 下面的示例中使用了三种方法来执行方法 两个类:Class1和Demo1,通过反射 ...
- Linux压力测试工具Tsung安装、使用和图形报表生成
简介 Tsung 是一个压力测试工具,可以测试包括HTTP, WebDAV, PostgreSQL, MySQL, LDAP, and XMPP/Jabber等服务器.针对 HTTP 测试,Tsung ...
- WindowXP与WIN7环境安装、破解、配置AppScan8.0
---------------------------------------------------------------------------------------------------- ...
- 让input框只能输入数字
var oInput = document.querySelector("input");oInput.onkeyup = function () { var value = th ...
- 07Spring_bean属性的依赖注入-重点@Autowriter
在spring2.5 版本,没有提供基本类型属性注入 ,但是spring3.0引入注解@Value 所以在Spring3.0中属性的注入只可以这么写.
- js 中常用的方法
1..call() 将.call()点之前的属性或方法,继承给括号中的对象. 2.(function(){xxx})() 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数, ...
- Angular权威指南学习笔记
第一章. 初识Angular--Angular是MVW的Js框架. 第二章. 数据绑定--ViewModel中不仅可以含有变量,还可以还有事件.可以通过事件来控制变量的值改 ...
- matlab取消和添加注释以及一些快捷键
1 matlab中关于注释: 多行注释: 选中要注释的若干语句,工具栏菜单Text->Comment,或者鼠标右击选"Comment",或者快捷键Ctrl+R 取消注释: 选 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...