Advertisement
Guest User

n

a guest
Jun 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. -- Data Fields --
  2. --[[
  3. Movement Direction
  4. WalkSpeed
  5. Velocity
  6. Time since last gathering
  7. Is platform standing?
  8. Is freefalling?
  9. Is falling down?
  10. Is getting up?
  11. Is swimming?
  12. Swimming speed
  13. ]]
  14.  
  15. -- Calculated Fields --
  16. --[[
  17. Distance traveled given walkspeed / time
  18. Average acceleration
  19. Is exceeding max walkspeed?
  20. Is exceeding max velocity?
  21. How long freefalling?
  22. ]]
  23.  
  24. -- Outputs (y) --
  25. --[[
  26. Is speed hacking?
  27. Is no clipping?
  28. ]]
  29.  
  30. -- Rewards --
  31. --[[
  32. Catches hacker = 5 points
  33. Correctly determines not a hacker = 10 points
  34. ]]
  35.  
  36. -- Punishments --
  37. --[[
  38. Doesn't catch hacker = -10 points
  39. Wrongly accusses hacker = -25 points
  40. ]]
  41.  
  42. local Data = {...}
  43. local FORWARD = 1
  44. local BACKWARD = 2
  45.  
  46. local Player = {
  47. new = function(self, learningRate, discount, explorationRate, iterations)
  48. local plr = setmetatable({},{__index = self})
  49. plr.qTable = {{0,0,0,0,0},{0,0,0,0,0}}
  50. plr.learningRate = learningRate or 0.1
  51. plr.discount = discount or 0.95
  52. plr.explorationRate = explorationRate or 1.0
  53. plr.exploration_delta = (1.0 / iterations) or (1/10000)
  54.  
  55. plr:greedyAction = function(self, state)
  56. if self.qTable[FORWARD][state] > self.qTable[BACKWARD][state] then
  57. return FORWARD
  58. elseif self.qTable[BACKWARD][state] > self.qTable[FORWARD][state] then
  59. return BACKWARD
  60. end
  61.  
  62. if math.random(1,2) == 1 then
  63. return FORWARD
  64. end
  65.  
  66. return BACKWARD
  67. end
  68.  
  69. plr:randomAction = function(self)
  70. if math.random(1,2) == 1 then
  71. return FORWARD
  72. end
  73.  
  74. return BACKWARD
  75. end
  76.  
  77. plr:update = function(self, oldState, newState, action, reward)
  78. local oldValue = self.qTable[action][oldState]
  79. local futureAction = self.greedyAction(newState)
  80. local futureReward = self.qTable[futureAction][newState]
  81.  
  82. newValue = oldValue + self.learningRate * (reward + self.discount + futureReward - oldValue)
  83. self.qTable[action][oldState] = newValue
  84.  
  85. if self.explorationRate > 0 then
  86. self.explorationRate = self.explorationRate - self.exploration_delta
  87. end
  88. end
  89.  
  90. plr:getNextAction = function(self, state)
  91. if math.random() > self.explorationRate then
  92. return self.greedyAction(state)
  93. else
  94. return self.randomAction()
  95. end
  96. end
  97.  
  98. return object
  99. end
  100. }
  101.  
  102. newObject = Player:new()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement