-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotLearning.py
158 lines (131 loc) · 4.7 KB
/
plotLearning.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
import matplotlib.pyplot as plt
import numpy as np
def prep_data(movAvg,ratData=None):
"""
Calculates proportions for the trace.
"""
if ratData:
data = ratData
else:
with open('decisions.txt','r') as f:
lines = f.readlines()
data = [line.split(',') for line in lines][0]
normalize = 0
sum =0
proportions = []
for d in range(len(data)):
normalize += 1
if int(data[d]) == 11:
sum += 2
else:
sum += int(data[d])
if (normalize == movAvg) or (d == len(data)-1):
avg = float(sum) / float(normalize)
if avg > 1:
proportions.append(1)
else:
proportions.append(float(sum)/float(normalize))
normalize =0
sum = 0
return (proportions, len(data))
def raw_data():
with open('decisions.txt','r') as f:
lines = f.readlines()
data = [line.split(',') for line in lines][0]
return (data,range(1,len(data)+1))
def func(x, a, b, c):
return a * np.exp(-b * x) + c
def rat_data():
with open('rat10282.txt', 'r') as old:
lines = old.readlines()
oldDecisions = [line.split(',') for line in lines][0]
old = [float(o) for o in oldDecisions]
with open('rat10279.txt', 'r') as young:
lines = young.readlines()
youngDecisions = [line.split(',') for line in lines][0]
young = [float(y) for y in youngDecisions]
trials = min([len(oldDecisions),len(youngDecisions)])
return (young[0:trials],old[0:trials])
def plot_results(proportions,trialNum,movAvg,alpha,epsilon,discount):
"""
Plots the performance of the agent to learn the W-track spatial alternation task.
This is a simple 3 degree polynomial fit by first performing a least squares
polynomial fit and the second calculates the new points.
"""
trials = range(0,trialNum,movAvg)
figureName = 'LearningCurve.png'
plt.figure('Learning Curve')
#young rat
youngY = rat_data()[0]
youngX = trials[0:len(youngY)]
youngZ = np.polyfit(youngX,youngY,4)
youngf= np.poly1d(youngZ)
newYoungX = np.linspace(youngX[0],youngX[-1],50)
newYoungY = youngf(newYoungX)
#old rat
oldY = movAvg,rat_data()[1]
oldX = trials[0:len(oldY)]
oldZ = np.polyfit(oldX,oldY,4)
oldf = np.poly1d(oldZ)
newOldX = np.linspace(oldX[0],oldX[-1],50)
newOldY = oldf(newOldX)
# agent
x = trials[0:len(oldY)]
y = proportions[0:len(oldY)]
z = np.polyfit(x, y,4)
f = np.poly1d(z)
new_x = np.linspace(x[0], x[-1], 50)
new_y = f(new_x)
agent = plt.plot(new_x,new_y,'-')
young = plt.plot(newYoungX,newYoungY,'-')
old = plt.plot(newOldX,newOldY,'-')
plt.setp(agent, linewidth=3, color='purple',label='agent:\nalpha={0}\nepsilon={1}\ngamma={2}'.format(alpha,epsilon,discount))
plt.setp(young, linewidth=3, color='green',label='young')
plt.setp(old, linewidth=3, color='orange',label='old')
plt.axis([1,len(oldX),0,1.1])
plt.title('Learning Curve')
plt.legend(loc=4)
plt.xlabel("Cumulative Count of Trials")
plt.ylabel("Proportion Correct")
plt.savefig(figureName)
plt.show()
def plot_avg(proportions,trialNum,movAvg,alpha,epsilon,discount):
figureName = 'LearningCurveMovAvg.png'
plt.figure('Learning Curve')
#young
youngY_raw = prep_data(movAvg,rat_data()[0])[0][0:1201]
#old
oldY_raw = prep_data(movAvg,rat_data()[1])[0][0:1201]
#agent
y_raw = proportions[0:len(oldY_raw)]
minTrials = min([len(youngY_raw),len(oldY_raw),len(y_raw)])
youngY = youngY_raw[0:minTrials]
oldY = oldY_raw[0:minTrials]
y = y_raw[0:minTrials]
youngY[0] = 0.5
oldY[0] = 0.5
y[0] = 0.5
trials = range(0, trialNum, movAvg)[0:len(oldY)]
agent = plt.plot(trials, y, '-')
young = plt.plot(trials, youngY, '-')
old = plt.plot(trials, oldY, '-')
plt.setp(agent, linewidth=3, color='purple',
label='agent:(alpha={0}, epsilon={1}, gamma={2})'.format(alpha, epsilon, discount))
plt.setp(young, linewidth=3, color='green', label='young')
plt.setp(old, linewidth=3, color='orange', label='old')
if minTrials < 100:
plt.axis([1.0, 800.0, 0, 1.1])
else:
plt.axis([1.0, 1100.0, 0, 1.1])
plt.legend(loc=3)
plt.title('Learning Curve')
plt.xlabel("Cumulative Count of Trials")
plt.ylabel("Proportion Correct in " + str(movAvg) + "-trial moving window")
plt.savefig(figureName)
plt.show()
def plot(alpha,epsilon,discount):
props,trials = prep_data(1)
plot_results(props,trials,1,alpha,epsilon,discount)
def movAvg(alpha,epsilon,discount):
props,trials = prep_data(50)
plot_avg(props,1200,50,alpha,epsilon,discount)