Skip to content

Bug using numpy float values in Pyomo expressions. #31

@whart222

Description

@whart222

Consider the following script:

# pyomo 4.4.1
# python 3.5
from pyomo.environ import *
import numpy

m = ConcreteModel()

m.T = RangeSet(10)
m.v = Var(initialize=1, bounds=(0,None))
m.c = Var(m.T, initialize=20)

def rule(m, t):
    h = numpy.float32(1.0)
    # This works
    return m.c[t]  == h * m.c[t]
m.X = Constraint(m.T, rule=rule)

def rule(m, t):
    h = numpy.float32(1.0)
    # This works
    return m.c[t]  == h * m.c[t] * m.v
m.X = Constraint(m.T, rule=rule)

def rule(m, t):
    h = numpy.float32(1.0)
    # This fails
    return m.c[t]  == h * m.v
m.x = Constraint(m.T, rule=rule)

The first two constraints are constructed fine, but the last one fails, generating the following error:

ERROR: Rule failed when generating expression for constraint x with index 1:
    KeyError: "Error accessing indexed component: Cannot treat the scalar component 'v' as an array"
ERROR: Constructing component 'x' from data=None failed:
    KeyError: "Error accessing indexed component: Cannot treat the scalar component 'v' as an array"
Traceback (most recent call last):
  File "tmp.py", line 26, in <module>
    m.x = Constraint(m.T, rule=rule)
  File "/Users/wehart/home/src/python35/src/pyomo/pyomo/core/base/block.py", line 483, in __setattr__
    self.add_component(name, val)
  File "/Users/wehart/home/src/python35/src/pyomo/pyomo/core/base/block.py", line 849, in add_component
    val.construct(data)
  File "/Users/wehart/home/src/python35/src/pyomo/pyomo/core/base/constraint.py", line 736, in construct
    ndx)
  File "/Users/wehart/home/src/python35/src/pyomo/pyomo/core/base/misc.py", line 59, in apply_indexed_rule
    return rule(model, index)
  File "tmp.py", line 25, in rule
    return m.c[t]  == h * m.v
  File "/Users/wehart/home/src/python35/src/pyomo/pyomo/core/base/indexed_component.py", line 499, in __getitem__
    raise KeyError(msg)
KeyError: "Error accessing indexed component: Cannot treat the scalar component 'v' as an array"

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions