Use of struct_MX (Problem in MX vecNZcat structure cat: missing expressions)

123 views
Skip to first unread message

Boris Belousov

unread,
Aug 18, 2015, 2:35:23 AM8/18/15
to CasADi
Good morning!

I have a question concerning the use of structs holding symbolic matrix expressions.

I want to define the discrete dynamics of the 2D-mean and 2x2-covariance matrix. I would like to use the handy power indexing. Unfortunately the following piece of code raises an exception. I am using CasADi 2.3.0 from Python.

# True state
state = cat.struct(['x','y'])

# Control
control = cat.struct_symMX([
        cat.entry('u',struct=state)
    ])

# Belief state (mu, Sigma)
belief = cat.struct_symMX([
        cat.entry('m',struct=state),
        cat.entry('S',shapestruct=(state,state))
    ])

# Discrete-time dynamics
drhs = cat.struct_MX(belief)
drhs['m'] = belief['m'] + control['u']
drhs['m']

If MX is replaced with SX, the code works. However, I am planning to do heavy matrix manipulations, so I thought it would be more efficient to define covariance matrix as MX.

The output of the IPython console is the following:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-272-d19def9de713> in <module>()
     16 drhs = cat.struct_MX(belief)
     17 drhs['m'] = belief['m'] + control['u']
---> 18 drhs['m']

/Users/b4be1/local/casadi/tools/structure.pyc in proper(self, mbt, *args)
    107     if not isinstance(mbt,tuple):
    108       mbt = (mbt,)
--> 109     return f(self,mbt,*args)
    110   return proper
    111 

/Users/b4be1/local/casadi/tools/structure.pyc in __getitem__(self, powerIndex)
    629   @properGetitem
    630   def __getitem__(self,powerIndex):
--> 631     return self.struct.traverseByPowerIndex(powerIndex,dispatcher=GetterDispatcher(struct=self.struct,master=self.master,priority_object_map=self.priority_object_map))
    632 
    633 class MasterSettable:

/Users/b4be1/local/casadi/tools/structure.pyc in master(self)
   1101       missing = filter(lambda k: self.storage[self.mapping[k]] is None,self.mapping.keys())
   1102 
-> 1103       raise Exception("Problem in MX vecNZcat structure cat: missing expressions. The following entries are missing: %s" % str(missing))
   1104 
   1105     if self.dirty:

Exception: Problem in MX vecNZcat structure cat: missing expressions. The following entries are missing: [('S',)]

Warm regards,
Boris

Joris Gillis

unread,
Aug 18, 2015, 2:46:41 AM8/18/15
to CasADi
Dear Boris,

This error seems legit, although it does not make sense that it should work for SX.
The statement "drhs['m'] = belief['m'] + control['u']"  sets a field of drhs,
The statement "drhs['m']" gets a field.

However, the action of 'getting' is prohibited until all fields of drhs are set.
Indeed if you do the following, it will not raise the error:


drhs = cat.struct_MX(belief)
drhs['m'] = belief['m'] + control['u']
drhs['S'] = mul(belief['m'],belief['m'].T) # replace with what you actually want
drhs['m']

This is actually a built-in precaution, as omitting fields is more likely to be a bug than actual desired behaviour.

Best regards,
    Joris
Reply all
Reply to author
Forward
0 new messages