-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandworld.py
More file actions
237 lines (199 loc) · 7.59 KB
/
randworld.py
File metadata and controls
237 lines (199 loc) · 7.59 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import numpy as np
import matplotlib.pyplot as plt
import random
import math
import matplotlib
import copy
from sklearn.utils import resample
# defining a useful function
def getFirst10(data):
#last dimension needs to be time
Times = np.ones([data.shape[0],data.shape[1],data.shape[2],10]) * 240
for idx1 in range(data.shape[0]):
for idx2 in range(data.shape[1]):
for idx3 in range(data.shape[2]):
times = np.where(data[idx1,idx2,idx3]==True)[0][:10] + 1
Times[idx1,idx2,idx3,:times.shape[0]] = times
Times[idx1,idx2,idx3,1:] -= Times[idx1,idx2,idx3,:-1].copy()
return Times
class Rat:
def __init__(self, startState, qvals, alpha, beta):#, gamma, epsilon):
self.state = startState
self.qvals = qvals #m x n matrix; m actions, n state variables
self.alpha = alpha #learning rate
self.gamma = 1 #discounting is none
self.action = float('nan')
self.beta = beta #learning the average reward
self.avgR = 0
self.selectionProbs = float('nan')
self.rpes = []
self.rews = []
self.epsilon = 0.9
def update(self, rwd, newState, rwdIsRPE, update_now=True):
'''
This function is used at every timestep in the simulations to perform all tasks of the rat
'''
#decide action values and action
actionVals = np.matmul(self.qvals, newState)
aVals = actionVals - max(actionVals)
newProbs = (math.e**aVals)/sum(math.e**aVals)
# newProbs = (math.e**actionVals)/sum(math.e**actionVals)
if np.random.rand()>self.epsilon:
newAction = np.random.choice(np.arange(self.qvals.shape[0]))
else:
newAction = np.random.choice(np.arange(self.qvals.shape[0]), p=newProbs) #softmax
isExplore = False
newQval = actionVals[newAction]
#update q values
if (not math.isnan(self.action)):# & (not self.wasExplore): #if this is not the first move (ie. there was no earlier action)
if rwdIsRPE:
rpe = rwd
else:
qval = sum(self.qvals[self.action,:] * self.state)
rpe = rwd - self.avgR + self.gamma*newQval - qval
if not update_now:
rpe=0
self.rpes.append(rpe)
self.rews.append(rwd)
self.avgR = self.avgR + self.beta*rpe
self.qvals[self.action,:] = self.qvals[self.action,:] + self.alpha*rpe*self.state #self.state is the gradient of the qfunction in the linear case
#update state and action
self.state = newState
self.action = newAction
self.selectionProbs = newProbs
class BoxWorld_basic:
rest = 3 #setting other reward strengths
sniff = 3
groom = 3
trialLength = 120 #number of timesteps in a trial
def __init__(self,stimIsRPE,rewardStrength):
self.time = 0
zeroVec = np.zeros(1)
zeroVec[0] = 1
self.state = zeroVec
self.stimIsRPE = stimIsRPE
self.rewardStrength = rewardStrength
def getNextState(self,action):
'''
This function is used at every timestep in the simulations to perform all tasks of the world
ie. generating states for the rat.
'''
rwdIsRPE = False
if action == 3: #pressed
reward = self.rewardStrength
rwdIsRPE = self.stimIsRPE
elif action == 2: #groomed
reward = self.groom
elif action == 1: #sniffed around
reward = self.sniff
elif action == 0: #rested
reward = self.rest
elif math.isnan(action): #first turn
reward = 0
outState = self.state
return reward, outState, rwdIsRPE
class BoxWorld_complete:
rest = 3
sniff = 3
groom = 3
trialLength = 120
def __init__(self,stimIsRPE, rewardStrengths):
self.triad = 0
self.trial = 0
self.time = 0
zeroVec = np.zeros(6)
zeroVec[4] = 1
self.state = zeroVec
self.rewardStrengths = rewardStrengths
self.stimIsRPE = stimIsRPE
def getNextState(self,action):
rwdIsRPE = False
if action == 3: #pressed
if not self.trial == 1: #if we are in leading or trailing trial
reward = self.rewardStrengths[self.trial]
else:
reward = self.rewardStrengths[2-((self.triad)%3)]
self.state[:] = 0
self.state[(2-((self.triad)%3))+1] = 1
rwdIsRPE = self.stimIsRPE
elif action == 2: #groomed
reward = self.groom
elif action == 1: #sniffed around
reward = self.sniff
elif action == 0: #rested
reward = self.rest
elif math.isnan(action): #first turn
reward = 0
if self.time == self.trialLength-1:
self.time = 0
if self.trial == 2: #if was in trailing trial, move to leading
self.trial = 0
self.triad += 1
self.state[:]=0
self.state[4]=1
elif self.trial == 1: #if was in test, move to trail
self.trial += 1
self.state[:]=0
self.state[5]=1
elif self.trial == 0: #if was in lead, move to test
self.trial += 1
self.state[:]=0
self.state[0]=1
else:
self.time += 1
outState = self.state
return reward, outState, rwdIsRPE
#rat thinks states alternate lo hi lo hi etc
class BoxWorld_incomplete:
rest = 3
sniff = 3
groom = 3
trialLength = 120
def __init__(self,stimIsRPE, rewardStrengths):
self.triad = 0
self.trial = 0
self.time = 0
zeroVec = np.zeros(3) #worth pressing [0,1] or not [1,0]?
#zeroVec[0] = 1
self.previousWorthPress = False
self.state = zeroVec
self.rewardStrengths = rewardStrengths
self.stimIsRPE = stimIsRPE
def getNextState(self,action):
rwdIsRPE = False
if action == 3: #pressed
self.state[:] = 0
if not self.trial == 1: #if we are in leading or trailing trial
reward = self.rewardStrengths[self.trial]
self.state[self.trial] = 1
else:
reward = self.rewardStrengths[self.triad%3]
self.state[self.triad%3] = 1
if reward > 2:
self.previousWorthPress = True
else:
self.previousWorthPress = False
rwdIsRPE = self.stimIsRPE
elif action == 2: #groomed
reward = self.groom
elif action == 1: #sniffed around
reward = self.sniff
elif action == 0: #rested
reward = self.rest
elif math.isnan(action): #first turn
reward = 0
if self.time == self.trialLength-1:
self.time = 0
if self.trial == 2: #if in trailing trial
self.trial = 0
self.triad += 1
else:
self.trial += 1
if self.previousWorthPress:
self.state = np.array([0,0,1])
else:
self.state = np.array([1,0,0])
else:
self.time += 1
outState = self.state
return reward, outState, rwdIsRPE