内容简介: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.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
JSP & Servlet学习笔记
【台湾】林信良 / 清华大学出版社 / 2012-5 / 58.00元
本书是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习JSP & Servlet时遇到的概念、操作、应用或认证考试等问题及解决方案。 本书针对Servlet 3.0的新功能全面改版,无论是章节架构与范例程序代码,都做了全面更新。书中详细介绍了Servlet/ JSP与Web容器之间的关系,必要时从Tomcat源代码分析,了解Servlet/ JSP如何与容器互动。本书还涵盖了文本处理......一起来看看 《JSP & Servlet学习笔记》 这本书的介绍吧!