Simulated Annealing TSP

限于喜欢 提交于 2020-01-20 05:09:42

问题


I'm looking to implement the simulated annealing algorithm in Java to find an optimal route for the Travelling Salesman Problem, so far I have implemented brute force and am looking to modify that code in order to use simulated annealing. Obviously brute-force and simulated annealing are very different and use very different functions.

I understand simulated annealing uses a variable known as the temperature which then cools as the algorithm runs; with the temperature starting high and it gradually cooling throughout. Whilst the temperature is high the algorithm is more likely to choose solutions which are worse than the current, eliminating local maxima as you'd find in the similar hill-climbing algorithm. As it cools the algorithm is more unlikely to accept worse solutions and so it can focus on a specific area and an optimum route is found quickly.

I believe I understand how the algorithm works but am having trouble putting this into Java, I have 2 classes; one called City which just contains methods to work out details of each city such as getIndex, getDistance, etc. The algorithm class reads from an input file and stores it in an array (int [][])

The code below is the algorithm for brute-force which is what I want to modify to do simulated annealing instead, if anyone could help me do that I'd appreciate it massively.

public static void doBF()
{
    int random1 = generateRand();

    if (towns2.size() > random1)
    {
        Town town = towns2.get(random1);
        visitedTowns[i] = town;
        towns2.remove(town);
        i++;
        if (lastTown != 1000)
        {
            journey += town.getDistance(lastTown);
        }
        lastTown = town.getIndex();
    }
    else 
    {
        doBF();
    }
}

回答1:


I don't want to show too much code, since it's part of an application belonging to my ongoing bachelor thesis. But here you go. The algorihtm should be kept very general. Take a look.

MAIN PART OF ALGORITHM

// one could check for minimum q factor to be satisfied here
while (temperature > 1)
{
  state.step();
  int next = state.energy();

  if (acceptEnergyLevel(next))
  {
    energy = next;

    if (energy < minEnergy)
    {
      minState = state.copy();
      minEnergy = energy;
    }
  }
  else
    state.undo();
  temperature *= DECAY_RATE;
}


STATE INTERFACE

public interface State<T extends State<T>>
{
  public void step();
  public void undo();
  public int energy();
  public T copy();
}

With this as basis for your algorithm you can solve any problem. Not just TSP. You just have to implement the State interface, e.g. TspProblemInstance implements State<TspProblemInstance>. The algorithm is generic and will return the optimum (or a result very near to the optimum) object of class TspProblemInstance. Therefore it is important that you implement the copy method diligently. The generic parameter T is bound by the implementing class, i. e. the copy will always have type T (a subtype could also be possible).

You should add some methods to your concrete implementation of the interface, that show you the sequence of the cities etc. The methods in the State interface are only the minimum for the algorithm to work on.

I recommend the wiki article for further reading. And here two other implementations, the first is a bit complex, and the second is rather simple, but hackish (and not kept general). But they should give you more understanding of simulated annealing.




回答2:


Take a look at http://www.theprojectspot.com/tutorial-post/simulated-annealing-algorithm-for-beginners/6. It provides a well documented example on how to use simulated annealing to solve the TSP problem.



来源:https://stackoverflow.com/questions/17281954/simulated-annealing-tsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!