|
|
import numpy as np
|
|
|
from scipy.optimize import linprog
|
|
|
|
|
|
|
|
|
def cargo_load_planning_linear1(weights, cargo_names, cargo_types_dict, positions, cg_impact, cg_impact_2u, cg_impact_4u,
|
|
|
max_positions):
|
|
|
"""
|
|
|
使用整数线性规划方法计算货物装载方案,最小化重心的变化量。
|
|
|
|
|
|
参数:
|
|
|
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): 总货位的数量。
|
|
|
|
|
|
返回:
|
|
|
result.x: 最优装载方案矩阵。
|
|
|
"""
|
|
|
|
|
|
cargo_types = [cargo_types_dict[name] for name in cargo_names]
|
|
|
|
|
|
num_cargos = len(weights)
|
|
|
num_positions = len(positions)
|
|
|
|
|
|
|
|
|
c = []
|
|
|
for i in range(num_cargos):
|
|
|
for j in range(num_positions):
|
|
|
if cargo_types[i] == 1:
|
|
|
c.append(abs(weights[i] * cg_impact[j]))
|
|
|
elif cargo_types[i] == 2 and j % 2 == 0 and j < len(cg_impact_2u) * 2:
|
|
|
c.append(abs(weights[i] * cg_impact_2u[j // 2]))
|
|
|
elif cargo_types[i] == 4 and j % 4 == 0 and j < len(cg_impact_4u) * 4:
|
|
|
c.append(abs(weights[i] * cg_impact_4u[j // 4]))
|
|
|
else:
|
|
|
c.append(0)
|
|
|
|
|
|
|
|
|
bounds = [(0, 1) for _ in range(num_cargos * num_positions)]
|
|
|
|
|
|
|
|
|
A_eq = []
|
|
|
b_eq = []
|
|
|
for i in range(num_cargos):
|
|
|
constraint = [0] * (num_cargos * num_positions)
|
|
|
for j in range(num_positions):
|
|
|
constraint[i * num_positions + j] = 1
|
|
|
A_eq.append(constraint)
|
|
|
b_eq.append(1)
|
|
|
|
|
|
|
|
|
A_ub = []
|
|
|
b_ub = []
|
|
|
for j in range(num_positions):
|
|
|
constraint = [0] * (num_cargos * num_positions)
|
|
|
for i in range(num_cargos):
|
|
|
constraint[i * num_positions + j] = 1
|
|
|
A_ub.append(constraint)
|
|
|
b_ub.append(1)
|
|
|
|
|
|
|
|
|
for i, cargo_type in enumerate(cargo_types):
|
|
|
if cargo_type == 2:
|
|
|
for j in range(0, num_positions - 1, 2):
|
|
|
constraint = [0] * (num_cargos * num_positions)
|
|
|
constraint[i * num_positions + j] = 1
|
|
|
constraint[i * num_positions + j + 1] = 1
|
|
|
A_ub.append(constraint)
|
|
|
b_ub.append(1)
|
|
|
elif cargo_type == 4:
|
|
|
for j in range(0, num_positions - 3, 4):
|
|
|
constraint = [0] * (num_cargos * num_positions)
|
|
|
constraint[i * num_positions + j] = 1
|
|
|
constraint[i * num_positions + j + 1] = 1
|
|
|
constraint[i * num_positions + j + 2] = 1
|
|
|
constraint[i * num_positions + j + 3] = 1
|
|
|
A_ub.append(constraint)
|
|
|
b_ub.append(1)
|
|
|
|
|
|
|
|
|
A_eq = np.array(A_eq)
|
|
|
b_eq = np.array(b_eq)
|
|
|
A_ub = np.array(A_ub)
|
|
|
b_ub = np.array(b_ub)
|
|
|
c = np.array(c)
|
|
|
|
|
|
|
|
|
result = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='highs')
|
|
|
|
|
|
if result.success:
|
|
|
|
|
|
solution = result.x.reshape((num_cargos, num_positions))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cg_change = 0
|
|
|
for i in range(num_cargos):
|
|
|
for j in range(num_positions):
|
|
|
if cargo_types[i] == 1:
|
|
|
cg_change += solution[i, j] * weights[i] * cg_impact[j]
|
|
|
elif cargo_types[i] == 2 and j % 2 == 0 and j < len(cg_impact_2u) * 2:
|
|
|
cg_change += solution[i, j] * weights[i] * cg_impact_2u[j // 2]
|
|
|
elif cargo_types[i] == 4 and j % 4 == 0 and j < len(cg_impact_4u) * 4:
|
|
|
cg_change += solution[i, j] * weights[i] * cg_impact_4u[j // 4]
|
|
|
|
|
|
return result,cg_change
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
result = []
|
|
|
return result,-1000000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|