SimpleVar object has keyerror " cannot treat the scalar component * as an array."

991 views
Skip to first unread message

Hongwei Jin

unread,
Jun 29, 2015, 9:24:45 PM6/29/15
to pyomo...@googlegroups.com
Hi,

I defined two variables x, y, and both of them are simplevar, which is not an indexedcomponent.
model.x = Var(within=Binary)
model.y = Var(within=NonNegativeReals)

and I added one constraint to 

def size_limit(model):
  return model.x >= model.y * CONSTANT

if condition=True:
  model.constraint_size_limit = Constraint(rule=size_limit)

And then I got an error with KeyError: "Error accessing indexed component: Cannot treat the scalar component 'y' as an array"

model.y is a binary variable, but it was treated as index variable?

How to debug this?

Has any opinions?

Thanks in advance.

Gabriel Hackebeil

unread,
Jun 29, 2015, 9:29:38 PM6/29/15
to pyomo...@googlegroups.com
I don’t see how the code you posted could produce that error. Also, "if condition=True:” is a syntax error in Python. Is there more to the code?

Gabe

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hongwei Jin

unread,
Jun 29, 2015, 10:00:45 PM6/29/15
to pyomo...@googlegroups.com
Hi Gabe,

`if condition=True` is a typo, sorry about that.

This is the exact code:
model.max_above_crl_on_peak_realized_demand = Var(within=NonNegativeReals)
model
.is_on_peak_max_above_crl = Var(within=Binary)
...
def limit2_max_above_crl_on_peak_realized_demand(model):
       
return model.max_above_crl_on_peak_realized_demand <= max_native_load_for_shifting_kw * model.is_on_peak_max_above_crl


   
if post_optimization_on_peak_enabled:
        model
.constraint_limit2_max_above_crl_on_peak_realized_demand = Constraint(rule=limit2_max_above_crl_on_peak_realized_demand)


max_native_load_for_shifting_kw is a float variable.
It shouldn't get the error related to indexcomponent, I don't know why.

Gabriel Hackebeil

unread,
Jun 29, 2015, 10:09:05 PM6/29/15
to pyomo...@googlegroups.com
Can you post the full stack trace from your error message. I’m still not seeing anything that could produce that error.

Gabe

On Jun 29, 2015, at 10:00 PM, Hongwei Jin <jinh...@gmail.com> wrote:

Hi Gabe,

`if condition=True` is a typo, sorry about that.

This is the exact code:
model.max_above_crl_on_peak_realized_demand = Var(within=NonNegativeReals)
model.is_on_peak_max_above_crl = Var(within=Binary)
...
def limit2_max_above_crl_on_peak_realized_demand(model):
        return model.max_above_crl_on_peak_realized_demand <=max_native_load_for_shifting_kw * model.is_on_peak_max_above_crl


    if post_optimization_on_peak_enabled:
        model.constraint_limit2_max_above_crl_on_peak_realized_demand =Constraint(rule=limit2_max_above_crl_on_peak_realized_demand)

Hongwei Jin

unread,
Jun 30, 2015, 1:09:39 AM6/30/15
to pyomo...@googlegroups.com
ERROR: Constructing component 'constraint_limit2_max_above_crl_on_peak_realized_demand' from data=None failed:
KeyError: "Error accessing indexed component: Cannot treat the scalar component 'is_on_peak_max_above_crl' as an array"
2015-06-29 22:07:48,753 ERROR -- Constructing component 'constraint_limit2_max_above_crl_on_peak_realized_demand' from data=None failed:
KeyError: "Error accessing indexed component: Cannot treat the scalar component 'is_on_peak_max_above_crl' as an array"

data=None?

How to debug this error?

Gabriel Hackebeil

unread,
Jun 30, 2015, 1:19:13 AM6/30/15
to pyomo...@googlegroups.com
What are you launching on the command line that produces this error? Usually traceback information includes the source line numbers where the exception originates from. If you are launching runph you may need to add “--traceback” in order to get the full stack trace I am asking for, if it is pyomo you may need to add -c. It also might help to just send me your model file.

Gabe

Hongwei Jin

unread,
Jun 30, 2015, 1:43:26 PM6/30/15
to pyomo...@googlegroups.com
I apply pyomo in a concretemodel with other integrated python scripts, so there is no command I can put in the script.?
I assigned values from pandas. Is there any  particular version of pandas (basically numpy) required? Currently I am using pyomo 4.0.9682 with pandas 0.16.2 and numpy 1.9.2.

Gabriel Hackebeil

unread,
Jun 30, 2015, 1:52:33 PM6/30/15
to pyomo...@googlegroups.com
Panda and Numpy are not required to use Pyomo, so this wouldn’t be an issue related to versions for those packages.

I need more error information. A full stack trace would look something like this:

$ python junk.py 
Traceback (most recent call last):
  File "junk.py", line 11, in <module>
    model.c = Constraint(model.s, model.S[1], rule=lambda m,i: m.x >= 1)
  File “~/pyomo/src/pyomo/pyomo/core/base/block.py", line 443, in __setattr__
    self.add_component(name, val)
  File “~/pyomo/src/pyomo/pyomo/core/base/block.py", line 681, in add_component
    self._add_temporary_set(val)
  File “~/pyomo/src/pyomo/pyomo/core/base/block.py", line 585, in _add_temporary_set
    if tset.name == "_unknown_":
AttributeError: '_IndexedSetData' object has no attribute 'name'

If you want to send you script that would also be helpful. I basically just need the file where the error is originating from, otherwise I can’t help you.

Gabe

jose santiago rodriguez

unread,
Jun 30, 2015, 2:30:15 PM6/30/15
to pyomo...@googlegroups.com
I have seen this error when someone tries to use the index of a set as an iteratable. I would check the rules of the model. It might be that you are creating a constraint with the following mistake

##### Incorrect #####
def c1_rule(m,j):
   for i in j: 
      return model.x[i]==4
#### Correct ######
def c1_rule(m,j)

model.c1 = Constraint(model.mySet, rule=c1_rule)  

jose santiago rodriguez

unread,
Jun 30, 2015, 2:33:26 PM6/30/15
to pyomo...@googlegroups.com
Sorry i hit enter before finishing

##### Incorrect #####
def c1_rule(m,j):
   for i in j: 
      return model.x[i]==4
#### Correct ######
def c1_rule(m,j):
    return model.x[j] == 4

model.c1 = Constraint(model.mySet, rule=c1_rule)  

I remember seeing something like and if im not wrong it was giving the error you are mentioning

May be that helps,
Santiago

Gabriel Hackebeil

unread,
Jun 30, 2015, 2:51:30 PM6/30/15
to pyomo...@googlegroups.com
I suspect “max_native_load_for_shifting_kw”  might be a numpy or panda array type that attempts to treat the pyomo types as arrays due to it’s own operator overloading. Go ahead and add the following print statements just before the return statement in that rule and report back with the output.

print model.max_above_crl_on_peak_realized_demand, type(model.max_above_crl_on_peak_realized_demand)
print model.is_on_peak_max_above_crl, type(model.is_on_peak_max_above_crl)
print max_native_load_for_shifting_kw, type(max_native_load_for_shifting_kw)
print type(native_load_for_shifting_kw)

Gabe

Hongwei Jin

unread,
Jun 30, 2015, 4:22:15 PM6/30/15
to pyomo...@googlegroups.com
Thanks to all you guys. The issue was caused because one SimpleVar multiple a numpy.float object (max_native_load_for_shifting_kw). And I forced convert that into a native float, which solved the problem.

Bravo!
Reply all
Reply to author
Forward
0 new messages