python - 类型错误 : unsupported operand type(s) for ** or pow(): 'MLinExpr' and 'int'

标签 python gurobi bin-packing

我正在尝试使用 Gurobi 解决复杂的装箱问题。

我有一个解决问题的模型,但我遇到了类型错误。

我的代码是:

from gurobipy import GRB, Model
import numpy as np


"""
Create data
"""
items = np.arange(50)
radii = 0.2 * np.ones_like(items)
areas = np.pi * radii ** 2                                          # Areas of items in m2
min_dist = np.add.outer(radii, radii)

pallet_length = 1.2                                                 # x-dimension of pallet in m
pallet_width = 0.8                                                  # y-dimension of pallet in m
pallet_area = pallet_length * pallet_width                          # pallet area in m2


def master(patterns, vtype):
    m = Model()
    x = m.addMVar(patterns.shape[1], lb=0, obj=1, vtype=vtype)
    contraints = m.addConstr(patterns @ x >= 1)

    m.optimize()

    if vtype == GRB.CONTINUOUS:
        return m.objVal, contraints.pi
    else:
        return m.objVal, x.X


def sub(duals):
    m = Model()
    m.setParam("NonConvex", 2)

    x = m.addMVar((len(items)), lb=0, obj=-duals, vtype=GRB.BINARY, name="x")
    cx = m.addMVar((len(items)), lb=radii, ub=pallet_length - radii, name="cx")
    cy = m.addMVar((len(items)), lb=radii, ub=pallet_width - radii, name="cy")

    # Pallet size constraint, the area of packed stacks cannot exceed the pallet area.
    m.addConstr(areas @ x <= pallet_area, name=f"area")

    for i in range(len(radii) - 1):
        for j in range(i + 1, len(radii)):
            lhs = (cx[i] - cx[j])** 2 + (cy[i] - cy[j]) ** 2
            rhs = (x[i] + x[j] - 1) * min_dist[i, j] ** 2
            m.addConstr(lhs >= rhs, name=f"overlap[{i},{j}]")

    def callback(model, where):
        if where == GRB.Callback.MIPNODE:
            if model.cbGet(GRB.Callback.MIPNODE_OBJBST) < -1:
                model.terminate()

    m.optimize(callback)

    return x.X if m.objVal < -1 else None


# Initial pattern: put every item on its own pallet.
patterns = np.eye(len(items))
num_pallets, duals = master(patterns, GRB.CONTINUOUS)

while (new_pattern := sub(duals)) is not None:
    print("Current number of pallets needed (LP):", num_pallets)

    # Check if we already have this pattern. If so, we are done as well.
    if any(np.allclose(new_pattern, column) for column in patterns.T):
        break

    patterns = np.column_stack((patterns, new_pattern))
    num_pallets, duals = master(patterns, GRB.CONTINUOUS)

min_pallets, used_patterns = master(patterns, GRB.BINARY)
unused = set(items)

print("Number of pallets needed (IP):", min_pallets)

for idx, used in enumerate(used_patterns):
    if used:
        on_pallet = items[patterns[:, idx].astype(bool)]
        on_pallet = {item for item in on_pallet if item in unused}
        unused -= on_pallet
        print(f"Put items {on_pallet} on same pallet.")

我得到的错误是

lhs = (cx[i] - cx[j])** 2 + (cy[i] - cy[j]) ** 2
          ~~~~~~~~~~~~~~^^~~~
TypeError: unsupported operand type(s) for ** or pow(): 'MLinExpr' and 'int'

所以我知道不可能对两个变量进行减法并获取它们的幂。我应该如何解决这个问题或改变我的模型以便我能够进行比较?

我使用的是 Gurobi v.10.0.1

最佳答案

而不是

lhs = (cx[i] - cx[j])** 2 + (cy[i] - cy[j]) ** 2

尝试:

lhs = (cx[i] - cx[j])*(cx[i] - cx[j]) + (cy[i] - cy[j])*(cy[i] - cy[j])

(请参阅以下示例: https://www.gurobi.com/documentation/10.0/refman/py_qex.html )

关于python - 类型错误 : unsupported operand type(s) for ** or pow(): 'MLinExpr' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76089678/

相关文章:

python - 当一个类别子集缺少数据时 Seaborn 对图

python - 如何从 python 中的 .wav 文件创建频谱图以解决音频分类问题

python - 根据特定列或列中是否存在空值从 DataFrame 中选择行

python - 将 Pyomo 导出到 .mps 文件时保留变量名称

java - Gurobi 无法设置或打印(手动)变量值

java - 写入文件: Constraints and Variables of Gurobi GRBModel in Java

在空间中拟合对象的算法

在另一个矩形内均匀分布矩形所需的算法

algorithm - 考虑上次更新,对动态集的部分进行装箱

python - 接受函数调用后立即创建新客户端