#๐Ÿ”’ TSP Hill climb problem with python

11 messages ยท Page 1 of 1 (latest)

tired crestBOT
#

@simple pier

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

simple pier
#
 import random

def randomSolution(tsp):
    cities = list(range(len(tsp)))
    solution = []

    for i in range(len(tsp)):
        randomCity = cities[random.randint(0, len(cities) - 1)]
        solution.append(randomCity)
        cities.remove(randomCity)
    return solution

def routeLength(tsp, solution):
    routeLength = 0
    for i in range(len(solution)):
        routeLength += tsp[solution[i - 1]][solution[i]]
    return routeLength

def getNeighbors(solution):
    neighbors = []
    for i in range(len(solution)):
        for j in range(i + 1, len(solution)):
            neighbor = solution.copy()
            neighbor[i] = solution[j]
            neighbor[j] = solution[i]
            neighbors.append(neighbor)
    return neighbors
#
def getBestNeighbor(tsp, neighbors):
    bestRouteLength = routeLength(tsp, neighbors[0])
    bestNeighbor = neighbors[0]
    for neighbor in neighbors:
        currentRouteLength = routeLength(tsp, neighbor)
        if currentRouteLength < bestRouteLength:
            bestRouteLength = currentRouteLength
            bestNeighbor = neighbor
    return bestNeighbor, bestRouteLength

def hillclimbing(tsp):
    # Generate a random initial solution
    currentSolution = randomSolution(tsp)
    initialSolution = currentSolution[:]  # Store the initial solution
    currentRouteLength = routeLength(tsp, currentSolution)
    initialRouteLength = currentRouteLength  # Store the initial route length

    # Get neighbors of the initial solution
    neighbors = getNeighbors(currentSolution)

    # Find the best neighbor
    bestNeighbor, bestNeighborRouteLength = getBestNeighbor(tsp, neighbors)

    # Print the initial solution and its neighbors
    print("Initial Solution:", initialSolution)
    print("Neighbors of the Initial Solution:")
    for neighbor in neighbors:
        print(neighbor)

    # Continue hill climbing until no better solution is found
    while bestNeighborRouteLength < currentRouteLength:
        # Update current solution and route length
        currentSolution = bestNeighbor
        currentRouteLength = bestNeighborRouteLength

        # Get neighbors of the current solution
        neighbors = getNeighbors(currentSolution)

        # Find the best neighbor
        bestNeighbor, bestNeighborRouteLength = getBestNeighbor(tsp, neighbors)

    # Print the best solution and route length found during hill climbing
    print("Best Solution:", currentSolution)
    print("Best Route Length:", currentRouteLength)
#
    return currentSolution, currentRouteLength

def main():
    tsp = [[0, 7, 20, 15, 12],
           [7, 0, 6, 14, 18],
           [20, 6, 0, 15, 30],
           [15, 14, 15, 0, 2],
           [12, 18, 30, 2, 0]]

    hillclimbing(tsp)

if __name__ == "__main__":
    main()
#

Hello, im doing the travel salesman problem using hill climb algorithm in python and i was wondering how i would visualize the results of this code in pycharm like what to import and stuff, or a better ide
or even a better solution to what i have

#

i think i solved this part of my project

#

im busy with this part here, if anyone has tips that would be cool

#

this is the visualization part which im not sure about

tired crestBOT
#

@simple pier

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.