# 粒子群算法求解物流配送路线问题（python）

## 5.处理关键技术

### 5.1将不可行解转化为可行解

def get_feasible_x(x):
# 修正粒子为满足约束的可行解粒子，超过其范围时按边界取值
vehicle_element = np.rint(x[:demand_node_num])  # 取整数表示哪辆车拜访哪个客户点
for index in range(len(vehicle_element)):
# x_bound = [[1, vehicle_num], [1, demand_node_num]]  # 自变量的上下限限制
if vehicle_element[index] < x_bound[0][0]:
vehicle_element[index] = x_bound[0][0]
elif vehicle_element[index] > x_bound[0][1]:
vehicle_element[index] = x_bound[0][1]
x[:demand_node_num] = vehicle_element
return x


def get_feasible_solution(x):
"""
# 判断是否满足车辆最大行驶里程数限制和车辆最大载重量限制，若原粒子不满足条件则生成满足约束条件的可行解
:param x: x为一个粒子
:return:
"""
x = get_feasible_x(x)
length, total_path_list = calculate_fitness(x)
result_list = []
for i in range(len(total_path_list)):
path_list = total_path_list[i]
path_length = 0
for j in range(len(path_list)-1):
segment = [path_list[j], path_list[j+1]]
path_length += distance_graph[segment[0], segment[1]]
if path_length > length_max:
new_x = set_codeForm(1)[0]
result_list.append(False)
else:
# 候选解满足车辆行驶最大里程数要求，再判断车辆的实际装载量是否在最大装载量范围内
path_list = total_path_list[i][1:-1]
capacity = sum([quantity_graph[j-1] for j in path_list])
if capacity > capacity_max:
new_x = set_codeForm(1)[0]
result_list.append(False)
else:
result_list.append(True)
if False not in result_list:
return x, length, total_path_list
else:
return get_feasible_solution(new_x)


### 5.2根据解码规则计算适应度函数值

def calculate_fitness(x):
"""
# 计算适应度函数
:param x: x为一个粒子
:return:
"""
# x = [[1.6,1.3,2.9,2.5,3.4,3.5,1.3,2,3,1,2,2,1,1]]
total_path_list = []
vehicle_element = x[:demand_node_num]
path_element = x[demand_node_num:]
actual_vehicle = list(set(vehicle_element))
for j in range(len(actual_vehicle)):
# customer_list表示第j辆车负责配送的客户位置索引列表
customer_list = np.where(vehicle_element == actual_vehicle[j])
path_list = path_element[customer_list]
path_list = [customer_list[0][customer_index] for customer_index in np.argsort(path_list)]
path_list = [element + 1 for element in path_list]
path_list.insert(0, 0)
path_list.append(0)
# path_list表示第j辆车访问各个客户节点的拜访顺序
total_path_list.append(path_list)
# 0-3-1-2-0
total_length = 0
for path in total_path_list:
for index in range(len(path)-1):
segment = [path[index], path[index+1]]
total_length += distance_graph[segment[0]][segment[1]]


## 6.算例结果

Matlab实现粒子群算法的程序代码：https://www.cnblogs.com/kexinxin/p/9858664.html
matlab代码求解函数最优值：https://blog.csdn.net/zyqblog/article/details/80829043

numpy模块中矩阵和数组的区别：https://www.cnblogs.com/wj-1314/p/10244807.html
random模块的常用方法：https://www.cnblogs.com/liangmingshen/p/8909376.html
numpy模块入门级用法介绍：
https://www.jianshu.com/p/3e566f09a0cf
matplotlib模块简单使用技巧：https://www.cnblogs.com/bainianminguo/p/10952586.html
PYTHON 画图神器 Matplotlib：https://www.jianshu.com/p/c41ac57cea33

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客