资源简介
万有引力算法
引力搜索算法在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
相关资源
- Pythonamp;课堂amp;笔记(高淇amp;400;集第
- Python中Numpy库最新教程
- 用python编写的移动彩信的发送程序
- Python全栈学习笔记面向对象大作业:
- python实现的ftp自动上传、下载脚本
- Python版的A*寻路算法
- IronPython IDE
- pip-10.0.1.tar.gz
- Data Science from Scratch 2nd Edition
- shape_predictor_68_face_landmarks.dat.bz2 68个标
- 爬取豆瓣电影TOP250程序,包含非常详
- 中文维基百科语料库百度网盘网址.
- MSCNN_dehaze.rar
- 爬取豆瓣排行榜电影数据(含GUI界面
- 字典文本资源
- Brainfuck / OoK 解码脚本
- 万有引力算法
- 案例实战信用卡欺诈检测数据集
- 招商策略_抱团启示录那些年我们一起
- sip-4.19.zip
- 树莓派3b+学习使用教程
- numpy 中文学习手册
- pytorch-1.4.0-py3.7_cpu_0.tar.bz2
- 机器学习实战 高清完整版PDF
- 泰坦尼克号0.81准确率实验报告.docx
-
abaqus sc
ripting reference manual.pdf - 网页版聊天程序--网络程序设计课程大
- Give Me Some Credit
-
ba
semap安装出错时,正确得pyproj文件 - 微信头像拼接工具
评论
共有 条评论