Beispiel #1
0
 def GA_go(self):
     ga_tsp = GA_TSP(func=self.cal_total_weight,
                     points=self.flow,
                     pop=50,
                     max_iter=100,
                     Pm=0.01)
     best_goods, best_value = ga_tsp.fit()
     print(best_goods, best_value)
     Y_history = pd.DataFrame(ga_tsp.FitV_history)
     fig, ax = plt.subplots(2, 1)
     ax[0].plot(Y_history.index, Y_history.values, '.', color='red')
     Y_history.min(axis=1).cummin().plot(kind='line')
     plt.show()
Beispiel #2
0
import matplotlib.pyplot as plt
from sko.demo_func import function_for_TSP

num_points, points_coordinate, distance_matrix, cal_total_distance = function_for_TSP(
    num_points=50)

# %%

from sko.GA import GA_TSP

ga_tsp = GA_TSP(func=cal_total_distance,
                n_dim=num_points,
                pop=500,
                max_iter=2000,
                Pm=0.3)
best_points, best_distance = ga_tsp.fit()

# %%
fig, ax = plt.subplots(1, 1)
best_points_ = np.concatenate([best_points, [best_points[0]]])
best_points_coordinate = points_coordinate[best_points_, :]
ax.plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r')
plt.show()

#%%

# import matplotlib.pyplot as plt
#
# Y_history = ga_tsp.all_history_Y
# Y_history = pd.DataFrame(Y_history)
# fig, ax = plt.subplots(3, 1)