"object has no attribute 'ctype'

121 views
Skip to first unread message

Jeffrey Kantor

unread,
Apr 27, 2022, 3:56:36 PM4/27/22
to pyomo...@googlegroups.com
All …

I’m continuing to work with pyomo.kernel, and have come across the following error message. Do I need to somehow specify a data type for the summation expression?

Jeff


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [79], in <cell line: 26>()
     22         m.d[j] = m.z[j] == sum([m.a[i] for i in range(n)])
     24     return m
---> 26 svm_conic(X_train, y_train, 1)

Input In [79], in svm_conic(X, y, c)
     20 m.d = pmo.constraint_dict()
     21 for j in range(p + 1):
---> 22     m.d[j] = m.z[j] == sum([m.a[i] for i in range(n)])
     24 return m

File ~/opt/anaconda3/lib/python3.9/site-packages/pyomo/core/kernel/dict_container.py:78, in DictContainer.__setitem__(self, key, item)
     77 def __setitem__(self, key, item):
---> 78     if item.ctype is self.ctype:
     79         if item._parent is None:
     80             if key in self._data:

AttributeError: 'EqualityExpression' object has no attribute 'ctype'




import pyomo.kernel as pmo

def svm_conic(X, y, c):
    
    n, p = X.shape
    
    F = np.append(np.ones((n, 1)), X.to_numpy(), axis=1)

    m = pmo.block()
    
    m.r = pmo.variable()
    m.a = pmo.variable_dict()
    for i in range(n):
        m.a[i] = pmo.variable(domain_type=pmo.RealSet)
        
    m.z = pmo.variable_dict()
    for j in range(p + 1):
        m.z[j] = pmo.variable(domain_type=pmo.RealSet)
        
    m.d = pmo.constraint_dict()
    for j in range(p + 1):
        m.d[j] = m.z[j] == sum([m.a[i] for i in range(n)])

    return m

svm_conic(X_train, y_train, 1)

Gabriel Hackebeil

unread,
Apr 27, 2022, 4:27:22 PM4/27/22
to pyomo...@googlegroups.com
With kernel, assignments to the containers have to be done with explicit objects of the container type. In your case, you need to assign a ‘kernel.constraint(…)’ to m.d[j]. You just pass the equality expression into the constraint object as the sole argument, but there are more efficient ways to create it by using body and rhs keywords for constraint.

Gabe

On Apr 27, 2022, at 3:56 PM, Jeffrey Kantor <jeff....@gmail.com> wrote:

All …
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/822CFFBA-CEBA-4D3F-8967-7938BCAC0244%40gmail.com.

Gabriel Hackebeil

unread,
Apr 27, 2022, 4:33:02 PM4/27/22
to pyomo...@googlegroups.com
Adding to this… requiring explicit assignments makes it a bit easier to support other nice things. For instance you can assign `kernel.linear_constraint` objects, which make building the linear constraints of a model much faster. You can also assign a constraint container (e.g., to have a nested container structure) and other constraint subclasses.

Gabe

Jeffrey Kantor

unread,
Apr 27, 2022, 6:24:24 PM4/27/22
to pyomo...@googlegroups.com
Gabe,

Thanks, that’s perfectly clear and, in retrospect, makes sense.

Diving into this deeper, I’d like to use kernel.matrix_constraint to build a constraint  y = A x where x and y are both variable lists, and A is a matrix.  Perhaps I could do something like [A -I] * [x y] = 0, but I’m not understanding how to use the container objects to make this simple.  Any advice?

Jeff



Gabriel Hackebeil

unread,
Apr 27, 2022, 7:38:58 PM4/27/22
to pyomo...@googlegroups.com
Here’s an example, Jeff. You can just treat it like any another kernel.constraint() and put it on a block or in a constraint container.

```
import pyomo.kernel as pmo
import numpy as np

m = pmo.block()
m.x = pmo.variable()
m.y = pmo.variable()

m.c = pmo.constraint_list()
for i in range(3):
    m.c.append(
        # this can be assigned to a block or put in a constraint container                                                      
        # it is a constraint_tuple                                                                                              
        pmo.matrix_constraint(
            np.random.rand(3, 2), # a sparse or dense matrix                                                                    
            x=[m.x, m.y], # also fine to use a variable_list()                                                                  
            lb=0, # a number or array                                                                                           
            ub=1, # a number or array                                                                                           
            #rhs=..., # same, but only use for equality constraint                                                              
            sparse=False, # set this if do not want to use scipy.sparse storage                                                 
        )
    )
```

Gabe

Gabriel Hackebeil

unread,
Apr 27, 2022, 7:42:19 PM4/27/22
to Pyomo Forum
For your example, you will need to compose the final Ax = b matrix / rhs forms, but you can likely do that using scipy block-/sparse-matrix functionality without too much pain.

Gabe
Reply all
Reply to author
Forward
0 new messages