-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdaw_mbf_1.py
203 lines (185 loc) · 6.65 KB
/
daw_mbf_1.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import random
import modelbasedforward as mb
# Uses the Q-update strategy found in Daw et al. 2011 supplemental materials
# Implements the Daw task without learning the transition probabilities
class Agent():
def __init__(self, randomReward = True, case1 = 0.25, case2 = 0.5, case3 = 0.5, case4 = 0.75,
alpha=0.3, noise=0.05):
state_dict = {1:[0], 2:[1, 2]}
transition_dict = {(0, "left", 1):0.7,
(0, "left", 2):0.3,
(0, "right", 1):0.3,
(0, "right", 2):0.7,
(1, "left", 0):1.0,
(1, "right", 0):1.0,
(2, "left", 0):1.0,
(2, "right", 0):1.0}
self.ai = mb.ModelBasedForward(actions=["left", "right"], states = state_dict, transitions=transition_dict,
alpha=alpha, noise=noise)
self.lastAction = None
self.lastState = None
self.numLevels = len(state_dict)
self.firstLevel = 1
self.currBoardState = 0
self.lastBoardState = None
self.currLevel = 1
self.currAction = None
self.pastReward = 0
self.currReward = 0
# stuff to set up the random walk of reward
self.SD = 0.025
self.lowerBoundary = 0.25
self.upperBoundary = 0.75
if randomReward:
self.case1RewardProb = self.initializeReward()
self.case2RewardProb = self.initializeReward()
self.case3RewardProb = self.initializeReward()
self.case4RewardProb = self.initializeReward()
else:
self.case1RewardProb = case1
self.case2RewardProb = case2
self.case3RewardProb = case3
self.case4RewardProb = case4
def initializeReward(self):
rewardProb = 0
while rewardProb < self.lowerBoundary or rewardProb > self.upperBoundary:
rewardProb = random.random()
return rewardProb
def getLastBoardState(self):
return self.lastBoardState
def getCurrBoardState(self):
return self.currBoardState
def getLastAction(self):
return self.lastAction
def getCurrReward(self):
return self.currReward
# random walk function
def randomWalk(self, oldValue):
newValue = 0
noise = random.gauss(0, self.SD)
addNoise = oldValue + noise
if addNoise > self.upperBoundary:
diff = self.upperBoundary - oldValue # how much distance between old value and upper boundary
extra = noise - diff # how much the noise makes the value go over the upper boundary
pointDiff = diff - extra # reflecting back, should be pos if
newValue = oldValue + pointDiff # old value plus whatever reflecting value we've calculated
elif addNoise < self.lowerBoundary:
diff = oldValue - self.lowerBoundary
extra = -noise - diff
pointDiff = diff - extra
newValue = oldValue - pointDiff
else:
newValue = addNoise
return newValue
# this should return the current reward based on the
# action taken in the current state
def calcReward(self, currState, currAction):
if currState == 0:
return 0
currProb = random.random()
if currState == 1:
if currAction == "left": # choose left (CASE 1)
reward = 0
#print currProb, self.case1RewardProb
if currProb > self.case1RewardProb:
reward = 1
return reward
elif currAction == "right": # choose right (CASE 2)
reward = 0
#print currProb, self.case2RewardProb
if currProb > self.case2RewardProb:
reward = 1
return reward
else:
print "Something went very wrong with choosing the action: should be either left or right"
return None
if currState == 2:
if currAction == "left": # choose left (CASE 3)
reward = 0
#print currProb, self.case3RewardProb
if currProb > self.case3RewardProb:
reward = 1
return reward
elif currAction == "right": # choose right (CASE 4)
reward = 0
#print currProb, self.case4RewardProb
if currProb > self.case4RewardProb:
reward = 1
return reward
else:
print "Something went very wrong with choosing the action: should be either left or right"
return None
def updateRewardProb(self):
self.case1RewardProb = self.randomWalk(self.case1RewardProb)
self.case2RewardProb = self.randomWalk(self.case2RewardProb)
self.case3RewardProb = self.randomWalk(self.case3RewardProb)
self.case4RewardProb = self.randomWalk(self.case4RewardProb)
# calculates the next state probabilistically
# (may want to include some way to change these probabilities externally)
# the paper does say that this prob was fixed throughout the experiment
def calcNextState(self, currState, currAction):
nextState = 0
if currState == 0:
#print "here"
if currAction == "left":
state1Prob = random.random()
if state1Prob > 0.3: # more likely to be state 1
nextState = 1
else:
nextState = 2
if currAction == "right":
state1Prob = random.random()
if state1Prob > 0.7: # more likely to be state 2
nextState = 1
else:
nextState = 2
return nextState
def calcNextLevel(self):
if self.currLevel == self.numLevels:
return self.firstLevel
else:
return self.currLevel + 1
def oneStep(self):
#print ""
#print "debug:"
#print " ", self.lastBoardState, self.lastAction, self.currBoardState, self.currLevel
currAction = self.ai.chooseAction(self.currBoardState)
#print " and the current action is", currAction
nextBoardState = self.calcNextState(self.currBoardState, currAction)
self.currReward = self.calcReward(self.currBoardState, currAction)
self.updateRewardProb() #bookkeeping step
if self.lastAction != None:
#print " learning is happening"
if self.ai.learn(self.lastBoardState, self.lastAction, self.pastReward, self.currBoardState, self.currLevel) == None:
return None
# more bookkeeping
self.lastBoardState = self.currBoardState
self.currBoardState = nextBoardState
self.currLevel = self.calcNextLevel()
self.pastReward = self.currReward
self.lastAction = currAction
return 1
if __name__ == '__main__':
agent = Agent()
#print "firstStageChoice secondStage secondStageChoice finalReward"
firstStageChoice = None
secondStage = None
secondStageChoice = None
finalReward = None
for step in range(10): # Repeat (for each step of episode):
if agent.oneStep() == None:
print "oneStep broke"
break
#tempLastBoardState = agent.getLastBoardState()
#tempLastAction = agent.getLastAction()
#tempCurrReward = agent.getCurrReward()
#tempCurrBoardState = agent.getCurrBoardState()
if step%2 == 0: # in stage 1
firstStageChoice = agent.getLastAction()
secondStage = agent.getCurrBoardState()
#print agent.getLastBoardState(), agent.getLastAction(), agent.getCurrReward(), agent.getCurrBoardState()
else: # in stage 2
secondStageChoice = agent.getLastAction()
finalReward = agent.getCurrReward()
print firstStageChoice, secondStage, secondStageChoice, finalReward
#print " ", agent.getLastBoardState(), agent.getLastAction(), agent.getCurrReward()