我如何解决多个时期EOQ的问题
大家好,我正在尝试解决多个时期的EOQ模型。我用buring_price定义了不媒体约束。因此,如果Q较高,那么价格将会改变,而我面临56号规则部分的错误。
import pyomo.environ as pyo
from pyomo.environ import *
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
# Time periods
model.T = pyo.RangeSet(4)
# Parameters during period t
model.D = {1: 15 , 2: 5 , 3: 20 , 4: 40} # D = Demand
model.C = {1: 20 , 2: 20 , 3: 20 , 4: 20} # C = Ordering Cost
model.H = {1: 2 , 2: 2 , 3: 2 , 4:2} # H = Holding Costs
# Variables during period t
model.B = pyo.Var( domain=pyo.NonNegativeReals) # B = buring price
model.Q = pyo.Var(model.T, domain=pyo.NonNegativeReals) # Q = Quantity
model.I = pyo.Var(model.T, domain=pyo.NonNegativeReals) # I = Inventory
I0 = 0 # I0 = Inventory in the first round
Q_List = [] # Q_List = Put the Q value in the list
# Simplifying parameters
T = model.T
Q = model.Q
I = model.I
D = model.D
C = model.C
H = model.H
B = model.B
# Define the inventory relationships
def inventory_Online (m,t) :
if t == m.T.first() :
return m.I[t] == (I0 + m.Q[t]) - m.D[t]
return model.I[t] == (m.I[t-1] + m.Q[t]) - m.D[t]
model.inventory = pyo.Constraint(model.T, rule = inventory_Online)
# Define the cost function
def Obj_rule(m,t) :
TIC = sum(m.C[t]*m.D[t]/m.Q[t] + m.H[t]*m.I[t] + m.H[t]*m.Q[t]/2 + m.B for t in model.T)
return TIC
model.Obj = pyo.Objective(rule=Obj_rule , sense=minimize)
# Define the buring_price
def buring_price (m,t):
for t in model.T:
if m.Q[t] > 0 and m.Q[t] >=10:
return m.B == m.Q[t]*10
elif m.Q[t] > 10 and m.Q[t] <= 100:
return m.B == m.Q[t]*9
else:
return m.B == m.Q[t]*8
model.discount = pyo.Constraint(rule= buring_price)
# Solve the problem
opt = SolverFactory('ipopt',executable='C:\\Ipopt\\bin\\ipopt.exe')
opt.solve(model)
# Print the results
model.pprint()
TIC_Optimal = pyo.value(Obj_rule(model,T))
print('-------------------------TIC Optimal----------------------------------')
print('TIC Optimal =',TIC_Optimal)
print('-------------------------I[t]----------------------------------')
for t in model.T:
print('Period: {0}, Prod. Amount: {1}'.format(t, pyo.value(model.I[t])))
print('-------------------------Q[t]----------------------------------')
for t in model.T:
print('Period: {0}, Prod. Amount: {1}'.format(t, pyo.value(model.Q[t])))
print('-------------------------Q[t] List----------------------------------')
for t in model.T:
Q_List.append(pyo.value(model.Q[t]))
print(Q_List)
我非常感谢帮助我。
错误
错误:规则在生成表达式以进行约束折扣的表达式失败 索引无:pyomoexception:无法将非恒定表达式转换为 布尔。此错误通常是由于布尔中使用表达式引起的 诸如if语句之类的上下文。例如, mx = var()如果mx&lt; = 0: ...
Hi guys I am trying to solve multi period EOQ model. I define the disocunt constraint with buring_price. So that if the Q was upper then a limit, the price would change and I faced this error form the rule section line 56.
import pyomo.environ as pyo
from pyomo.environ import *
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
# Time periods
model.T = pyo.RangeSet(4)
# Parameters during period t
model.D = {1: 15 , 2: 5 , 3: 20 , 4: 40} # D = Demand
model.C = {1: 20 , 2: 20 , 3: 20 , 4: 20} # C = Ordering Cost
model.H = {1: 2 , 2: 2 , 3: 2 , 4:2} # H = Holding Costs
# Variables during period t
model.B = pyo.Var( domain=pyo.NonNegativeReals) # B = buring price
model.Q = pyo.Var(model.T, domain=pyo.NonNegativeReals) # Q = Quantity
model.I = pyo.Var(model.T, domain=pyo.NonNegativeReals) # I = Inventory
I0 = 0 # I0 = Inventory in the first round
Q_List = [] # Q_List = Put the Q value in the list
# Simplifying parameters
T = model.T
Q = model.Q
I = model.I
D = model.D
C = model.C
H = model.H
B = model.B
# Define the inventory relationships
def inventory_Online (m,t) :
if t == m.T.first() :
return m.I[t] == (I0 + m.Q[t]) - m.D[t]
return model.I[t] == (m.I[t-1] + m.Q[t]) - m.D[t]
model.inventory = pyo.Constraint(model.T, rule = inventory_Online)
# Define the cost function
def Obj_rule(m,t) :
TIC = sum(m.C[t]*m.D[t]/m.Q[t] + m.H[t]*m.I[t] + m.H[t]*m.Q[t]/2 + m.B for t in model.T)
return TIC
model.Obj = pyo.Objective(rule=Obj_rule , sense=minimize)
# Define the buring_price
def buring_price (m,t):
for t in model.T:
if m.Q[t] > 0 and m.Q[t] >=10:
return m.B == m.Q[t]*10
elif m.Q[t] > 10 and m.Q[t] <= 100:
return m.B == m.Q[t]*9
else:
return m.B == m.Q[t]*8
model.discount = pyo.Constraint(rule= buring_price)
# Solve the problem
opt = SolverFactory('ipopt',executable='C:\\Ipopt\\bin\\ipopt.exe')
opt.solve(model)
# Print the results
model.pprint()
TIC_Optimal = pyo.value(Obj_rule(model,T))
print('-------------------------TIC Optimal----------------------------------')
print('TIC Optimal =',TIC_Optimal)
print('-------------------------I[t]----------------------------------')
for t in model.T:
print('Period: {0}, Prod. Amount: {1}'.format(t, pyo.value(model.I[t])))
print('-------------------------Q[t]----------------------------------')
for t in model.T:
print('Period: {0}, Prod. Amount: {1}'.format(t, pyo.value(model.Q[t])))
print('-------------------------Q[t] List----------------------------------')
for t in model.T:
Q_List.append(pyo.value(model.Q[t]))
print(Q_List)
I would be thankful to help me.
ERROR
ERROR: Rule failed when generating expression for Constraint discount with
index None: PyomoException: Cannot convert non-constant expression to
bool. This error is usually caused by using an expression in a boolean
context such as an if statement. For example,
m.x = Var() if m.x <= 0:
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论