Advertisement
Guest User

;w;

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BigramPredictor {
  6. int maxDataLength;
  7. List<char> data = new List<char>();
  8.  
  9. Dictionary<string, int> patternCounter = new Dictionary<string, int>();
  10.  
  11. public BigramPredictor(int length)
  12. {
  13. maxDataLength = length;
  14.  
  15. // initialize pattern counters;
  16. patternCounter["LL"] = 0;
  17. patternCounter["LR"] = 0;
  18. patternCounter["RL"] = 0;
  19. patternCounter["RR"] = 0;
  20. }
  21.  
  22. public char GetNextPrediction()
  23. {
  24. char prediction;
  25.  
  26. if (data.Count > 1)
  27. {
  28. char lastElement;/* = data[data.Count - 1];*/
  29.  
  30. // To do : Get the number of L (and R respectively) after the last element from the patternCounter dictionary
  31. int numL = 0;
  32. int numR = 0;
  33. for(int i = 1; i < data.Count; i++)
  34. {
  35. lastElement = data[data.Count - i];
  36. if(lastElement == 'L')
  37. {
  38. numL++;
  39. }
  40. if(lastElement == 'R')
  41. {
  42. numR++;
  43. }
  44. }
  45. lastElement = data[data.Count - 1];
  46.  
  47. int total = numL + numR;
  48.  
  49. // To do : calculate probabilities to get L and R
  50. float probabilityToGetL = 0;
  51. float probabilityToGetR = 0;
  52.  
  53. if(lastElement == 'L')
  54. {
  55. if(patternCounter["LL"] != 0)
  56. {
  57. patternCounter["LL"] = patternCounter["LL"] / numL;
  58. }
  59. if (patternCounter["LR"] != 0)
  60. {
  61. patternCounter["LR"] = patternCounter["LR"] / numR;
  62. }
  63. }
  64. else if(lastElement == 'R')
  65. {
  66. if (patternCounter["RL"] != 0)
  67. {
  68. patternCounter["RL"] = patternCounter["RL"] / numL;
  69. }
  70. if (patternCounter["RR"] != 0)
  71. {
  72. patternCounter["RR"] = patternCounter["RR"] / numR;
  73. }
  74. }
  75.  
  76. if (total > 0)
  77. {
  78. if (probabilityToGetL > probabilityToGetR)
  79. {
  80. prediction = 'L';
  81. }
  82. else
  83. {
  84. prediction = 'R';
  85. }
  86. return prediction;
  87. }
  88. }
  89.  
  90. // if prediction is not decided by the Ngram predictor then use random function.
  91. prediction = Random.Range(0, 2) < 1 ? 'L' : 'R';
  92. return prediction;
  93. }
  94.  
  95. public void InputTrainingData(char newValue)
  96. {
  97. if (data.Count > 0)
  98. {
  99. // update the counter with the new input value correspondingly
  100. char lastElement = data[data.Count - 1];
  101. patternCounter[lastElement.ToString() + newValue.ToString()] ++;
  102. }
  103. data.Add(newValue);
  104.  
  105. while(data.Count > maxDataLength)
  106. {
  107. // update the counter with the deleted value correspondingly
  108. patternCounter[data[0].ToString() + data[1].ToString()] --;
  109. data.RemoveAt(0); // remove head
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement