0

Parameter tying in Hidden Markov Models is essentially mapping multiple logical parameters of an HMM to a few physical parameters, in order to decrease computation and enforce constraints.

So if the parameters (states, GMMs, mean vectors, covariance matrices, transition matrices, stream weight vectors, duration parameter vectors...) are fields of an object which represents an HMM, these are to be mapped to a few physical copies. For example, any two states in the HMM set can have the same output probability distribution.

I need to have a syntactical representations of HMMs and parameter tying between them, and parse them and construct the HMM objects from them.

What would be a nice, elegant way of tying these parameters using features of the C++ language (if possible)?

EDIT

Raw pointers are definitely not an option. I'm looking into C++ pointer types like shared_ptr, but I'd like to avoid them too if possible. Also, it seems that C++ references may limit functionality.

Bruce
  • 945
  • 3
  • 12
  • 24

1 Answers1

0

You can use some kind of handle to parameter, i.e:

#include <cstdio>
#include <boost/math/distributions/uniform.hpp>

template< class T>
class Handle {
    T* rep_;
    int* pcount_;
    public:
        T* operator->() { return rep_;}
        Handle( T* rep) : rep_( rep), pcount_( new int(1)) {}
        Handle( const Handle& r) : rep_( r.rep_), pcount_( r.pcount_) {
            (*pcount_)++;
        }
        Handle& operator=( const Handle* r) {
            if ( rep_ == r->rep_) return *this;
            if ( --(*pcount_) == 0) {
                delete rep_;
                delete pcount_;
            }
            rep_ = r.rep_;
            pcount_ = r.pcount_;
            (*pcount_)++;
            return *this;
        }
        ~Handle() {
            if ( --(*pcount_) == 0) {
                delete rep_;
                delete pcount_;
                printf( "~Handle()");
            }
        }
};

Such handle can be freely passed around and they all can share single representation:

void f1( Handle<boost::math::uniform_distribution<int> > b) {
    Handle<boost::math::uniform_distribution<int> > a = b;
}

Handle<boost::math::uniform_distribution<int> > f2() {
    Handle<boost::math::uniform_distribution<int> > h( 
                                    new boost::math::uniform_distribution<int>);
    return h;
}

/*
 * 
 */
int main(int argc, char** argv) {

    Handle<boost::math::uniform_distribution<int> > h = f2();
    f1( h);
    Handle<boost::math::uniform_distribution<int> > k = h;
    return 0;
}

~Handle()

RUN SUCCESSFUL (total time: 62ms)

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Thanks for the answer! I added some points about pointers and references. Is it true that handles are better then either of them in this situation? If so, why? – Bruce Mar 03 '14 at 17:38
  • @Bruce it is strictly dependent on your needs, your skills and many more. Handles are safer for sure and easier to use, they free you from the burden of manual memory management and prevent memory leaks. Moreover you can add more features to Handle if needed: count usages, number of accesses at the moment, and so on – 4pie0 Mar 03 '14 at 17:47
  • @Bruce do you have additional questions? – 4pie0 Mar 04 '14 at 02:08
  • Thank you, yes - I still have to manipulate pointers if I have to work with the handle right? I was trying to completely avoid pointers and `new` if possible, as advised somewhere. I don't have broad knowledge of C++ so I'm a bit skeptical, should this introduce any problems as the size of the project grows. – Bruce Mar 04 '14 at 03:12
  • if you want to share object (in the meaning of sharing single memory address) between many others you have to use pointers or references to it. Pointers and references are great tool and no problem for programmer – 4pie0 Mar 04 '14 at 11:40