#NEWLINE# def learn( self ) : #NEWLINE# #INDENT# " Learn on the current dataset, either for many timesteps and\n            even episodes (batchMode = True) or for a single timestep\n            (batchMode = False). Batch mode is possible, because Q-Learning\n            is an off-policy method.\n\n            In batchMode, the algorithm goes through all the samples in the\n            history and performs an update on each of them. if batchMode is\n            False, only the last data sample is considered. The user himself\n            has to make sure to keep the dataset consistent with the agent's\n            history.\n        " #NEWLINE# if self . batchMode : #NEWLINE# #INDENT# samples = self . dataset #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# samples = [ [ self . dataset . getSample ( ) ] ] #NEWLINE# #UNINDENT# for seq in samples : #NEWLINE# #INDENT# self . laststate = None #NEWLINE# self . lastaction = None #NEWLINE# self . lastreward = None #NEWLINE# for ( state , action , reward ) in seq : #NEWLINE# #INDENT# state = int ( state ) #NEWLINE# action = int ( action ) #NEWLINE# if ( self . laststate == None ) : #NEWLINE# #INDENT# self . lastaction = action #NEWLINE# self . laststate = state #NEWLINE# self . lastreward = reward #NEWLINE# continue #NEWLINE# #UNINDENT# qvalue = self . module . getValue ( self . laststate , self . lastaction ) #NEWLINE# maxnext = self . module . getValue ( state , self . module . getMaxAction ( state ) ) #NEWLINE# self . module . updateValue ( self . laststate , self . lastaction , ( qvalue + ( self . alpha * ( ( self . lastreward + ( self . gamma * maxnext ) ) - qvalue ) ) ) ) #NEWLINE# self . laststate = state #NEWLINE# self . lastaction = action #NEWLINE# self . lastreward = reward