如何解决 pyomo 中数字上下文错误中的非数字类型 python

How to solve non-numeric type in numeric context error in pyomo python

这是代码。我认为求解器可能是 glpk 而不是 gurobi。在它尝试解决问题之前我得到了错误。

from pyomo.environ import *
from pyomo.opt import SolverFactory, SolverStatus
PrintSolverOutput =  False ###
model  = ConcreteModel()
model.dual = Suffix(direction =Suffix.IMPORT)
model.X = Var(I,T, within = NonNegativeReals)
model.In = Var(I,T, within = NonNegativeReals)
model.S = Var(T, within = NonNegativeReals)
def Objetivo(model):
    return (sum(C[i]*model.X[i,t]*((1+tau[i])**(t-1))+Ch[i]*model.In[i,t] 
                for i in I for t in T)
            +sum(Cs*model.S[t]
            for t in T)
            )
model.Total_Cost = Objective(rule = Objetivo, sense= minimize)
def Balance(model,i,t):
    if t==1:
        return model.X[i,t]+I0[i]-model.In[i,t]==D[i,t]
    elif t>=2:
        return model.X[i,t]+model.IN[i,t-1]-model.In[i,t]==D[i,t]
def Horas(model, t):
    return sum(r[i]*model.X[i,t] for i in I) <= H+model.S[t]
def Limite(model,t):
    return model.S[T]<=LS
model.RBalance = Constraint(I,T,rule=Balance)
model.RHoras = Constraint(T,rule=Horas)
model.RLimiteoras = Constraint(T,rule=Limite)
opt = SolverFactory("gurobi")

这是数据

之前忘记放数据了

T = [1,2,3,4,5,6]
I = ['A','B']

D = {('A',1):300,
     ('A',2):400,
     ('A',3):500,
     ('A',4):600,
     ('A',5):800,
     ('A',6):700,
     ('B',1):700,
     ('B',2):600,
     ('B',3):500,
     ('B',4):400,
     ('B',5):300,
     ('B',6):400}
tau = {'A':0.02,'B':0.01}
r = {'A':5,'B':3}
H = 3520
LS = 800
C = {'A':150,'B':120}
Ch = {'A':8,'B':4}
Cs = 6
I0 = {'A':100,'B':250}

错误

该代码来自 youtube 教程,对他有效,但对我无效。为什么?

除了修正您代码中的两个拼写错误外,该模型还为我计算和解决了这个错误。进行这些修复,运行 再次修复,重新 post 带有行号的确切错误以及卡住时产生错误的确切代码...