from scipy.spatial.distance import pdist, squareform
import numpy as np
from matplotlib import pyplot as plt
from m4opt.utils.optimization import solve_tsp

# Construct a random cloud of points.
points = np.random.default_rng(1234).random((30, 2))

# Calculate the Euclidean distances between each pair of points.
dist = squareform(pdist(points))

# Find the shortest path, and the path length.
indices, path_length = solve_tsp(dist)

# Plot the points and the path.
ax = plt.axes(aspect=1, xlim=(0, 1), ylim=(0, 1))
ax.plot(*points[indices].T, "o-")