Have you heard about the Optimization Problem?
By: Cornellius Yudha Wijaya
Tags: Education, Programming, Data Science, Data

Real Application — Travelling Salesperson Problems

The travelling salesperson problem (TSP) is an optimization problem where we try to determine the shortest tour of the n cities (or house, station, etc. any kind you like), starting and ending at the same point and visiting all of the locations once. For example, I have a collection of five cities in the grid below.

I want the salesperson to travel all the cities exactly once with starting and ending in the same city. If we set the solution vector x = [0,1,2,3,4] and assuming the city start at 0, then the salesman would travel just like in the grid below.

The route travelled seems not the most optimal path. This is where we try to find the shortest route to go or the optimization problem.

For this article, I would use an optimization problem module called mlrose. If you want to install the module, you can run the following code.

#Installing using pip

pip install mlrose

Solving an optimization problem using mlrose involves three steps:

Define a fitness function object (The function we like to minimize/maximize). Define an optimization problem object (Object that contains all of the essential information about the optimization problem we are trying to solve). Run a randomized optimization algorithm.

First, we need to define the fitness function object. Luckily, mlrose already had TravellingSales() class is to be used to define the fitness function object. Initially, TravellingSales() class would need (x, y) coordinates of all the cities (or the distances).

import mlrose # List of city coordinates

coords = [(1, 0), (3, 2), (7, 7), (1, 4), (5, 2)] # Initialize fitness function object

fitness_coords = mlrose.TravellingSales(coords = coords)

After initiating the fitness function, we would then define our optimization problem object. The mlrose module already had the TSPOpt() as optimization problem object. We only need to define the object by inputting a few parameters. In the example above, we want to solve a minimization problem of length 5. In that case, we define the optimization object below

# Define optimization problem object. length parameter is the number of the cities, fitness_fn is the fitness function, and we set maximize to false as we want the minimal distance problem = mlrose.TSPOpt(length = 5, fitness_fn = fitness_coords, maximize=False)

We now ready to select a randomized optimization algorithm and use it to solve our problem. The randomized optimization algorithm is base on the Evolutionary Algorithm, I would not explain the matter in deep, but the parameter used in the mlrose module is based on that. In our example, I try to set the mutation_prob parameter (Just think this parameter as the rate of how big the state to change based on the current best state) to 0.2 and the max_attemps (The repetition number) to 100.

best_state, best_fitness = mlrose.genetic_alg(problem, mutation_prob = 0.2,max_attempts = 100, random_state = 2) print('Best State is:')

print(best_state) print('Best Fitness (Total Distances) is:')

print(best_fitness)

We can see the best state the algorithm found is [1,0,3,2,4]. I also present the Best Fitness to show the total distances we took, as what we minimize in this problem is the total distance (Euclidean Distance to be exact). If we visualize it, our salesman will walk in the following route.

And that’s it. We are already applying the Optimization Problem to our Travelling Salesman Problem and found the best route that the salesman could traverse. This is how we use the Optimization Problem to solve the question we have.