Ejemplo n.º 1
0
        /// <summary>
        /// Register a sequence of actions.
        /// </summary>
        /// <param name="actions">
        /// The actions list, which should be at least of size N.
        /// </param>
        public void RegisterSequence(IReadOnlyList <T> actions)
        {
            // Can only register sequence if its size N
            if (actions.Count == NValue)
            {
                // Previous actions
                ReadOnlyListSegment <T> prevActions =
                    new ReadOnlyListSegment <T>(actions, 0, NValue - 1);

                // Previous actions in key form
                string prevActionsKey = SequenceToString(prevActions);

                // Action performed
                T actionPerformed = actions[NValue - 1];

                // Check if our data contains the key (i.e. sequence of actions)
                // If not, create a new record for this sequence of actions
                if (!data.ContainsKey(prevActionsKey))
                {
                    data[prevActionsKey] = new ActionFrequency <T>();
                }

                // Get the data record for this sequence of actions
                ActionFrequency <T> actionFrequency = data[prevActionsKey];

                // Increment the number of times this action was performed
                // after the given sequence of actions
                actionFrequency.IncrementFrequency(actionPerformed);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Register a sequence of actions.
        /// </summary>
        /// <param name="actions">
        /// The actions list, which should be at least of size 1.
        /// </param>
        public void RegisterSequence(IReadOnlyList <T> actions)
        {
            // Register the sequence of actions in each Ni-Gram
            for (int i = 0; i < NValue; i++)
            {
                // Are there enough actions for the current Ni-Gram?
                if (actions.Count >= i + 1)
                {
                    ReadOnlyListSegment <T> subactions =
                        new ReadOnlyListSegment <T>(
                            actions,
                            actions.Count - i - 1,
                            i + 1);

                    // Register the sequence of actions in the current Ni-Gram
                    predictors[i].RegisterSequence(subactions);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the most likely action given a sequence of actions.
        /// </summary>
        /// <param name="actions">
        /// The actions list, which should be at least of size 1.
        /// </param>
        /// <returns>
        /// The most likely action for the given a sequence of actions.
        /// </returns>
        public T GetMostLikely(IReadOnlyList <T> actions)
        {
            // Declare variable for best action and set it to its default value
            T bestAction = default;

            // Go through the various Ni-Grams
            for (int i = 0; i < NValue; i++)
            {
                // Are there enough actions for the current Ni-Gram?
                if (actions.Count >= NValue - i - 1)
                {
                    // Get current Ni-Gram
                    NGram <T> p = predictors[NValue - i - 1];

                    // Create a view containing only the actions for the
                    // current Ni-Gram
                    ReadOnlyListSegment <T> subactions =
                        new ReadOnlyListSegment <T>(
                            actions,
                            actions.Count + i + 1 - NValue,
                            NValue - i - 1);

                    // Get frequency of action sequence in the current Ni-Gram
                    int numActions = p.GetActionsFrequency(subactions);

                    // Is that frequency larger than the threshold?
                    if (numActions > threshold)
                    {
                        // Then use this action
                        bestAction = p.GetMostLikely(subactions);
                        break;
                    }
                }
            }

            // Return the best action
            return(bestAction);
        }
    public IReadOnlyList <TValue> this[TKey key]
    {
        get
        {
            IReadOnlyList <TValue> readOnlyList;
            var index = IndexOf(key);
            if (index >= 0)
            {
                var currentGroupIndex = _groups[index];
                var nextGroupIndex    = index < _groups.Count - 1 ? _groups[index + 1] : _values.Count;
                var count             = nextGroupIndex - currentGroupIndex;

                readOnlyList = new ReadOnlyListSegment <TValue>(_values, currentGroupIndex, count);
            }
            else
            {
                readOnlyList = EmptyReadOnlyCollection <TValue> .Value;
            }

            Assert.IsNotNull(readOnlyList);
            return(readOnlyList);
        }
    }
Ejemplo n.º 5
0
 public bool Equals(ReadOnlyListSegment <T> obj) => obj.List == List && obj.Offset == Offset && obj.Count == Count;