guofei9987 / scikit-opt

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)

Home Page:https://scikit-opt.github.io/scikit-opt/#/en/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Significantly Sub-optimal Results from SA_TSP

johnziebro opened this issue · comments

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

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

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