资源简介
万有引力算法
引力搜索算法在2009年被首次提出,是一种基于万有引力定律和牛顿第二定律的种群优化算法。该算法通过种群的粒子位置移动来寻找最优解,即随着算法的循环,粒子靠它们之间的万有引力在搜索空间内不断运动,当粒子移动到最优位置时,最优解便找到了。
GSA即引力搜索算法,是一种优化算法的基础上的重力和质量相互作用的算法。GSA的机制是基于宇宙万有引力定律中两个质量的相互作用。
代码片段和文件信息
from pprint import pprint
from matplotlib import pyplot as plt
import numpy as np
class GSA:
def __init__(self pop_size=1 dimension=1 max_iter=100):
self.cost_func = None
self.alpha = 0.1
self.G = 0.9
self.max_iter = max_iter
self.pop_size = pop_size
self.dimension = dimension
self.best_so_far = None
self.X = np.random.rand(pop_size dimension)
self.V = np.random.rand(pop_size dimension)
self.f = np.full((pop_size dimension) None) # None # Will become a list inside cal_f
self.a = np.full((pop_size dimension) None)
self.q = np.full((pop_size 1) None)
self.M = np.full((pop_size 1) None)
self.cost_matrix = np.full((pop_size 1) None)
# Evaluate a single x (x_i)
def evaluate(self args):
return self.cost_func(args)
# Generate the cost of all particles
def gen_cost_matrix(self):
for i x in enumerate(self.X):
self.cost_matrix[i] = self.evaluate(x)
def cal_q(self):
best = np.min(self.cost_matrix)
worst = np.max(self.cost_matrix)
self.q = (self.cost_matrix - worst) / best - worst
def cal_m(self):
self.M = self.q / np.sum(self.q)
def cal_f(self):
costs = self.cost_matrix.copy()
costs.sort(axis=0)
costs = costs
for i in range(self.pop_size):
f = None
for cost in costs:
j = int(np.where(self.cost_matrix == cost)[0])
dividend = float(self.M[i] * self.M[j])
divisor = np.sqrt(np.sum((self.X[i] - self.X[j]) ** 2)) + np.finfo(‘float‘).eps
if f is None:
f = self.G * (dividend / divisor) * (self.X[j] - self.X[i])
else:
f = f + self.G * (dividend / divisor) * (self.X[j] - self.X[i])
self.f[i] = np.random.uniform(0 1) * f
def cal_a(self):
for i in range(self.pop_size):
self.a[i] = self.f[i] / self.M[i]
def cal_v(self):
self.V = (np.random.uniform(0 1) * self.V) + self.a
def move(self):
self.X = self.X + self.V
def update_g(self iteration):
self.G = self.G * np.e ** (- self.alpha * (iteration / self.max_iter))
def show_results(self):
print(‘Best seen so far is located at:‘ self.best_so_far ‘Cost:‘ self.evaluate(self.best_so_far))
def update_best_so_far(self):
best = np.min(self.cost_matrix)
index = int(np.where(self.cost_matrix == best)[0])
if self.best_so_far is None or self.evaluate(self.best_so_far) > self.evaluate(self.X[index]):
self.best_so_far = self.X[index]
def start(self):
_iter = 0
avg = []
bsf = []
while _iter < self.max_iter:
self.gen_cost_matrix()
self.cal_q()
self.cal_m()
self.update_g(_iter)
self.cal_f()
sel
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-01-23 11:53 GSA-master\
文件 3743 2019-01-23 11:53 GSA-master\GSA.py
文件 35149 2019-01-23 11:53 GSA-master\LICENSE
文件 52 2019-01-23 11:53 GSA-master\README.md
相关资源
- 点云数据ply格式
- PyQt5快速开发与实践
- Arcgis批量裁剪
- Regression回归算法代码
- PyQt5系列教程(二)利用QtDesigner设计
- 简单贝叶斯实现垃圾邮件分类
- Opencv+Tensorflow入门人工智能处理无密完
- 运用ID3算法训练决策树
- BPNet多层网络学习算法可以实现有效的
- 文书网最新版getKey.js(获取vl5x值)
- djangol实现学生管理网站
- 基于西瓜书的聚类代码和介绍
- Sublime3护眼主题
- tensorflow-2.1.0-cp37-cp37m-win_amd64.whl
- 12306爬虫源码
-
failed to execute sc
ript 解决方案+命令行 - 一次性口令身份认证
- XGboost训练印第安人糖尿病数据
- 查找两幅图中不同
- PyQt5:简单视频播放器
- Machine learning DecisionTree
- Face-tracking.zip
- [MIT][计算机科学及编程导论][6.00][课程
- 花卉识别程序+数据集
- SciHub神器,基于爬虫原理
- 机器学习-泰坦尼克号船员获救
- 基于Flask的留言板Demo
- tensorflow-2.3.0-cp37-cp37m-win_amd64.whl
- tensorflow麻将智能出牌源码
- 引力搜索算法
评论
共有 条评论