示例#1
0
        /**
         * /// Given a node, select a random successor from the set of possible successor nodes
         *
         * /// @param node the node
         * /// @return a random successor node.
         */
        private GrammarNode SelectRandomSuccessor(GrammarNode node)
        {
            GrammarArc[] arcs = node.GetSuccessors();

            // select a transition arc with respect to the arc-probabilities (which are log and we don't have a logMath here
            // which makes the implementation a little bit messy
            if (arcs.Length > 1)
            {
                double[] linWeights    = new double[arcs.Length];
                double   linWeightsSum = 0;

                double EPS = 1E-10;

                for (int i = 0; i < linWeights.Length; i++)
                {
                    linWeights[i]  = (arcs[0].Probability + EPS) / (arcs[i].Probability + EPS);
                    linWeightsSum += linWeights[i];
                }

                for (int i = 0; i < linWeights.Length; i++)
                {
                    linWeights[i] /= linWeightsSum;
                }


                double selIndex = _randomizer.NextDouble();
                int    index    = 0;
                for (int i = 0; selIndex > EPS; i++)
                {
                    index     = i;
                    selIndex -= linWeights[i];
                }

                return(arcs[index].GrammarNode);
            }
            else
            {
                return(arcs[0].GrammarNode);
            }
        }
示例#2
0
 /**
  * Retrieves the end node associated with the given node
  *
  * @param node the node of interest
  * @return the ending node or null if no end node is available
  */
 private GrammarNode GetEndNode(GrammarNode node)
 {
     GrammarArc[] arcs = node.GetSuccessors();
     Debug.Assert(arcs != null && arcs.Length > 0);
     return(arcs[0].GrammarNode);
 }