forked from pybrain/pybrain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
egreedy.py
29 lines (19 loc) · 826 Bytes
/
egreedy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from learning import LearningAgent
from scipy import random, array
class EpsilonGreedyAgent(LearningAgent):
def __init__(self, module, learner):
LearningAgent.__init__(self, module, learner)
self.epsilon = 0.5
self.epsilondecay = 0.9999
def getAction(self):
""" activates the module with the last observation and stores the result as last action. """
# get greedy action
action = LearningAgent.getAction(self)
# explore by chance
if random.random() < self.epsilon:
action = array([random.randint(self.module.numActions)])
# setting finally chosen action
self.lastaction = action
# reduce epsilon
self.epsilon *= self.epsilondecay
return action