Search code examples
pythonoptimizationwarningspyomo

Why does PyCharm give me a warning when using Pyomo's .fix() method?


I use Pyomo to model optimization problems and have an indexed decision variable where I have to fix a variable with a certain index to a value. In a minimal example, it looks like this:

import pyomo.environ as pyo

model = pyo.ConcreteModel()
model.s = pyo.Set(initialize=[0, 1, 2])
model.v = pyo.Var(model.s)
model.v[0].fix(0.0)

However, I get a warning from PyCharm when I use .fix() that says: "Unresolved attribute reference 'fix' for class 'ComponentData'".

However, Pyomo itself suggests exactly this way to set variables to a fixed value: https://pyomo.readthedocs.io/en/6.8.0/working_models.html#fixing-variables-and-re-solving. In this example we are dealing with instances of AbstractModels, but with ConcreteModels this should make no difference, I think. The same is repeated below with a ConcreteModel.

Can anyone tell me if this is the right way to set variables to a fixed value or why I get the warning even though Pyomo itself uses this way?

I use Pyomo 6.8.2 and the PyCharm Community Edition 2024.2.0.1.


Solution

  • You are using it correctly.

    There are a handful of false warnings like this that you may bump into in pyomo. My suspicion is large chunks of the pyomo codebase were structured / written before the linters and type-checkers got so good and they are still catching up a bit.

    My general approach when this happens is to check the dox to ensure I'm using it correctly and maybe do a small test. If the warning bugs you, you can use Pycharm's actions thing to ignore it.