2
$\begingroup$

I just started to learn about Pyomo. In several tutorials it is stated that the command for creating a variable is just model.Variable = VAR(...). However, I get the error message when using the Var function:

NameError: name 'Var' is not defined

Why do I get this error? Here you see my full code (the problematic statement is the last line):

import pyomo.environ as pyo

model = pyo.ConcreteModel()

#Sets


#empty set
model.A = pyo.Set()

#initialized set with values
model.D = pyo.Set(initialize=['red', 'green', 'blue'])


#Define a function to create ta sequence of numbers
def createSequenceOfNumbers(startNumber, endNumber):
    return (i for i in range (startNumber, endNumber + 1))

model.C = pyo.Set(initialize=createSequenceOfNumbers(4, 9))
model.E = pyo.RangeSet(5,9)


#printing a set
model.C.pprint()
model.D.pprint()
model.E.pprint()


#Parameter

#define sets
model.A_P = pyo.RangeSet(1,3)
model.B_P = pyo.RangeSet(1,3)

#Values for the sets by using a dict-datatype.
valuesForParam = {}
valuesForParam [1,1] = 1
valuesForParam [1,3] = 2
valuesForParam [2,3] = 10

#define Parameters (2 dimensional)
model.P = pyo.Param(model.A_P, model.B_P, initialize=valuesForParam, default=0)

model.P.pprint()



#Variables


model.variable1 = Var(within=NonNegativeReals, bounds =(0,6), initialize=1.5)
$\endgroup$
4
  • $\begingroup$ Use pyo.Var instead of just Var $\endgroup$
    – Stradivari
    Jan 29 at 12:01
  • $\begingroup$ Thanks Stradivari for your comment. When I use your suggested code I get the following error message "NameError: name 'NonNegativeReals' is not defined" $\endgroup$
    – PeterBe
    Jan 29 at 12:40
  • $\begingroup$ Please prepend pyo when using its variables. pyo.NonNegativeReals $\endgroup$
    – Stradivari
    Jan 29 at 12:48
  • $\begingroup$ Thanks for your comment Stradivari. Now it is okay :-). However, I do not understand, why I always have to use pyo before the commands in this case while in all tutorials they do not use it? By the way: If you write your comments in an answer, I will accept and upvote it such that you get points for that. Anyways, thanks a lot for your good help. I really appreciate it. $\endgroup$
    – PeterBe
    Jan 29 at 13:14
5
$\begingroup$

You are just importing the pyomo.environ module while the tutorials probably use the from syntax.

These variables are inside pyomo.environ so you have 3 alternatives:

  1. Import them explicitly from pyomo.environ import Var, NonNegativeReals
  2. Import them using a wildcard from pyomo.environ import * (this is considered an antipattern)
  3. Import the module (with an alias in this case) import pyomo.environ as pyo and access them like pyo.VariableName

PS: this isn't really related to pyomo. For more information about the python import system please read: https://docs.python.org/3/reference/simple_stmts.html#import

$\endgroup$
5
  • $\begingroup$ Thanks for the answer Stradivari. What would you say is the most common way in Python for importing (which of your 3 suggested options)? $\endgroup$
    – PeterBe
    Feb 1 at 8:33
  • $\begingroup$ Using wildcard is frowned upon because you cant see what you are importing. I usually just follow the "convention" for each library, for pyomo I think the most common way is the third. $\endgroup$
    – Stradivari
    Feb 1 at 9:19
  • $\begingroup$ Thanks for the comment Stradivari. But when using the wildcard you are importing everything, right? This way you know what you are importing. It is just everything and thus you do not have to care about specific moduels. $\endgroup$
    – PeterBe
    Feb 1 at 9:35
  • $\begingroup$ If you search "python wildcard import" in google there should be multiple posts about why you should avoid them, I would say that for small (non production) code it isn't a issue $\endgroup$
    – Stradivari
    Feb 1 at 10:05
  • $\begingroup$ Okay, thanks for your answer. I really appreciate it. $\endgroup$
    – PeterBe
    Feb 1 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.