Search code examples
pythonspydermathematical-optimizationpyomo

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


This is the code. I think the solver could be glpk instead of gurobi. I got the error before it tries to solve the problem.

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")

This is the data

I forgot to put the data before.

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}

error

enter image description here enter image description here

The code is from a youtube tutorial and it worked for him but not for me. Why?


Solution

  • Aside from fixing two typos in your code, this model computes and solves for me without that error. Make these fixes, run it again, re-post the exact error with line number and the exact code that produces the error if stuck...

    enter image description here