0

To summarise the issue I will cover the implementation explanation and results.

The problem I am aiming to solve with the algorithm is to correct some results gathered through a Randomised nearest neighbour implementation, for anyone who is unfamiliar with this it is very similar to other nearest neighbour algorithms. Random nearest neighbour selects a set of nearest items then randomly selects from those.

my results are varied but consistently provide a strong candidate for the path which we are trying to generate.

so my next step is to pass those resulting points along with the distance values for each potential path to the next algorithm.

my paths are open ended, meaning the start point A is not the end point.

so here is some code for the 2 opt algorithm I will include both the core algorithm code and the 2 opt swap code.


        var newDistance = CLLocationDistance()
        var newRoute = Array<Int>()
        var routeHolder = Array<Array<Int>>()
        var distanceHolder = Array<CLLocationDistance>()
        var preExistingRoute = routeToProcess
        var bestDistanceAvailable = currentDistanceEfficiency
        var hasImproved = true
        
        while hasImproved{
            bestDistanceAvailable = currentDistanceEfficiency
            for i in 1..<routeToProcess.count - 1{
                for k in (i + 1)..<routeToProcess.count {
                    //Two opt operations happen here
                    newRoute = twoOptSwapActions(routeToAlter: preExistingRoute, iIndex: i, kIndex: k)
                    let distanceElements = twoOptDistanceCalculation(listOfIndices: newRoute)
                    newDistance = calculateTotalDistance(distanceToCalculate: distanceElements)

                    print (newDistance, currentDistanceEfficiency ,"distance values new, curr")
                    if newDistance < currentDistanceEfficiency {
                        preExistingRoute = newRoute
                        bestDistanceAvailable = currentDistanceEfficiency
                        routeHolder.append(newRoute)
                        distanceHolder.append(newDistance)
                        hasImproved = true
                    }
                }
            }
            hasImproved = false
        }
        
       return (routeHolder, distanceHolder)
    } 

This is the core algorithm code and the actual 2 opt swapping method is below in the next segment.

        var firstHalf = Array<Int>()
        var secondHalf = Array<Int>()
        var completedCorrection = Array<Int>()
        
        firstHalf = Array(routeToAlter[0...(iIndex - 1)])
        print(firstHalf, "the first half of our process")
        completedCorrection.append(contentsOf: firstHalf)
        secondHalf = Array(routeToAlter[(iIndex)...kIndex])//step 2 -
        print(secondHalf)
        completedCorrection.append(contentsOf: secondHalf.reversed())
        //reverse the contents - step 2
        print(routeToAlter[kIndex], "the value held in the array at the kindex value")
        if kIndex != routeToAlter.count - 1 {
            completedCorrection.append(contentsOf: routeToAlter[kIndex + 1 ... routeToAlter.count - 1])//step 3
        }
        print(completedCorrection)
        return completedCorrection
    } 

finally to put my results into perspective, 9/10 times I run the algorithm I will receive no results for the 2 opt correction.

other times I will run this on e.g 2000 routes and might get lucky with 5 - 6 results, however those results are no use to me as they are not an improvement on the best route found within my Random Nearest Neighbour algorithm.

Any help toward finding the issue or reconfiguring this algorithm would be greatly appreciated, if there is anything I have not included please notify me so I can make sure the relevant information is present.

AR1412
  • 11
  • 1
  • 1
    I would try to find a reproducable small failing example (by using fixed seed for the random-number-generator) and than start debugging. – MrSmith42 Nov 24 '21 at 11:11
  • Hello, thank you for your response. I have attempted debugging through this method and have seen no indication of the issue, im struggling to understand what's wrong. I feel the issue is routed in the methodology rather than the execution. The problem is definitely routed within he segments I have included. Im not sure where though and there are very few sources on the matter. – AR1412 Nov 24 '21 at 14:15

0 Answers0