2选择算法以最佳方式解决TSP

时间:2016-04-20 19:56:26

标签: java algorithm traveling-salesman simulated-annealing

我正在尝试在java中找到旅行商问题的解决方案。我已经应用模拟退火以下列方式解决这个问题。这是我实现模拟退火的代码段:

{{1}}

当我使用上述方式完成TSP问题时,我发现图片中有很多交叉,我听说2-Opt Arithmetic可以解决这个问题。基本上,我想创建两个顶点的Tours,或者基本上是一组独特的边缘。现在对于每个唯一的边对(x,y)和(u,v),如果成本(x,y)+成本(u,v)< cost(x,u)+ cost(y,v),然后我将使用edge(x,y)和(u,v)over(x,u)和(y,v)。我会为每一对独特的边缘重复这个过程,直到成本没有降低。

但是我如何找到应用2-opt技术的独特边缘?我的意思是,如果我之前生成一个解决方案(如上面的代码中所示),我将如何在解决方案中找到交叉边缘(我需要检查以应用2 opt)?

1 个答案:

答案 0 :(得分:0)

2-Opt可以实现为子巡回逆转。

int[] tour= new int[]{1,2,3,6,5,4,7,8,9};
List<int[]> neighboors = new ArrayList<int[]>();
for (int i = 0; i<tour.length; ++i) {
  for (int j = i+1; j<tour.length; ++j) {
    neighboors.add(opt2(tour,i,j));
  }
}
function int[] opt2(tour, i,j) {
  int n = tour.length;
  int d = Math.abs(j-i);
  if (j<i) return null;//or fix it
  d = (d+1)/2;//d+1==element count... /2 === number of pairs to swap
  for (int k = 0; k <d; k++) {
    //swap tour[i+k] and tour[j-k]
  }
}

opt2(tour,3,5)={1,2,3,4,5,6,7,8,9}这会将(3,6)(4,7)(3,4)(6,7)交换。 opt2(tour,2,6)={1,2,7,4,5,6,3,8,9}(2,3)(7,8)(2,7)(3,8)交换为边缘{"reverse begining", "keep the same", "reverse ending"}

请注意,您不需要考虑在数组的开头或结尾处进行反转。 reverseTheWholeArray({"keep the same", "reverse the middle", "keep the same"}),因为它与{1,2,3,4}相同。这是{4,3,2,1}之旅与const video = document.getElementById('video'); const caption = document.getElementsByTagName('span'); video.addEventListener('timeupdate', () => { for (i = 0; i < caption.length; i += 1) { let captionTime = caption[i].getAttribute('data-time'); if (captionTime === video.currentTime) { caption.style.backgroundColor = 'yellow'; console.log(captionTime); } } }); <video id="video" width="400" height="225"> <source type="video/mp4" src="video/video.mp4"> <source type="video/ogg" src="video/video.ogg"> </video> </div> <div class="transcript-wrapper"> <p> <span data-time = "0.240"> Now that we've looked at the architecture of the internet, let's see how you might</span> <span data-time ="4.130"> connect your personal devices to the internet inside your house.</span> <span data-time="7.535"> Well there are many ways to connect to the internet, and <span data-time="11.270"> most often people connect wirelessly.</span> </p> <p> <span data-time="13.960"> Let's look at an example of how you can connect to the internet.</span> <span data-time="17.940"> If you live in a city or a town, you probably have a coaxial cable for</span> <span data-time="22.370"> cable Internet, or a phone line if you have DSL, running to the outside of</span> <span data-time="26.880"> your house, that connects you to the Internet Service Provider, or ISP.</span> <span data-time="32.100"> If you live far out in the country, you'll more likely have</span> <span data-time="34.730"> a dish outside your house, connecting you wirelessly to your closest ISP, or</span> <span data-time="39.430"> you might also use the telephone system.</span> </p> <p> <span data-time="42.350"> Whether a wire comes straight from the ISP hookup outside your house, or</span> <span data-time="46.300"> it travels over radio waves from your roof,</span> <span data-time="49.270"> the first stop a wire will make once inside your house, is at your modem.</span> <span data-time="53.760"> A modem is what connects the internet to your network at home.</span> <span data-time="57.780"> A few common residential modems are DSL or</span> </p> </div> <script src="jquery-3.2.1.min"></script> <script src="mediaelementplayer/mediaelement-and-player.min.js"></script> <script src="main.js"></script> 相同。

现在有趣的部分是实现3-Opt可以通过巡回“旋转”和反转&gt; :)或最多3次反转来实现。