def kakak(): ga_tsp = GA_TSP(func=cal_total_weight, n_dim=flow_nums, size_pop=50, max_iter=200, prob_mut=0.06) best_goods, best_value = ga_tsp.run() print(best_goods, best_value) point2(ga_tsp)
def _GA_start(flows_len): if(flows_len%2!=0): flows_len=flows_len-1 len=flows_len ga_tsp = GA_TSP(func=_GA_Fit, n_dim=len, size_pop=len, max_iter=200,prob_mut=0.06) best_goods, best_value = ga_tsp.run() print(best_goods, best_value) #point2(ga_tsp) return best_goods
def kaka(self): ga_tsp = GA_TSP(func=cal_total_weight, n_dim=flow_nums, size_pop=flow_nums, max_iter=500, prob_mut=0.01) best_goods, best_value = ga_tsp.run() print(best_goods, best_value) fit_history = pd.DataFrame(ga_tsp.all_history_Y) fig, ax = plt.subplots(2, 1) ax[0].plot(fit_history.index, fit_history.values, '.', color='red') plt_min = fit_history.min(axis=1) ax[1].plot(plt_min.index, plt_min, Label='min') ax[1].plot(plt_min.index, plt_min.cummin()) # fit_history.max(axis=1).cummin().plot(kind='line') plt.show()
def Get_Data(_num_points=20): num_points = _num_points points_coordinate = np.random.rand(num_points, 2) * 10 # generate coordinate of points 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) ]) # %% do GA ga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1) best_points, best_distance = ga_tsp.run() return points_coordinate.tolist(), best_points, best_distance
def getTotalDistance(routine): num_points, = routine.shape return sum([ distanceMatrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points) ]) ga_tsp = GA_TSP(func=getTotalDistance, n_dim=NUM_POINT, size_pop=50, max_iter=500, prob_mut=1) best_points, best_distance = ga_tsp.run() print('\n遗传旅行商\n') print(best_points, '\nbest Distance:', best_distance) # fig, ax = plt.subplots(1, 2) # best_points_ = np.concatenate([best_points, [best_points[0]]]) # best_points_coordinate = pointsCoordinate[best_points_, :] # ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r') # ax[1].plot(ga_tsp.generation_best_Y) # plt.show() ''' 粒子群算法 ''' def func3(x):
def runGA(): ga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=8, max_iter=10, prob_mut=0.1) return ga_tsp.run()