Traveling Salesman Problem (TSP) By Random Restart Hill Climbing – JAVA 8 Tutorial

this tutorial will cover solving the
Traveling Salesman problem with Java 8 and random restart hill climbing I
will be using code from the previous hill climbing tutorial and modify that
code adding random restart functionality and finally test running the application
I will start by creating a new project and it’s gonna be running on Java 8 and let’s create a new package and open up the hill climbing
project and i will be copying all the classes from here and let me rename this to random restart hill climbing now the
city class and the route class will stay the same
the randomly start hill climbing will be changed and the driver will be
changed also now here let’s remove this compare routes string and for the purpose of this tutorial
let’s say we have 10 random restarts so this method will return an ArrayList
containing 10 routes considered shortest route so we define this
ArrayList and we will return it but before doing that we will have a for
loop calling the find shortest route and I want those routes sorted by shortest
distance so let’s have this method sort routes by distance that uses Java 8 to
do the sorting and we’ll be calling it from here so
this does it for this class next let’s go to the Driver class now here let’s
start by removing this and those two so I will be instantiating a random
restart hill climbing instance and calling the find shortest routes method
on it passing it this route containing those cities and what’s returned is gonna be
stored in the routes ArrayList so here we printed the heading and here we will
print the contents so the cities and the total distance and finally I will print
the shortest route found and the distance for that shortest route and test running this application we
will get so this is the shortest route since they’re sorted by distance and let’s
see if we run it again what we get

Leave a Reply

Your email address will not be published. Required fields are marked *