I am working on a project for a math class at school, and I decided to tackle my problems at Traveling Salesman, which I always wanted to explore more. However, I have problems with my brute force algorithm.
* Go to the update below to view the latest version of the code
SKIP THIS PARAGRAPH IF YOU KNOW THAT THE PROBLEM OF TRAVELING: Summing up as much as possible, TSP looks like this: you are a seller who wants to visit every city in the region (the city is essentially a point on the map). In a limited area of โโx and y there are "n" cities, and each city is connected to each city (on a straight road). You need to find the shortest route among the cities, which allows you to visit each city. One of the algorithms that I want to use (and I will need to test other algorithms) is Brute Force, which checks all possible routes and returns the shortest route. The reason this is not always used is because it requires a check (n-1)! possible routes, and this number becomes huge as the "n" increases - in fact, only 50 cities, it will be 608281864034267560872252163321295376887552831379210240000000000 routes to check.
TAKE FOR ALL EXAMPLES TALKED IN THIS MAIL THAT WE ARE USING THE ARBITRATION REGION WITH 4 CITIES (although the algorithm can process the cities, but we do not care about the distance - we want to hit every possible route in brute force).
Here is a simple picture demonstrating what I'm talking about (4 cities is where I start to check if the process works correctly)
map image with cities
Here is the Brute Force algorithm (suppose all other called methods work correctly because they do it):
(see below for more details)
[code]
public void BruteForceFindBestRoute(Route r)
Here is my logic: (Note: the city object has the boolean variable "flag" called "visited").
(if all routes are not marked, and if the route does not contain every possible city)
- start with a route with 1 unflagged city.
- check the "last unpolluted" city (this city is a "hinge")
- Find each city that is not in ROUTE R and add it to the new route.
- call the BruteForce method recursively on each of these routes.
(if all routes are not marked, but the route contains each city)
(otherwise ... this means that the route has every city flag and contains every possible city)
- see if this is the shortest route - if there is one, save it in a global variable
This image will help me explain the problem ... Thus, the program runs correctly on the left side. However, after it reaches the bottom, it can be expected that the recursion will return to step 4, and this will happen. However, instead of R having a city A and flag B, and then recursively calling itself a โnew routeโ containing Aflag and B, R now has all 4 cities, and all 4 are marked with an icon. It fails because it again adds city D to "newRoute", recursively calls itself again, and in another method we get an error outside the borders because there are no 5 cities in my region, but 5 cities located on route r (A, B, C, D, D).
Useful picture of a recursive tree structure
The problem is with the recursion call in the loop or the "r" route referenced by the recursive call.
If you have an idea of โโwhat I need to do, I seriously appreciate some help.
Thanks to everyone who will help me. I will send the whole project to everyone who wants to help too.
UPDATE
Ok, so I tried to shorten and simplify my original method, and this is what I have:
public void BruteForceFindBestRoute(Route r, ArrayList<City> citiesNotInRoute) { if(!citiesNotInRoute.isEmpty()) { for(int i = 0; i<citiesNotInRoute.size(); i++) { City justRemoved = (City) citiesNotInRoute.remove(0).clone(); Route newRoute = (Route) r.clone(); newRoute.addToRoute(justRemoved); BruteForceFindBestRoute(newRoute, citiesNotInRoute); citiesNotInRoute.add(justRemoved); } } else
The problem is that the variable i inside the for loop seems to lose it when we exit the recursion and the loop does not continue. Ideas?