Python 中的 PuLP 错误“/: Unsupported operand types for 'int' and 'LpVariable'” 因为我想使用我的 LpVariable 进行数学运算

问题描述 投票:0回答:1

我想用Python创建一个EOQ模型。我将决策变量 x 设置为 EOQ 值本身。但我使用 x 的这个值来确定我的目标函数(在数学运算中)。但我收到错误 unsupported operand type(s) for /: 'int' and 'LpVariable'。因此,我无法分配构成目标函数的 order_cost_term 和 Holding_cost_term 值。我该如何解决这个问题?

import math

import pulp

# Data

weekly_demand = 7500  # Haftalık talep miktarı (kg)

order_cost = 27  # Her siparişin maliyeti (kg)

holding_cost = 3  # Her bir birim stok başına yıllık maliyet

confidence_level = 0.95  # Güven düzeyi 

z_value = 1.96  # %95 güven düzeyi için Z değeri (standart normal dağılım)

# Creating a mathematical model

model = pulp.LpProblem("EOQ_Optimization", pulp.LpMinimize)

D = weekly_demand * 52  # Yıllık talep

# Decision variable

x = pulp.LpVariable("EOQ", lowBound=0, cat='Continuous')

# Constraint (Order quantity must meet weekly demand)

model += x >= D, "Sipariş_Talep_Karşıla"

# Auxiliary variables

order_cost_term = (D / x) * order_cost

holding_cost_term = (x / 2) * holding_cost

# Define the objective function

model += order_cost_term + holding_cost_term, "Toplam_Maliyet"

# Solve the model

model.solve()

# Get results

EOQ = x.varValue()

reorder_time = EOQ / weekly_demand

# Adjust reorder time based on confidence level

standart_deviation = math.sqrt(reorder_time)

reorder_time = standart_deviation * z_value

# Print results

print("Optimal Order Quantity (EOQ):", EOQ, "kg")

print("Optimal Reorder Time:", reorder_time, "week")

我想我特别是在除法运算中遇到了这个错误。所以我尝试乘以除法,但结果给出了相同的错误。我对第22行做了很多修改,例如,我改变了它的位置或者尝试将它作为模型添加到Lp中,但我无法解决我的问题。我想通过在具有整数和浮点值的数学运算中使用我的决策变量来确定我的目标函数而不会出现错误。

python optimization mathematical-optimization linear-programming pulp
1个回答
0
投票

有时,如果您将变量放入分母中,即使表达式仍然是线性的,

pulp
也会抱怨。您可以重新排列
order_cost_term
的方程式,但这对您没有帮助,因为您将这两个表达式添加在一起以形成以下形式:

f(x) = a/x + bx

就变量而言是非线性的

x

© www.soinside.com 2019 - 2024. All rights reserved.