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实现 压缩感知重构 ...
随机推荐
-
对接第三方支付接口-获取http中的返回参数
这几天对接第三方支付接口,在回调通知里获取返回参数,有一家返回的json格式,请求参数可以从标准输入流中获取. //1.解析参数 , 读取请求内容 BufferedReader br; String ...
-
osg 笔记一 (转)
场景图形采用一种自顶向下的,分层的树状数据结构来组织空间数据集,以提高渲染的效率 场景图形树结构的顶部是一个根节点,从根节点向下延伸,各个组节点中均包含了几何信息和用于控制其外观的渲染状态信息.根节点 ...
-
谁才是最快的消息队列:ActiveMQ, RabbitMQ[转]
Lately I performed a message queue benchmark, comparing several queuing frameworks (RabbitMQ, Active ...
-
engine中调整Element的上下显示顺序(遮盖)
pGraphicsContainer.AddElement(pElement, 0); Engine中IGraphicsContainer类似于栈,加Element时,默认加到第一个,所以会将之前加的 ...
-
UVA-10037 Bridge---过河问题进阶版(贪心)
题目链接: https://vjudge.net/problem/UVA-10037 题目大意: N个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 才能继续过人,每个人过桥都需要 ...
-
WebBrowser Cookie
WebBrowser的Cookie操作 .在WebBrowser中获取Cookie CookieContainer myCookieContainer = new CookieContainer(); ...
-
scipy优化器optimizer
#optimazer优化器 from scipy.optimize import minimize def rosem(x): return sum(100.0*(x[1:]-x[:-1])**2.0 ...
-
Analytics.js简介
analytics.js JavaScript代码段是一种可用于衡量用户与您网站的互动情况的全新方式.它与之前的跟踪代码ga.js类似,但为开发者自定义实现方案提供了更大的灵活性. analytics ...
-
UML(一)下载与安装
三步 Step1 下载绿色版: http://www.pc6.com/softview/SoftView_64080.html Step2 下载汉化包,(提供一个地址): http://downloa ...
-
chrony 时间同步
RHEL7.4 192.168.100.1 作为时间服务器,其它主机到这台来同步时间. 时间服务器安装及配置:#yum install chrony --RHEL7默认已安装chrony,而没有安装n ...