public string GetMostLikely(string[] actions)
    {
        string stringOfArray = string.Join("", actions);

        if (data[stringOfArray] == null)
        {
            return(string.Empty);
        }

        KeyDataRecord keyData = (KeyDataRecord)data[stringOfArray];

        int    highestValue = 0;
        string bestAction   = string.Empty;

        ICollection possibleActions = keyData.counts.Keys;

        foreach (string action in possibleActions)
        {
            if ((int)keyData.counts[action] > highestValue)
            {
                highestValue = (int)keyData.counts[action];
                bestAction   = action;
            }
        }
        return(bestAction);
    }
    public void RegisterSequence(T action)
    {
        string key = ArrToStrKey(ref action);
        T      val = action;

        if (!data.ContainsKey(key))
        {
            data[key] = new KeyDataRecord <T>();
        }

        KeyDataRecord <T> kdr = data[key];

        if (!kdr.counts.ContainsKey(val))
        {
            kdr.counts[val] = 1;
        }
        else
        {
            kdr.counts[val]++;
        }


        //if (kdr.counts.ContainsKey(val))
        //{
        //    kdr.counts[val] = 0;
        //}

        //kdr.counts[val]++;
        kdr.total++;
    }
Beispiel #3
0
    //computes the prediction of the best action to take
    public T GetMostLikely(T[] actions)
    {
        string            key = ArrayToStringKey(ref actions);
        KeyDataRecord <T> kdr = data[key];
        int highestVal        = 0;
        T   bestAction        = default(T);

        foreach (KeyValuePair <T, int> kvp in kdr.counts)
        {
            if (kvp.Value > highestVal)
            {
                bestAction = kvp.Key;
                highestVal = kvp.Value;
            }
        }
        return(bestAction);
    }
Beispiel #4
0
    //registers a set of sequences
    public void RegisterSequence(T[] actions)
    {
        string key = ArrayToStringKey(ref actions);
        T      val = actions[nValue - 1];

        if (!data.ContainsKey(key))
        {
            data[key] = new KeyDataRecord <T>();
        }
        KeyDataRecord <T> kdr = data[key];

        if (kdr.counts.ContainsKey(val))
        {
            kdr.counts[val] = 0;
        }
        kdr.counts[val]++;
        kdr.total++;
    }
Beispiel #5
0
    public string GetMostLikely(string[] actions)
    {
        for (int i = 0; i < nValue - 1; i++)
        {
            NGramPredictor nGram = ngrams[nValue - i - 1];

            //get substring
            string[] subActions = new string[nValue - i - 1];

            int startIndex = (actions.Length - 1) - (subActions.Length - 1);

            for (int j = 0; j < subActions.Length; j++)
            {
                subActions[j] = actions[startIndex];
                startIndex++;
            }

            string stringOfArray = string.Join("", subActions);

            if (stringOfArray == "")
            {
                return(String.Empty);
            }

            if (subActions.Length == 1)
            {
                stringOfArray = subActions[0];
            }

            //check if there are enough entries

            if (nGram.data.ContainsKey(stringOfArray))
            {
                KeyDataRecord data = (KeyDataRecord)nGram.data[stringOfArray];

                if (data.total > threshold)
                {
                    return(nGram.GetMostLikely(subActions));
                }
            }
        }
        return(string.Empty);
    }
    public void RegisterSequence(string[] actions)
    {
        //split the sequence
        string[] prevActions = new string[nValue - 1];

        for (int i = 0; i < nValue - 1; i++)
        {
            prevActions[i] = actions[i];
        }

        string currentAction = actions[nValue - 1];

        string stringOfArray = string.Join("", prevActions);

        if (nValue == 1)
        {
            stringOfArray = currentAction;
        }


        if (!data.ContainsKey(stringOfArray))
        {
            data[stringOfArray] = new KeyDataRecord();
        }

        KeyDataRecord keyData = (KeyDataRecord)data[stringOfArray];

        if (keyData.counts == null)
        {
            keyData.counts = new Hashtable();
        }

        if (!keyData.counts.ContainsKey(currentAction))
        {
            keyData.counts[currentAction] = 0;
        }

        keyData.counts[currentAction] = (int)keyData.counts[currentAction] + 1;
        keyData.total += 1;

        data[stringOfArray] = keyData;
    }
Beispiel #7
0
    // Registers a set of actions with predictor, updating
    // its data. We assume actions has exactly nValue
    // elements in it.
    public void registerSequence(CubeType[] actions)
    {
        double startTime = Time.realtimeSinceStartup;

        if(actions.Length != nValue){
            Debug.Log("actions.Length != nValue");
            Debug.Break();
        }

        // Split the sequence into a key and value for the Hashtable
        CubeType[] previousActions = new CubeType[nValue-1];
        for (int i = 0; i < nValue-1; i++)
        {
            previousActions[i] = actions[i];
        }

        CubeType currentAction = actions[nValue-1];

        keyStringOfArray = "";
        keyStringOfArray = returnStringName(previousActions);
        if(doDebugLogs)
            Debug.Log(playerThisNgramPredictorBelongsTo +  "registered sequence: " + keyStringOfArray + " + " + currentAction);

        if (!data.ContainsKey(keyStringOfArray))
        {
            data[keyStringOfArray] = new KeyDataRecord();
        }

        // Get the correct data structure
        KeyDataRecord keyData = (KeyDataRecord)data[keyStringOfArray];

        if (keyData.counts == null)
            keyData.counts = new Hashtable();

        // Make sure we have a record for the follow on value
        if(!keyData.counts.ContainsKey(currentAction))
            keyData.counts[currentAction] = 0;

        // Add to the total, and to the count for the value
        keyData.counts[currentAction] = (int)keyData.counts[currentAction] + 1;
        keyData.total += 1;

        data[keyStringOfArray] = keyData;

        double temp = Time.realtimeSinceStartup-startTime;
        overallTimeTakenForRegistering += temp;
        overallRegisters++;
        averageTimeTakenForRegistering = overallTimeTakenForRegistering / overallRegisters;
    }