资源简介
使用python 实现使用粒子群算法解决TSP问题,与matlab相比,更加的直观
代码片段和文件信息
# encoding:utf-8
‘‘‘
Solution for Travelling Salesman Problem using PSO (Particle Swarm Optimization)
Discrete PSO for TSP
References:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.258.7026&rep=rep1&type=pdf
http://www.cs.mun.ca/~tinayu/Teaching_files/cs4752/Lecture19_new.pdf
http://www.swarmintelligence.org/tutorials.php
References are in the folder “references“ of the repository.
‘‘‘
from operator import attrgetter
import random sys time copy
# class that represents a graph
class Graph:
def __init__(self amount_vertices):
self.edges = {} # dictionary of edges
self.vertices = set() # set of vertices
self.amount_vertices = amount_vertices # amount of vertices
# adds a edge linking “src“ in “dest“ with a “cost“
def addEdge(self src dest cost = 0):
# checks if the edge already exists
if not self.existsEdge(src dest):
self.edges[(src dest)] = cost
self.vertices.add(src)
self.vertices.add(dest)
# checks if exists a edge linking “src“ in “dest“
def existsEdge(self src dest):
return (True if (src dest) in self.edges else False)
# shows all the links of the graph
def showGraph(self):
print(‘Showing the graph:\n‘)
for edge in self.edges:
print(‘%d linked in %d with cost %d‘ % (edge[0] edge[1] self.edges[edge]))
# returns total cost of the path
def getCostPath(self path):
total_cost = 0
for i in range(self.amount_vertices - 1):
total_cost += self.edges[(path[i] path[i+1])]
# add cost of the last edge
total_cost += self.edges[(path[self.amount_vertices - 1] path[0])]
return total_cost
# gets random unique paths - returns a list of lists of paths
def getRandomPaths(self max_size):
random_paths list_vertices = [] list(self.vertices)
initial_vertice = random.choice(list_vertices)
if initial_vertice not in list_vertices:
print(‘Error: initial vertice %d not exists!‘ % initial_vertice)
sys.exit(1)
list_vertices.remove(initial_vertice)
list_vertices.insert(0 initial_vertice)
for i in range(max_size):
list_temp = list_vertices[1:]
random.shuffle(list_temp)
list_temp.insert(0 initial_vertice)
if list_temp not in random_paths:
random_paths.append(list_temp)
return random_paths
# class that represents a complete graph
class CompleteGraph(Graph):
# generates a complete graph
def generates(self):
for i in range(self.amount_vertices):
for j in range(self.amount_vertices):
if i != j:
weight = random.randint(1 10)
self.addEdge(i j weight)
# class that represents a particle
class Particle:
def __init__(self solution cost):
# current solution
self.solution = solution
# best solution (fitness) it has achieved so far
self.pbest = solution
# set costs
self.cost_current_solution = cost
self.cost_pbest_solution = cost
# velocity of a particle is a sequence of 4-tuple
# (1 2 1 ‘beta‘) means SO(12) prabability 1 and compares with “beta“
self.velocity = []
# set
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-04-30 20:49 tsp_pso-master\
文件 11 2015-04-30 20:49 tsp_pso-master\.gitignore
文件 486 2015-04-30 20:49 tsp_pso-master\README.md
目录 0 2015-04-30 20:49 tsp_pso-master\images\
文件 8358 2015-04-30 20:49 tsp_pso-master\images\graph.png
文件 18998 2015-04-30 20:49 tsp_pso-master\images\output_graph.png
目录 0 2015-04-30 20:49 tsp_pso-master\references\
文件 521034 2015-04-30 20:49 tsp_pso-master\references\paper.pdf
文件 88917 2015-04-30 20:49 tsp_pso-master\references\slides.pdf
文件 9647 2015-04-30 20:49 tsp_pso-master\tsp_pso.py
- 上一篇:强化学习控制gym下的倒立摆
- 下一篇:python首次连接STK
相关资源
- python首次连接STK
- PythonOCC的安装
-
pyQt5_wavepla
yer python计算声音分贝 语 - The Python Language Reference Manual 无水印
- iris.csv数据集和python代码
- Python爬虫爬取校内论坛标题,并将关
- crowd counting test single image demo
- kNN(python实现)
- ds18x20_onewire.rar
- python生成扭曲带干扰验证码
- 基于OpenCV 3 LBPH 人脸识别 Python代码
- Python标准库源代码.zip
- 信息隐藏——Python语言幻方置乱实现
- train_loss_acc.py
- 可视化函数绘图计算器
- Bayesian Network贝叶斯网络 Python Program
- Python WSQ行情订阅演示案例.rar
- python网络爬虫爬取Boss直聘代码
- generate_train_val_test_txt.py
- 在python环境下成功实现视频分帧,并
- 传染病SEIR传播动力模型python代码
- 船舶AIS数据轨迹可视化python代码.py
- python背单词小程序
- 深度信念网络分类算法python程序.doc
- Python爬取论文标题、作者、摘要等信
- python爬虫的随机请求头+随机代理
- python实现种子填充算法.zip
- python实现有序边表算法.zip
- 纯python实现mnist手写体识别.zip
- Python爬取豆瓣图书信息并保存到本地
评论
共有 条评论