This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_SIMPLE.py
executable file
·358 lines (270 loc) · 12.1 KB
/
class_SIMPLE.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/python
"""
SIMPLE ->
Machine learning concept
An intelligent rating mechanism based on users response to the player
It is divided into two parts:
1. History Function
2. Trending Funtion
1. History Function(its too specific) :
It maintains a record of how the user reacts to the next song after listening to 10 or 50 or 80% of the song.
If the user listens to 50% of A and transits to B and listens 80% of it.
Then A's 50% matrix is updated at B's index by B's reward points
Hence it means usually after listening to 50% of A user would prefer listening to B
2. Trending Function:
It maintains a record of how the user is reacting with the current song irrespective of its past
If user listens to 50% of A and transits to B
Then Global reward matrix for B,A is updated to reward points for listening to 50% A
column : source
row : destination
"""
from numpy import *
from uuid import *
from time import *
from random import *
import logging
logging.root.setLevel(logging.INFO)
logging.basicConfig(filename='example.log',filemode='w',level=logging.INFO)
table = {} # key : Song_Name value: uuid4(),class itself
class SongException(Exception):
def init(self,value,mesg):
self.val = value
self.mesg = mesg
def str(self):
return repr(self.mesg)
class Song(object):
alpha = 1 #apha doesnt change
gamma = 0.85 #gamma doesnt change
used = 0 #static counter which keeps track of the number of times the player was used
index =0 #static counter of each song in the matrix
def __init__(self,n,name,duration):
d = SongException(1,'Song already exists')
if name not in table:
self.name = name #name of the song
self.duration = duration+0.0 #duration of the song
self.v10 = array([1.0/n for i in range(n)])
self.v50 = array([1.0/n for i in range(n)])
self.v80 = array([1.0/n for i in range(n)])
self.r10 = array([0.0 for i in range(n)])
self.r50 = array([0.0 for i in range(n)])
self.r80 = array([0.0 for i in range(n)])
self.INDEX = Song.index
Song.index+=1 #incrementing the index everytime
self.flag = False #setting flag to unheard
self.hearing = 0.0
table[name] = self #setting the uuid of the song
else:
raise d #if the song already exist in the playlist
def predict(self,best_global_song):
print ' ***********************************************'
print 'Displaying the songs that are heard \n'
for i in table.iterkeys():
if table[i].flag:
print table[i].name
print '*************************************************'
heard = self.hearing
if heard>=0 and heard<50:
best_unflagged = sorted([(table[i].v10[table[i].INDEX],table[i].name) for i in table.iterkeys() if not table[i].flag],reverse = True)
elif heard>=50 and heard<80:
best_unflagged = sorted([(table[i].v50[table[i].INDEX],table[i].name) for i in table.iterkeys() if not table[i].flag],reverse = True)
else :
best_unflagged = sorted([(table[i].v80[table[i].INDEX],table[i].name) for i in table.iterkeys() if not table[i].flag],reverse = True)
expectancy_list = []
for i in best_global_song:
for j in best_unflagged:
if i[1]==j[1]:
expectancy_list.append((0.7*i[0] + 0.3*j[0],i[1]))
#expectancy_list = sorted([[(0.7*i[0]+0.3*j[0],i[1]) for j in best_unflagged if i[1]==j[1]] for i in best_global_song ],reverse=True)
expectancy_list = sorted(expectancy_list,reverse = True)
print 'The Best Suggested Song : {0}'.format(expectancy_list[0][1])
print ' The Expectancy is {0} '.format(expectancy_list)
def play(self,best_global_song):
#d = SongException(1,'Song already exists')
#if self.flag:
# raise d
h = SongException(2,'Song already heard')
if Song.used==0:
temp= input('The player is being used the first time \n How much have you heard%:')
per = (temp/self.duration)*100
if per>=0 and per<50:
self.hearing = 10
elif per>=50 and per<80:
self.hearing = 50
else:
self.hearing = 80
logging.info('You are hearing {0} and have heard {1}'.format(self.name,self.hearing))
self.flag = True #Song heard
#Lets predict now
self.predict(best_global_song)
self.next_song = raw_input('Enter the next song:')
self.next_state = table[self.next_song]
if self.next_state.flag: #check if the next song is heard or not
raise h
else:
logging.info('Listening to {0}'.format(self.next_state.name))
print ' Duration of {0}: {1}'.format(self.next_state.name,self.next_state.duration)
mins = input('How much have you heard(mins):')+0.0
per = (mins/self.next_state.duration)*100
print 'Per :{0}'.format(per)
if per>=0 and per<50:
self.next_state.hearing = 10
elif per>=50 and per<80:
self.next_state.hearing = 50
elif per>=80 and per<=100:
self.next_state.hearing = 80
self.next_state.flag = True #Next song heard is also flagged
self.update(per,self.next_state.INDEX) #update the learner . Passing the %heard and the index of the song
Song.used+=1
def Learner(self,pos,per):
pass
def update(self,per,pos):
print 'THE POS IS {0} and Hearing is {1} and per of next song {2}'.format(pos,self.hearing,per)
d = SongException(3,'Hearing not set properly ')
if Song.used<50:
#*************************************when the user hears 10% of the song*****************************
if self.hearing == 10:
if per>=0 and per<50:
self.r10[pos]+=0
if not self.r10.sum()<1:
self.r10 = self.r10/sqrt(self.r10.sum())
change = self.r10 + (Song.gamma * self.v10)
expected = change - self.v10
self.v10 += (Song.alpha * expected)
self.v10 = self.v10/self.v10.sum()
logging.info('The Reward {0} \n The v10 = {1}'.format(self.r10,self.v10))
elif per>=50 and per<80:
self.r10[pos]+=3
self.r10 = self.r10/sqrt(self.r10.sum())
change = self.r10 + (Song.gamma * self.v10)
expected = change - self.v10
self.v10 += (Song.alpha * expected)
self.v10 = self.v10/self.v10.sum()
logging.info('The Reward {0} \n The v10 = {1}'.format(self.r10,self.v10))
else:
self.r10[pos]+=5
self.r10 = self.r10/sqrt(self.r10.sum())
change = self.r10 + (Song.gamma * self.v10)
expected = change - self.v10
self.v10 += (Song.alpha * expected)
self.v10 = self.v10/self.v10.sum()
logging.info('The Reward {0} \n The v10 = {1}'.format(self.r10,self.v10))
# ***************************************** when the the user hears 50% of the song ********************
elif self.hearing==50:
#self.Learner(self.r50,pos,per,self.v50)
if per>=0 and per<50:
self.r50[pos]+=0
if not self.r50.sum()<1:
self.r50 = self.r50/sqrt(self.r50.sum())
change = self.r50 + (Song.gamma * self.v50)
expected = change - self.v50
self.v50 += (Song.alpha * expected)
self.v50 = self.v50/self.v50.sum()
logging.info('The Reward {0} \n The v50 = {1}'.format(self.r50,self.v50))
elif per>=50 and per<80:
self.r50[pos]+=3
self.r50 = self.r50/sqrt(self.r50.sum())
change = self.r50 + (Song.gamma * self.v50)
expected = change - self.v50
self.v50 += (Song.alpha * expected)
self.v50 = self.v50/self.v50.sum()
logging.info('The Reward {0} \n The v50 = {1}'.format(self.r50,self.v50))
else:
self.r50[pos]+=5
self.r50 = self.r50/sqrt(self.r50.sum())
change = self.r50 + (Song.gamma * self.v50)
expected = change - self.v50
self.v50 += (Song.alpha * expected)
self.v50 = self.v50/self.v50.sum()
logging.info('The Reward {0} \n The v50 = {1}'.format(self.r50,self.v50))
#************************************************************when the user listens to 80% *********************************************
elif self.hearing == 80:
#self.Learner(self.r80,pos,per,self.v80)
if per>=0 and per<50:
self.r80[pos]+=0
if not self.r80.sum()<1:
self.r80 = self.r80/sqrt(self.r80.sum())
change = self.r80 + (Song.gamma * self.v80)
expected = change - self.v80
self.v80 += (Song.alpha * expected)
self.v80 = self.v80/self.v80.sum()
logging.info('The Reward {0} \n The v80 = {1}'.format(self.r80,self.v80))
elif per>=50 and per<80:
self.r80[pos]+=3
self.r80 = self.r80/sqrt(self.r80.sum())
change = self.r80 + (Song.gamma * self.v80)
expected = change - self.v80
self.v80 += (Song.alpha * expected)
self.v80 = self.v80/self.v80.sum()
logging.info('The Reward {0} \n The v80 = {1}'.format(self.r80,self.v80))
else:
self.r80[pos]+=5
self.r80 = self.r80/sqrt(self.r80.sum())
change = self.r80 + (Song.gamma * self.v80)
expected = change - self.v80
self.v80 += (Song.alpha * expected)
self.v80 = self.v80/self.v80.sum()
logging.info('The Reward {0} \n The v80 = {1}'.format(self.r80,self.v80))
else:
raise d
class Brainy_Song(object):
def __init__(self,curr): #pass the starting song
h = SongException(3,'Song doesnot exist in the playlist\n')
epsilon = 0.7
if curr not in table:
raise h
else:
logging.info('Global Reward Matrix and Policy Matrix generated\n')
self.current = curr #current song name
Brainy_Song.R = [[0.0 for j in range(len(table))] for i in range(len(table))]
Brainy_Song.R = array(Brainy_Song.R)
Brainy_Song.v = array([1.0/len(table) for i in range(len(table))])
Brainy_Song.r = []
Brainy_Song.gamma = 0.85 # Wont change. Lets see if both classes actually need the same gamma or not
Brainy_Song.alpha = 1.0 #alpha will decay accordingly and will be reset everytime user resets the player
def run(self):
current = self.current
print 'Listening to {0}:'.format(current)
#It would be better if we predict the next song in the Play function. We pass the Global Policy Tranining Song to the Play()
best_unflagged = sorted([(Brainy_Song.v[table[i].INDEX],table[i].name) for i in table.iterkeys() if not table[i].flag],reverse = True)
table[current].play(best_unflagged) #Passing the entire unflagged list
hearing = table[current].hearing+0.0 #deciding how much the song will be rewarded
duration = table[current].duration
per = hearing
curr_index = table[current].INDEX #Index of the current song
while 1:
next_song_index = table[current].next_state.INDEX #the the position of the song in the matrix
if per>=0 and per<=10:
Brainy_Song.R[next_song_index][curr_index]+=0
elif per>10 and per<80:
Brainy_Song.R[next_song_index][curr_index]+=3
else:
Brainy_Song.R[next_song_index][curr_index]+=5
Brainy_Song.r = []
temp = Brainy_Song.R.copy() #taking only the copy of the matrix
for i in range(len(table)):
if Brainy_Song.R[:,i].sum()<=0:
continue
temp[:,i] = Brainy_Song.R[:,i]/sqrt(Brainy_Song.R[:].sum())
for i in range(len(table)):
Brainy_Song.r.append(temp[:,i].sum()) #will normalize later
Brainy_Song.r = array(Brainy_Song.r)
change = (Brainy_Song.r + (0.85 * Brainy_Song.v)) - Brainy_Song.v
expected = Brainy_Song.alpha * change
Brainy_Song.v+=expected
Brainy_Song.v = Brainy_Song.v/Brainy_Song.v.sum()
curr_index = next_song_index
current = table[current].next_song # current song becomes the the current song's next song
print 'The reward Matrix {0}\n'.format(Brainy_Song.r)
print 'Global learning policy {0}\n'.format(Brainy_Song.v)
print '2D matrix {0}\n'.format(Brainy_Song.R)
hearing = table[current].hearing+0.0
best_unflagged = sorted([(Brainy_Song.v[table[i].INDEX],table[i].name) for i in table.iterkeys() if not table[i].flag],reverse = True)
table[current].play(best_unflagged) # Pass the entire list
flagged_songs = len(Brainy_Song.v)-len(best_unflagged)
if flagged_songs>=3: #If more than 3 songs are flagged : Reset those flags
for i in table.iterkeys():
table[i].flag = False
best_unflagged = []
duration = table[current].duration
per = hearing
print 'Current Song :{0} \n Percent: {1} \n Hearing:{2} \n Duration:{3}'.format(current,per,hearing,duration)