python调用CPLEX求解——装箱问题
https://blog.csdn.net/weixin_45870844/article/details/106865119
问题描述
假设现有m = 5 个装货空箱,每个装货空箱的最大载重量是W = 5000,现有n = 8个客户需要运送货物,每个客户的货物重量是 weight = [1050, 1730, 2575, 3540, 1220, 1340, 1530, 1270],如何用最少的箱子将这个货物都运出去呢?
建模
#导入包
from docplex.mp.model import Model
# 相关数据和类型的简单处理
m = 5
n = 8
M = [i for i in range(1,m+1)]
N = [i for i in range(1,n+1)]
A = {(i,j) for i in M for j in N}
W = 5000
weight = [1050, 1730, 2575, 3540, 1220, 1340, 1530, 1270]
weight_dict = {i:weight[i-1] for i in N}
# 定义模型种类, 这里是混合整数规划“MIP"
mdl = Model('MIP') #mdl是英文单词“model" 的缩写
# 定义变量,
y = mdl.binary_var_dict(M, name='y')
x = mdl.binary_var_dict(A, name='x')
#定义目标函数
mdl.minimize(mdl.sum(y[i] for i in M))
#定义约束条件
mdl.add_constraints(mdl.sum(x[i,j] for i in M)==1 for j in N)
#print(list(mdl.sum(x[i,j] for i in M)==1 for j in N))
mdl.add_constraints(mdl.sum(x[i,j]*weight_dict[j] for j in N) <= W*y[i] for i in M)
#求解模型并显示
solution = mdl.solve()
print(solution)