Search code examples
indexingmodelpyomoconcrete

KeyError: "Index '(None, 0)' is not valid for indexed component 'sigma'"


I am working with pyomo and i am really new with it. I have been trying to solve a small problem but i keep getting this error, although i followed lots and lots of corrections and suggestions i found over here, but still not working, does any body know what this error can mean : KeyError: "Index '(None, 0)' is not valid for indexed component 'sigma'"? with sigma being my variable. I checked the available problems that look like mine but that didn't solve anything

I will put the code and the error bellow, THANK YOU

Bellow is my current code:

from pyomo.environ import *
import numpy as np
model = ConcreteModel()

def flatten(l):
    return [item for sublist in l for item in sublist]

matrix = numpy.array(matrixnw)

liste=flatten(myd.values())

def W_init(model, i, j):
    return matrix [i,j]
def W_init1D(model, i):
    return liste [i]

model.i = Set(initialize=range(0,3), doc='paths')
model.j = Set(initialize=range(0,15), doc='subflows')

model.capa = Param(model.i,  initialize=W_init1D)
model.routing = Param(model.i, model.j,  initialize=W_init)

model.sigma = Var(model.i, model.j, within= NonNegativeReals)


def limite(model, i):
    return sum(model.sigma[i,j]*model.routing[i,j]  for j in model.j) <= model.capa[i]
model.limite = Constraint(model.i, rule=limite)

image for the error:

2

I was expecting the constraint to be accepted by the model because i use the same way of writing (define a function then call it in the objective function) for the objective function and it does not show any error


Solution

  • First, in general, it is standard to cut and paste actual code, not an image. Ideally, your "error" should be reproducible by somebody who can help you by cutting/pasting your code into their IDE and running it.

    That said...

    You are using what I call a "rule-function combo" to make your constraints, but in the call to the function you are not providing the index I.

    Try this:

    model.limite = Constraint(model.i, rule=limite)