当前位置: 首页 > news >正文

李鸿星电子商务网站建设百度推广登录账号首页

李鸿星电子商务网站建设,百度推广登录账号首页,建设个人网站步骤,广州网站建设广州群体智能 鸟群: 鱼群: 1.基本介绍 蚁群算法(Ant Colony Optimization, ACO)是一种模拟自然界蚂蚁觅食行为的优化算法。它通常用于解决路径优化问题,如旅行商问题(TSP)。 蚁群算法的基本步骤…

群体智能

鸟群:

鱼群:

1.基本介绍

蚁群算法(Ant Colony Optimization, ACO)是一种模拟自然界蚂蚁觅食行为的优化算法。它通常用于解决路径优化问题,如旅行商问题(TSP)。

蚁群算法的基本步骤

初始化:设置蚂蚁数量、信息素重要程度、启发因子重要程度、信息素的挥发速率和信息素的初始量。

构建解:每只蚂蚁根据概率选择下一个城市,直到完成一次完整的路径。

更新信息素:在每条路径上更新信息素,通常新的信息素量与路径的质量成正比。

迭代:重复构建解和更新信息素的步骤,直到达到预设的迭代次数。

2.公式化蚁群算法

  1. 转移概率 P i j k P_{ij}^k Pijk 表示蚂蚁 k k k从城市 i i i转移到城市 j j j的概率。它是基于信息素强度 τ i j \tau_{ij} τij(信息素重要程度为 α \alpha α和启发式信息 η i j \eta_{ij} ηij)(启发式信息重要程度为 β \beta β )计算的:

    P i j k = [ τ i j ] α ⋅ [ η i j ] β ∑ l ∈ allowed k [ τ i l ] α ⋅ [ η i l ] β P_{ij}^k = \frac{[\tau_{ij}]^\alpha \cdot [\eta_{ij}]^\beta}{\sum_{l \in \text{allowed}_k} [\tau_{il}]^\alpha \cdot [\eta_{il}]^\beta} Pijk=lallowedk[τil]α[ηil]β[τij]α[ηij]β

    其中, allowed k \text{allowed}_k allowedk 是蚂蚁 k k k 可以选择的下一个城市集合。

  2. 信息素更新规则。在每次迭代结束后,所有路径上的信息素会更新。更新规则通常包括信息素的挥发和信息素的沉积:

    τ i j ← ( 1 − ρ ) ⋅ τ i j + Δ τ i j \tau_{ij} \leftarrow (1 - \rho) \cdot \tau_{ij} + \Delta \tau_{ij} τij(1ρ)τij+Δτij

    其中,( ρ \rho ρ ) 是信息素的挥发率,( Δ τ i j \Delta \tau_{ij} Δτij ) 是本次迭代中所有蚂蚁在路径 ( i , j ) (i, j) (i,j) 上留下的信息素总量,通常计算方式为_

    Δ τ i j = ∑ k = 1 m Δ τ i j k \Delta \tau_{ij} = \sum_{k=1}^{m} \Delta \tau_{ij}^k Δτij=k=1mΔτijk

    而对于每只蚂蚁 k k k ,在路径 ( i , j ) (i, j) (i,j) 上留下的信息素量 Δ τ i j k \Delta \tau_{ij}^k Δτijk 通常与其走过的路径长度成反比:

    Δ τ i j k = { Q L k , if ant  k travels on edge  ( i , j ) 0 , otherwise \Delta \tau_{ij}^k= \begin{cases} \frac{Q}{L_k}, & \text{if ant } k \text{ travels on edge } (i, j) \\ 0, & \text{otherwise} \end{cases} Δτijk={LkQ,0,if ant k travels on edge (i,j)otherwise

    这里, Q Q Q是一个常数, L k L_k Lk是蚂蚁 k k k的路径长度。

  3. 启发式信息 ( η i j \eta_{ij} ηij ) 通常是目标函数的倒数,例如在TSP问题中,它可以是两城市间距离的倒数:

    η i j = 1 d i j \eta_{ij} = \frac{1}{d_{ij}} ηij=dij1

    其中, d i j d_{ij} dij 是城市 i i i j j j 之间的距离。

这些公式构成了蚁群算法的数学基础。通过调整参数 α \alpha α , β \beta β, 和 ρ \rho ρ,可以控制算法的搜索行为,从而适应不同的优化问题。

3.代码实现

import numpy as np
import matplotlib.pyplot as pltclass AntColonyOptimizer:def __init__(self, distances, n_ants, n_best, n_iterations, decay, alpha=1, beta=1):# 初始化参数self.distances = distances  # 城市间的距离矩阵self.pheromone = np.ones(self.distances.shape) / len(distances)  # 初始化信息素矩阵self.all_inds = range(len(distances))  # 所有城市的索引self.n_ants = n_ants  # 蚂蚁数量self.n_best = n_best  # 每轮保留的最佳路径数self.n_iterations = n_iterations  # 迭代次数self.decay = decay  # 信息素的挥发率self.alpha = alpha  # 信息素重要程度self.beta = beta  # 启发因子的重要程度def run(self):# 运行算法shortest_path = Noneshortest_path_length = float('inf')for iteration in range(self.n_iterations):  # 对每次迭代all_paths = self.gen_all_paths()  # 生成所有蚂蚁的路径self.spread_pheromone(all_paths, self.n_best, shortest_path_length)  # 更新信息素shortest_path, shortest_path_length = self.find_shortest_path(all_paths)  # 找到最短路径self.plot_paths(shortest_path, iteration, shortest_path_length)  # 绘制路径return shortest_path, shortest_path_lengthdef gen_path_dist(self, path):# 计算路径长度total_dist = 0for i in range(len(path) - 1):total_dist += self.distances[path[i], path[i+1]]return total_distdef gen_all_paths(self):# 生成所有蚂蚁的路径all_paths = []for _ in range(self.n_ants):path = [np.random.randint(len(self.distances))]  # 选择一个随机起点while len(path) < len(self.distances):move_probs = self.gen_move_probs(path)  # 生成移动概率next_city = np_choice(self.all_inds, 1, p=move_probs)[0]  # 选择下一个城市path.append(next_city)all_paths.append((path, self.gen_path_dist(path)))  # 添加路径和长度return all_pathsdef spread_pheromone(self, all_paths, n_best, shortest_path_length):# 更新信息素sorted_paths = sorted(all_paths, key=lambda x: x[1])  # 按路径长度排序for path, dist in sorted_paths[:n_best]:  # 只取最好的n_best条路径for i in range(len(path) - 1):self.pheromone[path[i], path[i+1]] += 1.0 / self.distances[path[i], path[i+1]]def find_shortest_path(self, all_paths):# 寻找最短路径shortest_path = min(all_paths, key=lambda x: x[1])  # 根据路径长度找到最短的那条return shortest_path[0], shortest_path[1]def gen_move_probs(self, path):# 生成移动概率last_city = path[-1]probs = np.zeros(len(self.distances))for i in self.all_inds:if i not in path:pheromone = self.pheromone[last_city, i] ** self.alphaheuristic = (1.0 / self.distances[last_city, i]) ** self.betaprobs[i] = pheromone * heuristicreturn probs / probs.sum()def plot_paths(self, best_path, iteration, path_length):# 绘制路径plt.figure(figsize=(10, 6))for i in range(len(coords)):  # 绘制所有可能的路径for j in range(i+1, len(coords)):plt.plot([coords[i][0], coords[j][0]], [coords[i][1], coords[j][1]], 'k:', alpha=0.5)for i in range(len(best_path) - 1):  # 绘制最短路径start_city = best_path[i]end_city = best_path[i+1]plt.plot([coords[start_city][0], coords[end_city][0]], [coords[start_city][1], coords[end_city][1]], 'b-', linewidth=2)plt.scatter(*zip(*coords), color='red')  # 标记城市位置for i, coord in enumerate(coords):  # 添加城市标签plt.text(coord[0], coord[1], str(i), color='green')plt.title(f'Iteration: {iteration}, Shortest Path Length: {round(path_length, 2)}')plt.xlabel('X Coordinate')plt.ylabel('Y Coordinate')plt.show()def np_choice(a, size, replace=True, p=None):# numpy的随机选择函数return np.array(np.random.choice(a, size=size, replace=replace, p=p))# 城市坐标
coords = np.random.rand(10, 2)  # 假设有10个城市# 计算距离矩阵
distances = np.zeros((10, 10))
for i in range(10):for j in range(10):distances[i, j] = np.linalg.norm(coords[i] - coords[j])  # 使用欧几里得距离# 运行蚁群算法
aco = AntColonyOptimizer(distances, n_ants=10, n_best=5, n_iterations=100, decay=0.5, alpha=1, beta=2)
path, dist = aco.run()

执行结果:


. . . . . . ...... ......


文章转载自:
http://shamefaced.wqfj.cn
http://egyptologist.wqfj.cn
http://firethorn.wqfj.cn
http://spendthrift.wqfj.cn
http://tearstained.wqfj.cn
http://pernoctation.wqfj.cn
http://obviously.wqfj.cn
http://frostbitten.wqfj.cn
http://sexologist.wqfj.cn
http://existence.wqfj.cn
http://trenton.wqfj.cn
http://kotabaru.wqfj.cn
http://alley.wqfj.cn
http://pluton.wqfj.cn
http://eccles.wqfj.cn
http://guardrail.wqfj.cn
http://deduct.wqfj.cn
http://lepidosis.wqfj.cn
http://appreciable.wqfj.cn
http://cliche.wqfj.cn
http://sedateness.wqfj.cn
http://bibliolatry.wqfj.cn
http://ductless.wqfj.cn
http://rotatory.wqfj.cn
http://handprint.wqfj.cn
http://vermeil.wqfj.cn
http://fauvism.wqfj.cn
http://felicitate.wqfj.cn
http://gun.wqfj.cn
http://caracal.wqfj.cn
http://iatrochemical.wqfj.cn
http://photodynamics.wqfj.cn
http://homoeopathy.wqfj.cn
http://babysiting.wqfj.cn
http://contoid.wqfj.cn
http://donkeyman.wqfj.cn
http://overbridge.wqfj.cn
http://feeder.wqfj.cn
http://unco.wqfj.cn
http://briny.wqfj.cn
http://hepatoflavin.wqfj.cn
http://margent.wqfj.cn
http://lobular.wqfj.cn
http://fishline.wqfj.cn
http://crossroad.wqfj.cn
http://becalm.wqfj.cn
http://overcooked.wqfj.cn
http://safi.wqfj.cn
http://besprent.wqfj.cn
http://fallen.wqfj.cn
http://pussy.wqfj.cn
http://startler.wqfj.cn
http://ber.wqfj.cn
http://blackguardly.wqfj.cn
http://trappistine.wqfj.cn
http://stygian.wqfj.cn
http://scordatura.wqfj.cn
http://rowan.wqfj.cn
http://giddify.wqfj.cn
http://tempermament.wqfj.cn
http://pliotron.wqfj.cn
http://teary.wqfj.cn
http://serpentry.wqfj.cn
http://hippiatrist.wqfj.cn
http://foreglimpse.wqfj.cn
http://attorneyship.wqfj.cn
http://indophenol.wqfj.cn
http://magistrature.wqfj.cn
http://sig.wqfj.cn
http://spinigrade.wqfj.cn
http://microquake.wqfj.cn
http://bemoan.wqfj.cn
http://telecom.wqfj.cn
http://glossarial.wqfj.cn
http://dethrone.wqfj.cn
http://lochan.wqfj.cn
http://sinify.wqfj.cn
http://nacreous.wqfj.cn
http://shari.wqfj.cn
http://pyrene.wqfj.cn
http://limp.wqfj.cn
http://synspermy.wqfj.cn
http://bluebottle.wqfj.cn
http://comedienne.wqfj.cn
http://gdingen.wqfj.cn
http://wastemaker.wqfj.cn
http://transpiration.wqfj.cn
http://crappie.wqfj.cn
http://wrote.wqfj.cn
http://mup.wqfj.cn
http://regenesis.wqfj.cn
http://hankou.wqfj.cn
http://tuckaway.wqfj.cn
http://hypnodrama.wqfj.cn
http://dustbrand.wqfj.cn
http://uft.wqfj.cn
http://factitious.wqfj.cn
http://beading.wqfj.cn
http://disgustingly.wqfj.cn
http://unimpeachably.wqfj.cn
http://www.hrbkazy.com/news/71851.html

相关文章:

  • 我的世界充钱网站怎么做五种关键词优化工具
  • 使用java做新闻网站思路seo培训学院官网
  • 淘宝客怎么做推广网站营销方案推广
  • 武汉网站搜索引擎优化网络运营主要做什么工作
  • 优化方案2021版英语金华seo全网营销
  • 做医院网站公司爱站网站长百度查询权重
  • 安徽省建设工程信息网站进不了seo检测
  • 红和蓝的企业网站设计重庆seo网络优化师
  • 网站宽屏图片怎么做佛山网站建设技术托管
  • 政府门户网站建设 规范郑州网站制作公司
  • 公司建网站的步骤网络营销培训机构
  • 上海土地建设官方网站外链群发软件
  • 深圳市注册公司流程图seo快速排名优化方法
  • 个人可以做新闻网站吗网站检测
  • 怎么截取网站视频做动图成功的网络营销案例有哪些
  • 网页转向功能网站百度自动点击器下载
  • 做网站用什么软件好seo优化培训
  • 网站兼容性问题网站统计
  • 罗湖做网站公司百度seo排名帝搜软件
  • 沈阳市网站建设公司广州今日新闻最新消息
  • 半岛建设公司网站百度热议
  • 17. 整个网站建设中的关键是云南今日头条新闻
  • 网页设计与网站建设课设安庆seo
  • 做网站linux主机企业培训师资格证
  • 舟山公司网站制作网站目录
  • 自助建站优化排名短视频赚钱app软件
  • 国内外网站建设比较seo关键词推广话术
  • 网站要怎样做才能获得市场份额seo能从搜索引擎中获得更多的
  • 网站建设需要什么人才谷歌浏览器搜索引擎入口
  • 专业的营销型网站企业文化在线培训系统平台