Пример #1
0
        public int CompareTo(BeamNode <T> other)
        {
            // This method is important for comparing and ranking nodes in terms of their relative weights.
            var result = this.Weight.CompareTo(other.Weight);

            return(result);
        }
Пример #2
0
        // Construction

        internal BeamNode(BeamSearch <T> beam, double weight)
        {
            PreviousNode = null;
            Weight       = weight;
            Item         = default(T);
            _beam        = beam;
            Depth        = 0;
        }
Пример #3
0
        // Construction


        /// <summary>Creates a new <c>BeanSearch</c> structure.</summary>
        /// <param name="rootWeight">
        /// If you are using logarithmic numbers, set the value to 0. If you are using percentages, set this value to 1.
        /// </param>
        public BeamSearch(double rootWeight)
        {
            _root = new BeamNode <T>(this, rootWeight);
            Level.Add(new List <BeamNode <T> >()
            {
                _root
            });
        }
Пример #4
0
        // Methods

        /// <summary>Adds the specified <c>item</c> as the next node in the <c>BeamSearch</c> structure.</summary>
        /// <param name="weight">
        /// This is the weight of this node, which will determine which nodes are given priority by the <c>BeamSearch</c> structure.
        /// </param>
        public void AddNextNode(T item, double weight)
        {
            var newNode = new BeamNode <T>(this, item, weight);

            if (newNode.Depth >= _beam.Level.Count)
            {
                Debug.Assert(_beam.Level.Count == newNode.Depth);
                _beam.Level.Add(new List <BeamNode <T> >());
            }
            _beam.Level[newNode.Depth].Add(newNode);
        }
Пример #5
0
        /// <summary><para>Creates a new BeamNode instance.
        /// </para><para>Next, call parentNode.AddChild(...) to add this node to the appropriate level of the Beam.
        /// </para></summary>
        /// <param name="parent"></param>
        /// <param name="item"></param>
        /// <param name="weight"></param>
        public BeamNode(BeamNode <T> parent, T item, double weight)
        {
            Debug.Assert(parent != null);
            Debug.Assert(item != null);

            PreviousNode = parent;
            Item         = item;
            Weight       = weight;
            _beam        = parent._beam;
            Depth        = parent.Depth + 1;
        }
Пример #6
0
        // Methods

        /// <summary>Returns the best sequence of items stored in the Beam.</summary>
        public T[] GetBestSequence()
        {
            T[]          results      = new T[Level.Count - 1];
            var          bestSequence = SearchHelper.GetMaxNItems(1, Level[Level.Count - 1]);
            BeamNode <T> currentLevel = Level[Level.Count - 1][bestSequence[0]];

            while (currentLevel.Depth > 0)
            {
                results[currentLevel.Depth - 1] = currentLevel.Item;
                currentLevel = currentLevel.PreviousNode;
            }
            return(results);
        }