guofei9987 / guofei9987/scikit-opt

Significantly Sub-optimal Results from SA_TSP

Open
#196 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Using code from the SA_TSP example at: https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa_tsp.py I am receiving significantly sub-optimal results.

```python
import numpy as np
from sko.SA import SA_TSP
from scipy import spatial
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

num_points = 20
points_coordinate = np.random.rand(num_points, 2)

# using SA_TSP example code
num_points = points_coordinate.shape[0]
distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')

def cal_total_distance(routine):
'''The objective function. input routine, return total distance.
cal_total_distance(np.arange(num_points))
'''
num_points, = routine.shape
return sum([distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points)])

sa_tsp = SA_TSP(func=cal_total_distance, x0=range(num_points), T_max=100, T_min=1, L=10 * num_points)
best_points, best_distance = sa_tsp.run()

# Plot both solutions
fig, ax = plt.subplots(1, 2)

best_points_ = np.concatenate([best_points, [best_points[0]]])
best_points_coordinate = points_coordinate[best_points_, :]

ax[0].plot(sa_tsp.best_y_history)
ax[0].set_xlabel("Iteration")
ax[0].set_ylabel("Distance")
ax[0].set_title('Optimization')
ax[1].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1],
marker='o', markerfacecolor='b', color='c', linestyle='-')
ax[1].xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
ax[1].yaxis.set_major_formatter(FormatStrFormatter('%.3f'))
ax[1].set_xlabel("X")
ax[1].set_ylabel("Y")
ax[1].set_title('Coordinates')

plt.show()
```
![image](https://user-images.githubusercontent.com/90479072/184193466-a1698e73-6fb5-4e6a-9bfb-369bb1aaf95f.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.