8.6.5. Var

class Var

Variable in the model. The index of a variable increases from 0. Each time a variable is added to the model, the index is increased by 1.

Properties

index

The index position of the variable

Methods

getAttr()

Get the attribute value of a variable

sameAs()

Test whether the variable is the same as another variable

setAttr()

Set the attribute value of a variable

getAttr(attrname)

Get the attribute value of a variable

Parameters

attrname – Attribute name

example:

m = Model()
x = m.addVar()
print(x.varname)
print(x.getAttr(MDO.Attr.VarName))

Note

Attribute can also be read and written directly through object attributes, in this case, the attribute name is case-insensitive

sameAs(var)

Test whether the variable is the same as another variable.

Parameters

var – Another variable to be tested

Returns

A bool result

example:

m = Model()
x = m.addVar()
print(x.sameAs(m.getVars()[0]))
setAttr(attrname, attrvalue)

Set the attribute value of a variable.

Parameters
  • attrname – The name of the attribute.

  • attrvalue – The value of the attribute to be set.

example:

m = Model()
x = m.addVar()
x.ub = 1.0
x.setAttr(MDO.Attr.UB, 2.0)
print(x.ub == 2.0)

Note

Attribute can also be read and written directly through object attributes, in this case, the attribute name is case-insensitive