|
|
import numpy as np
|
|
|
import pyswarms as ps
|
|
|
|
|
|
|
|
|
def cargo_load_planning_pso(weights, cargo_names, cargo_types_dict, positions, cg_impact, cg_impact_2u, cg_impact_4u,
|
|
|
max_positions, options=None, swarmsize=100, maxiter=100):
|
|
|
"""
|
|
|
使用粒子群优化方法计算货物装载方案,最小化重心的变化量。
|
|
|
|
|
|
参数:
|
|
|
weights (list): 每个货物的质量列表。
|
|
|
cargo_names (list): 每个货物的名称。
|
|
|
cargo_types_dict (dict): 货物名称和占用的货位数量。
|
|
|
positions (list): 可用的货位编号。
|
|
|
cg_impact (list): 每个位置每kg货物对重心index的影响系数。
|
|
|
cg_impact_2u (list): 两个位置组合的重心影响系数。
|
|
|
cg_impact_4u (list): 四个位置组合的重心影响系数。
|
|
|
max_positions (int): 总货位的数量。
|
|
|
options (dict, optional): PSO算法的配置选项。
|
|
|
swarmsize (int, optional): 粒子群大小。
|
|
|
maxiter (int, optional): 最大迭代次数。
|
|
|
|
|
|
返回:
|
|
|
best_solution (np.array): 最优装载方案矩阵。
|
|
|
best_cg_change (float): 最优方案的重心变化量。
|
|
|
"""
|
|
|
|
|
|
cargo_types = [cargo_types_dict[name] for name in cargo_names]
|
|
|
|
|
|
num_cargos = len(weights)
|
|
|
num_positions = len(positions)
|
|
|
dimension = num_cargos
|
|
|
|
|
|
|
|
|
if options is None:
|
|
|
options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
|
|
|
|
|
|
|
|
|
def fitness_function(x):
|
|
|
"""
|
|
|
计算每个粒子的适应度值。
|
|
|
|
|
|
参数:
|
|
|
x (numpy.ndarray): 粒子的位置数组,形状为 (n_particles, dimension)。
|
|
|
|
|
|
返回:
|
|
|
numpy.ndarray: 每个粒子的适应度值。
|
|
|
"""
|
|
|
fitness = np.zeros(x.shape[0])
|
|
|
|
|
|
for idx, particle in enumerate(x):
|
|
|
|
|
|
start_positions = []
|
|
|
penalty = 0
|
|
|
cg_change = 0.0
|
|
|
occupied = np.zeros(num_positions, dtype=int)
|
|
|
|
|
|
for i in range(num_cargos):
|
|
|
cargo_type = cargo_types[i]
|
|
|
pos_continuous = particle[i]
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
start_pos = int(np.round(pos_continuous)) % num_positions
|
|
|
elif cargo_type == 2:
|
|
|
start_pos = (int(np.round(pos_continuous)) // 2) * 2
|
|
|
if start_pos >= num_positions - 1:
|
|
|
start_pos = num_positions - 2
|
|
|
elif cargo_type == 4:
|
|
|
start_pos = (int(np.round(pos_continuous)) // 4) * 4
|
|
|
if start_pos >= num_positions - 3:
|
|
|
start_pos = num_positions - 4
|
|
|
else:
|
|
|
start_pos = 0
|
|
|
|
|
|
|
|
|
if start_pos < 0 or start_pos + cargo_type > num_positions:
|
|
|
penalty += 1000
|
|
|
continue
|
|
|
|
|
|
|
|
|
if cargo_type == 2 and start_pos % 2 != 0:
|
|
|
penalty += 1000
|
|
|
if cargo_type == 4 and start_pos % 4 != 0:
|
|
|
penalty += 1000
|
|
|
|
|
|
|
|
|
if np.any(occupied[start_pos:start_pos + cargo_type]):
|
|
|
penalty += 1000
|
|
|
else:
|
|
|
occupied[start_pos:start_pos + cargo_type] = 1
|
|
|
|
|
|
start_positions.append(start_pos)
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
cg_change += weights[i] * cg_impact[start_pos]
|
|
|
elif cargo_type == 2:
|
|
|
cg_change += weights[i] * cg_impact_2u[start_pos // 2]
|
|
|
elif cargo_type == 4:
|
|
|
cg_change += weights[i] * cg_impact_4u[start_pos // 4]
|
|
|
|
|
|
fitness[idx] = cg_change + penalty
|
|
|
|
|
|
return fitness
|
|
|
|
|
|
|
|
|
|
|
|
lower_bounds = []
|
|
|
upper_bounds = []
|
|
|
for i in range(num_cargos):
|
|
|
cargo_type = cargo_types[i]
|
|
|
if cargo_type == 1:
|
|
|
lower_bounds.append(0)
|
|
|
upper_bounds.append(num_positions - 1)
|
|
|
elif cargo_type == 2:
|
|
|
lower_bounds.append(0)
|
|
|
upper_bounds.append(num_positions - 2)
|
|
|
elif cargo_type == 4:
|
|
|
lower_bounds.append(0)
|
|
|
upper_bounds.append(num_positions - 4)
|
|
|
else:
|
|
|
lower_bounds.append(0)
|
|
|
upper_bounds.append(num_positions - 1)
|
|
|
|
|
|
bounds = (np.array(lower_bounds), np.array(upper_bounds))
|
|
|
|
|
|
|
|
|
optimizer = ps.single.GlobalBestPSO(n_particles=swarmsize, dimensions=dimension, options=options, bounds=bounds)
|
|
|
|
|
|
|
|
|
best_cost, best_pos = optimizer.optimize(fitness_function, iters=maxiter)
|
|
|
|
|
|
|
|
|
best_start_positions = []
|
|
|
penalty = 0
|
|
|
cg_change = 0.0
|
|
|
occupied = np.zeros(num_positions, dtype=int)
|
|
|
|
|
|
for i in range(num_cargos):
|
|
|
cargo_type = cargo_types[i]
|
|
|
pos_continuous = best_pos[i]
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
start_pos = int(np.round(pos_continuous)) % num_positions
|
|
|
elif cargo_type == 2:
|
|
|
start_pos = (int(np.round(pos_continuous)) // 2) * 2
|
|
|
if start_pos >= num_positions - 1:
|
|
|
start_pos = num_positions - 2
|
|
|
elif cargo_type == 4:
|
|
|
start_pos = (int(np.round(pos_continuous)) // 4) * 4
|
|
|
if start_pos >= num_positions - 3:
|
|
|
start_pos = num_positions - 4
|
|
|
else:
|
|
|
start_pos = 0
|
|
|
|
|
|
|
|
|
if start_pos < 0 or start_pos + cargo_type > num_positions:
|
|
|
penalty += 1000
|
|
|
best_start_positions.append(start_pos)
|
|
|
continue
|
|
|
|
|
|
|
|
|
if cargo_type == 2 and start_pos % 2 != 0:
|
|
|
penalty += 1000
|
|
|
if cargo_type == 4 and start_pos % 4 != 0:
|
|
|
penalty += 1000
|
|
|
|
|
|
|
|
|
if np.any(occupied[start_pos:start_pos + cargo_type]):
|
|
|
penalty += 1000
|
|
|
else:
|
|
|
occupied[start_pos:start_pos + cargo_type] = 1
|
|
|
|
|
|
best_start_positions.append(start_pos)
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
cg_change += abs(weights[i] * cg_impact[start_pos])
|
|
|
elif cargo_type == 2:
|
|
|
cg_change += abs(weights[i] * cg_impact_2u[start_pos // 2])
|
|
|
elif cargo_type == 4:
|
|
|
cg_change += abs(weights[i] * cg_impact_4u[start_pos // 4])
|
|
|
|
|
|
total_cg_change = cg_change + penalty
|
|
|
|
|
|
|
|
|
best_xij = np.zeros((num_cargos, num_positions), dtype=int)
|
|
|
for i, start_pos in enumerate(best_start_positions):
|
|
|
cargo_type = cargo_types[i]
|
|
|
for k in range(cargo_type):
|
|
|
pos = start_pos + k
|
|
|
if pos < num_positions:
|
|
|
best_xij[i, pos] = 1
|
|
|
|
|
|
|
|
|
if total_cg_change >= -999999:
|
|
|
return best_xij, total_cg_change
|
|
|
else:
|
|
|
return [], -1000000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|