|
|
import numpy as np
|
|
|
|
|
|
|
|
|
def cargo_load_planning_heuristic(weights, cargo_names, cargo_types_dict, positions,
|
|
|
cg_impact, cg_impact_2u, cg_impact_4u, max_positions,
|
|
|
max_iterations=1000):
|
|
|
"""
|
|
|
使用两阶段启发式算法计算货物装载方案,最小化重心的变化量。
|
|
|
|
|
|
参数:
|
|
|
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): 总货位的数量。
|
|
|
max_iterations (int): 优化阶段的最大迭代次数。
|
|
|
|
|
|
返回:
|
|
|
solution (np.array): 最优装载方案矩阵。
|
|
|
total_cg_change (float): 最优方案的重心变化量。
|
|
|
"""
|
|
|
|
|
|
cargo_types = [cargo_types_dict[name] for name in cargo_names]
|
|
|
|
|
|
num_cargos = len(weights)
|
|
|
num_positions = len(positions)
|
|
|
|
|
|
|
|
|
solution = np.zeros((num_cargos, num_positions), dtype=int)
|
|
|
|
|
|
|
|
|
occupied = np.zeros(num_positions, dtype=int)
|
|
|
|
|
|
|
|
|
sorted_indices = sorted(range(num_cargos), key=lambda i: (-cargo_types[i], -weights[i]))
|
|
|
|
|
|
|
|
|
for i in sorted_indices:
|
|
|
cargo_type = cargo_types[i]
|
|
|
feasible_positions = []
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
possible_starts = range(0, num_positions)
|
|
|
elif cargo_type == 2:
|
|
|
possible_starts = range(0, num_positions - 1, 2)
|
|
|
elif cargo_type == 4:
|
|
|
possible_starts = range(0, num_positions - 3, 4)
|
|
|
else:
|
|
|
possible_starts = []
|
|
|
|
|
|
for start in possible_starts:
|
|
|
|
|
|
if start + cargo_type > num_positions:
|
|
|
continue
|
|
|
|
|
|
if np.any(occupied[start:start + cargo_type]):
|
|
|
continue
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
cg = abs(weights[i] * cg_impact[start])
|
|
|
elif cargo_type == 2:
|
|
|
cg = abs(weights[i] * cg_impact_2u[start // 2])
|
|
|
elif cargo_type == 4:
|
|
|
cg = abs(weights[i] * cg_impact_4u[start // 4])
|
|
|
feasible_positions.append((start, cg))
|
|
|
|
|
|
|
|
|
if feasible_positions:
|
|
|
best_start, best_cg = min(feasible_positions, key=lambda x: x[1])
|
|
|
solution[i, best_start:best_start + cargo_type] = 1
|
|
|
occupied[best_start:best_start + cargo_type] = 1
|
|
|
else:
|
|
|
|
|
|
for start in range(0, num_positions - cargo_type + 1):
|
|
|
if np.all(occupied[start:start + cargo_type] == 0):
|
|
|
solution[i, start:start + cargo_type] = 1
|
|
|
occupied[start:start + cargo_type] = 1
|
|
|
break
|
|
|
|
|
|
|
|
|
total_cg_change = 0.0
|
|
|
for i in range(num_cargos):
|
|
|
cargo_type = cargo_types[i]
|
|
|
assigned_positions = np.where(solution[i] == 1)[0]
|
|
|
if len(assigned_positions) == 0:
|
|
|
continue
|
|
|
if cargo_type == 1:
|
|
|
total_cg_change += abs(weights[i] * cg_impact[assigned_positions[0]])
|
|
|
elif cargo_type == 2:
|
|
|
total_cg_change += abs(weights[i] * cg_impact_2u[assigned_positions[0] // 2])
|
|
|
elif cargo_type == 4:
|
|
|
total_cg_change += abs(weights[i] * cg_impact_4u[assigned_positions[0] // 4])
|
|
|
|
|
|
|
|
|
for _ in range(max_iterations):
|
|
|
improved = False
|
|
|
for i in range(num_cargos):
|
|
|
cargo_type = cargo_types[i]
|
|
|
current_positions = np.where(solution[i] == 1)[0]
|
|
|
if len(current_positions) == 0:
|
|
|
continue
|
|
|
current_start = current_positions[0]
|
|
|
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
possible_starts = range(0, num_positions)
|
|
|
elif cargo_type == 2:
|
|
|
possible_starts = range(0, num_positions - 1, 2)
|
|
|
elif cargo_type == 4:
|
|
|
possible_starts = range(0, num_positions - 3, 4)
|
|
|
else:
|
|
|
possible_starts = []
|
|
|
|
|
|
best_start = current_start
|
|
|
best_cg = total_cg_change
|
|
|
|
|
|
for start in possible_starts:
|
|
|
if start == current_start:
|
|
|
continue
|
|
|
|
|
|
if start + cargo_type > num_positions:
|
|
|
continue
|
|
|
|
|
|
if np.any(occupied[start:start + cargo_type]):
|
|
|
continue
|
|
|
|
|
|
if cargo_type == 1:
|
|
|
new_cg = abs(weights[i] * cg_impact[start])
|
|
|
elif cargo_type == 2:
|
|
|
new_cg = abs(weights[i] * cg_impact_2u[start // 2])
|
|
|
elif cargo_type == 4:
|
|
|
new_cg = abs(weights[i] * cg_impact_4u[start // 4])
|
|
|
else:
|
|
|
new_cg = 0
|
|
|
|
|
|
|
|
|
temp_cg_change = total_cg_change - (
|
|
|
weights[i] * cg_impact[current_start] if cargo_type == 1 else
|
|
|
weights[i] * cg_impact_2u[current_start // 2] if cargo_type == 2 else
|
|
|
weights[i] * cg_impact_4u[current_start // 4] if cargo_type == 4 else 0
|
|
|
) + new_cg
|
|
|
|
|
|
|
|
|
if temp_cg_change < best_cg:
|
|
|
best_cg = temp_cg_change
|
|
|
best_start = start
|
|
|
|
|
|
|
|
|
if best_start != current_start:
|
|
|
|
|
|
occupied[current_start:current_start + cargo_type] = 0
|
|
|
solution[i, current_start:current_start + cargo_type] = 0
|
|
|
|
|
|
|
|
|
solution[i, best_start:best_start + cargo_type] = 1
|
|
|
occupied[best_start:best_start + cargo_type] = 1
|
|
|
|
|
|
|
|
|
total_cg_change = best_cg
|
|
|
improved = True
|
|
|
|
|
|
if not improved:
|
|
|
break
|
|
|
|
|
|
return solution, total_cg_change
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
weights = [500, 800, 1200, 300, 700, 1000, 600, 900]
|
|
|
cargo_names = ['LD3', 'LD3', 'PLA', 'LD3', 'P6P', 'PLA', 'LD3', 'BULK']
|
|
|
cargo_types_dict = {"LD3": 1, "PLA": 2, "P6P": 4, "BULK": 1}
|
|
|
positions = list(range(44))
|
|
|
cg_impact = [i * 0.1 for i in range(44)]
|
|
|
cg_impact_2u = [i * 0.08 for i in range(22)]
|
|
|
cg_impact_4u = [i * 0.05 for i in range(11)]
|
|
|
max_positions = 44
|
|
|
|
|
|
solution, cg_change = cargo_load_planning_heuristic(
|
|
|
weights, cargo_names, cargo_types_dict, positions,
|
|
|
cg_impact, cg_impact_2u, cg_impact_4u, max_positions,
|
|
|
max_iterations=1000
|
|
|
)
|
|
|
|
|
|
if solution is not None and solution.size > 0:
|
|
|
print("成功找到最优装载方案!")
|
|
|
print("装载方案矩阵:")
|
|
|
print(solution)
|
|
|
print(f"重心的变化量: {cg_change:.2f}")
|
|
|
|
|
|
|
|
|
for i in range(len(weights)):
|
|
|
assigned_positions = []
|
|
|
for j in range(len(positions)):
|
|
|
if solution[i, j] > 0.5:
|
|
|
assigned_positions.append(j)
|
|
|
print(f"货物 {cargo_names[i]} (占 {cargo_types_dict[cargo_names[i]]} 单位): 放置位置 -> {assigned_positions}")
|
|
|
else:
|
|
|
print("未找到可行的装载方案。")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main()
|
|
|
|