0

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

Meriem
  • 3
  • 4

1 Answers1

0

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)
AirSquid
  • 10,214
  • 2
  • 7
  • 31
  • thank you for your response, and for the standard for posting a request, I am new here so that helps a lot. as for your proposition, i tried it and i get another error, here it is : ValueError: Constraint 'limite[0]' does not have a proper value. Found 'True' Expecting a tuple or relational expression. Examples: sum(model.costs) == model.income (0, model.price[item], 50) – Meriem Feb 22 '23 at 17:16
  • There isn't really enough info to help with that, but if I had to guess, you have a zero in `routing[i,j]` so when you multiply by zero in the expression, there is no variable left. – AirSquid Feb 22 '23 at 17:32
  • i think you are right because i changed my matrix to a matrix with ones only and the model displayed some results (also not correct of course), but i will see what i can do for that, thank you for your help ! – Meriem Feb 22 '23 at 18:32
  • 1
    It is an easy fix. Are you familiar with `Constraint.Skip` ? – AirSquid Feb 22 '23 at 19:36
  • No, never seen it, i will look for how is it used – Meriem Feb 23 '23 at 17:29
  • Ok. It is one possible way to get around the "zero problem" there are others, depending on how your model is set up.... but that is a different question, and you could post that separately if you get stuck. If this answer satisfied your original issue, it is customary to close out the question by accepting the answer (checkmark near the answer). – AirSquid Feb 23 '23 at 17:35