Пример #1
0
 internal GestureRecognizer(HMMModel model)
 {
     hmm = new DiscreteHMM <int, int>(model, symbols, states);
 }
Пример #2
0
        public DiscreteHMM(HMMModel model, T[] symbolsList, U[] statesList)
        {
            #region Check the symbols list validity
            if (symbolsList != null)
            {
                for (int i = 0; i < symbolsList.Length; i++)
                {
                    if (symbolsList[i] == null)
                    {
                        throw new ArgumentException("Null symbols are not supported in the symbols list.");
                    }
                    //TODO: Check if all the symbols are unique
                }
            }
            else
            {
                throw new ArgumentNullException("symbolsList");
            }
            #endregion

            #region Check the states list validity
            if (statesList != null)
            {
                for (int i = 0; i < statesList.Length; i++)
                {
                    if (statesList[i] == null)
                    {
                        throw new ArgumentException("Null states are not supported in the states list.");
                    }
                    //TODO: Check if all the states are unique
                }
            }
            else
            {
                throw new ArgumentNullException("statesList");
            }
            #endregion

            symbols = symbolsList;
            states  = statesList;

            #region Create the probability matrices
            stateTransitionProbabilities = new double[statesList.Length, statesList.Length];
            initialStateDistribution     = new double[statesList.Length];
            symbolDistributionDiscrete   = new double[statesList.Length, symbolsList.Length];
            double evenSymProb = 1.0 / (double)symbolsList.Length;

            if (model == HMMModel.Ergodic)
            {
                double evenStateProb = 1.0 / (double)statesList.Length;

                //Setup the initial state distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    initialStateDistribution[i] = evenStateProb;
                }

                //Setup the state transition probabilities
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < statesList.Length; j++)
                    {
                        stateTransitionProbabilities[i, j] = evenStateProb;
                    }
                }

                //Setup the symbol distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < symbolsList.Length; j++)
                    {
                        symbolDistributionDiscrete[i, j] = evenSymProb;
                    }
                }
            }
            else if (model == HMMModel.LeftToRight)
            {
                //Setup the initial state distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    if (i == 0)
                    {
                        //initialStateDistribution[i] = 1.0;
                        initialStateDistribution[i] = 0.9;
                    }
                    else
                    {
                        //initialStateDistribution[i] = 0;
                        initialStateDistribution[i] = 0.1 / (statesList.Length - 1);
                    }
                }

                //Setup the state transition probabilities
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < statesList.Length; j++)
                    {
                        if (j == i || j == i + 1)
                        {
                            stateTransitionProbabilities[i, j] = 0.5;
                        }
                        else
                        {
                            stateTransitionProbabilities[i, j] = 0.0;
                        }
                    }
                }

                //Setup the symbol distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < symbolsList.Length; j++)
                    {
                        symbolDistributionDiscrete[i, j] = evenSymProb;
                    }
                }
            }
            else if (model == HMMModel.LeftToRightAll)
            {
                //Setup the initial state distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    if (i == 0)
                    {
                        //initialStateDistribution[i] = 1.0;
                        initialStateDistribution[i] = 0.9;
                    }
                    else
                    {
                        //initialStateDistribution[i] = 0;
                        initialStateDistribution[i] = 0.1 / (statesList.Length - 1);
                    }
                }

                //Setup the state transition probabilities
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < statesList.Length; j++)
                    {
                        if (j >= i)
                        {
                            stateTransitionProbabilities[i, j] = 1.0 / (double)(statesList.Length - i);
                        }
                        else
                        {
                            stateTransitionProbabilities[i, j] = 0.0;
                        }
                    }
                }

                //Setup the symbol distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < symbolsList.Length; j++)
                    {
                        symbolDistributionDiscrete[i, j] = evenSymProb;
                    }
                }
            }
            else if (model == HMMModel.LeftToRight2)
            {
                //Setup the initial state distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    if (i == 0)
                    {
                        //initialStateDistribution[i] = 1.0;
                        initialStateDistribution[i] = 0.9;
                    }
                    else
                    {
                        //initialStateDistribution[i] = 0;
                        initialStateDistribution[i] = 0.1 / (statesList.Length - 1);
                    }
                }

                //Setup the state transition probabilities
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < statesList.Length; j++)
                    {
                        if (j == i || j == i + 1 || j == i + 2)
                        {
                            stateTransitionProbabilities[i, j] = 1.0 / 3.0;
                        }
                        else
                        {
                            stateTransitionProbabilities[i, j] = 0.0;
                        }
                    }
                }

                //Setup the symbol distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < symbolsList.Length; j++)
                    {
                        symbolDistributionDiscrete[i, j] = evenSymProb;
                    }
                }
            }
            else if (model == HMMModel.LeftToRight2Loop)
            {
                //Setup the initial state distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    if (i == 0)
                    {
                        //initialStateDistribution[i] = 1.0;
                        initialStateDistribution[i] = 0.9;
                    }
                    else
                    {
                        //initialStateDistribution[i] = 0;
                        initialStateDistribution[i] = 0.1 / (statesList.Length - 1);
                    }
                }

                //Setup the state transition probabilities
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < statesList.Length; j++)
                    {
                        if (j == i || j == i + 1 || j == i + 2)
                        {
                            stateTransitionProbabilities[i, j] = 1 / 3.0;
                        }
                        //else if (i == statesList.Length - 2 && j == 0)
                        //{
                        //    stateTransitionProbabilities[i, j] = 1 / 3.0;
                        //}
                        else if (i == statesList.Length - 1 && (j == 0 /*|| j == 1*/))
                        {
                            stateTransitionProbabilities[i, j] = 1 / 3.0;
                        }
                        else
                        {
                            stateTransitionProbabilities[i, j] = 0.0;
                        }
                    }
                }

                //Setup the symbol distribution
                for (int i = 0; i < statesList.Length; i++)
                {
                    for (int j = 0; j < symbolsList.Length; j++)
                    {
                        symbolDistributionDiscrete[i, j] = evenSymProb;
                    }
                }
            }
            #endregion
        }