内容简介:Have you ever heard of the Travelling Salesman Problem? I'm pretty sure you do, but let's refresh our mind looking at its formulation: "Given a list of points and the distances between each pair of points, what is the shortest possible path that visits eac
Have you ever heard of the Travelling Salesman Problem? I'm pretty sure you do, but let's refresh our mind looking at its formulation: "Given a list of points and the distances between each pair of points, what is the shortest possible path that visits each point and returns to the starting point?".
What makes this problem so famous and so studied is the fact that it has no "quick" solution as the complexity of calculating the best path increases adding more points. And the complexity increases so fast that, even with modern hardware, it can be impossible to compute an exact solution in a reasonable time. In more rigorous terms, it is an NP-hard problem. Many heuristics are known to solve this problem and in this post we will see a solution based onSelf-organizing Maps (SOM). A SOM is a Neural Network that is capable of mapping an input point into a bi-dimnsional space placing points that are close to each other into the same area. Hence, the idea to solve our problem is to train the SOM in order to map the points to visit in single dimension map and visit the points from the one mapped to the first cell (the one on the left) to the last cell (on the right). Points that are mapped to the same cell are visited consecutively.
Let's generate a set of points to test this idea:
import numpy as np import matplotlib.pyplot as plt np.random.RandomState(10) N_points = 20 N_neurons = N_points*2 t = np.linspace(0, np.pi*2, N_points) x = np.cos(t)+(np.random.rand(N_points)-.5)*.3 y = np.sin(t)*.8+(np.random.rand(N_points)-.5)*.2 points = np.array([x,y]).T plt.scatter(x, y)
We can now import MiniSom, our favorite implementation of the Self_Organizing Maps, and see what path it's able to produce:
from minisom import MiniSom
som = MiniSom(1, N_neurons*2, 2, sigma=10,
neighborhood_function='gaussian', random_seed=50)
max_iter = 2000
som.pca_weights_init(points)
paths_x = []
paths_y = []
for i in np.arange(max_iter):
i_point = i % len(points)
som.update(points[i_point], som.winner(points[i_point]), i, max_iter)
visit_order = np.argsort([som.winner(p)[1] for p in points])
visit_order = np.concatenate((visit_order, [visit_order[0]]))
paths_x.append(points[visit_order][:,0])
paths_y.append(points[visit_order][:,1])
plt.scatter(x, y, label='point to visit')
plt.plot(paths_x[-1], paths_y[-1],
'C3', linewidth=2, label='path')
In the snippet above we initialized the SOM and run 2000 training iterations (checkthis out to discover how that works). At each iteration we have saved the path found and visualized the last solution. As we can see, the line covers all the points and it's easy to see that it's the best possible path with just a glance. However, it's interesting to see how the solution evolves at each iteration:
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
fig, ax = plt.subplots()
plt.scatter(x, y, label='point to visit')
ln, = plt.plot([], [], 'C3', linewidth=2, label='path')
plt.legend()
def update(frame):
ln.set_data(paths_x[frame], paths_y[frame])
plt.title('iteration = %d' % frame)
return ln,
ani = FuncAnimation(fig, update, frames=np.arange(max_iter),
interval=10, repeat=False, blit=False)
HTML(ani.to_html5_video())
Here we note that the initial path is very messy and presents various loops and that the more the network is trained the more optimal the solution becomes. Notice that the snippet above uses the objectfrom the IPython library and it will automatically display the video if a Jupyter notebook is used. The video can be saved in a specific location using.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTTP/2基础教程
Stephen Ludin、Javier Garza / 罗正龙、郑维智 / 人民邮电出版社 / 2018-1 / 49.00元
让网站和应用更快速、更简洁、更稳健,从而有效提升用户体验,这无疑是众多开发者梦寐以求的。然而互联网发展日新月异,HTTP/1.1协议已经难以满足现今的需求。在众多Web性能提升方案中,HTTP/2值得尝试。 本书是HTTP/2实用指南,介绍了HTTP/2的设计初衷和新特性,以及如何才能充分利用这些特性来打造高性能网站及应用。作者用定量分析方法,对比了不同网络环境下及不同浏览器上HTTP/1.......一起来看看 《HTTP/2基础教程》 这本书的介绍吧!
HTML 压缩/解压工具
在线压缩/解压 HTML 代码
HEX HSV 转换工具
HEX HSV 互换工具