Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Significantly Sub-optimal Results from SA_TSP #196

Open
johnziebro opened this issue Aug 11, 2022 · 2 comments
Open

Significantly Sub-optimal Results from SA_TSP #196

johnziebro opened this issue Aug 11, 2022 · 2 comments

Comments

@johnziebro
Copy link

johnziebro commented Aug 11, 2022

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.

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

@johnziebro johnziebro changed the title Significantly Sub-optimal Results from SA_TSA Significantly Sub-optimal Results from SA_TSP Aug 11, 2022
@johnziebro
Copy link
Author

Possibly related to #160, however no code was supplied to compare.

@johnziebro
Copy link
Author

johnziebro commented Aug 11, 2022

scikit-opt shows as version 0.6.6, which is strange since 0.6.5 is the newest version.

import sko
sko.__version__

0.6.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant