<s> '''<STR_LIT>''' <EOL> from numpy import * <EOL> import scipy as Sci <EOL> import scipy . linalg <EOL> import copy <EOL> from matplotlib import pyplot <EOL> import abstract_rl <EOL> import lib . matplotlib_util as mpl_util <EOL> class Simple_RL ( abstract_rl . AbstractReinforcementLearner ) : <EOL> def __init__ ( self , alpha , gamma , num_states = ( <NUM_LIT:15> , <NUM_LIT:15> ) ) : <EOL> self . num_states = num_states [ <NUM_LIT:0> ] * num_states [ <NUM_LIT:1> ] <EOL> self . dimensions = num_states <EOL> self . alpha = alpha <EOL> self . gamma = gamma <EOL> self . curr_state = self . get_start_state ( ) <EOL> self . num_actions = <NUM_LIT:4> <EOL> self . Q_mat = zeros ( ( self . num_states , self . num_actions ) ) <EOL> self . trans_mat = { <NUM_LIT:0> : - <NUM_LIT:15> , <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:15> , <NUM_LIT:3> : - <NUM_LIT:1> } <EOL> def get_start_state ( self ) : <EOL> return random . randint ( <NUM_LIT:0> , self . num_states ) <EOL> def get_reward ( self , prev_state , action ) : <EOL> if self . prev_state + self . trans_mat [ action ] == ( self . num_states * <NUM_LIT:3> / <NUM_LIT:4> ) : <EOL> return <NUM_LIT:10> <EOL> return - <NUM_LIT:1> <EOL> def select_action ( self , state ) : <EOL> best_action = nonzero ( self . Q_mat [ state ] == max ( self . Q_mat [ state ] ) ) [ <NUM_LIT:0> ] <EOL> return best_action [ random . randint ( <NUM_LIT:0> , len ( best_action ) ) ] if random . random ( ) < <NUM_LIT> else random . randint ( <NUM_LIT:0> , self . num_actions ) <EOL> def execute_action ( self , state , action ) : <EOL> move = self . trans_mat [ action ] <EOL> self . prev_state = state <EOL> r = self . get_reward ( self . prev_state , action ) <EOL> self . curr_state = min ( max ( self . curr_state + move , <NUM_LIT:0> ) , self . num_states - <NUM_LIT:1> ) <EOL> if self . prev_state % self . dimensions [ <NUM_LIT:0> ] == <NUM_LIT:0> and self . curr_state % self . dimensions [ <NUM_LIT:0> ] == <NUM_LIT> : <EOL> self . curr_state -= <NUM_LIT:1> <EOL> if self . prev_state % self . dimensions [ <NUM_LIT:0> ] == <NUM_LIT> and self . curr_state % self . dimensions [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> self . curr_state += <NUM_LIT:1> <EOL> self . Q_mat [ self . prev_state , action ] = ( <EOL> self . Q_mat [ self . prev_state , action ] * ( <NUM_LIT:1> - self . alpha ) + <EOL> ( r + self . gamma * max ( self . Q_mat [ self . curr_state ] ) ) * self . alpha ) <EOL> if r == <NUM_LIT:10> : <EOL> return True <EOL> return False <EOL> def display ( self ) : <EOL> fig1 = pyplot . figure ( <NUM_LIT:1> ) <EOL> mpl_util . plot_running_average ( self . tot_reward , <NUM_LIT:100> ) <EOL> fig1 . suptitle ( "<STR_LIT>" ) <EOL> fig2 = pyplot . figure ( <NUM_LIT:2> ) <EOL> mpl_util . plot_running_average ( self . tot_steps , <NUM_LIT:100> ) <EOL> fig2 . suptitle ( "<STR_LIT>" ) <EOL> pyplot . show ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> rl = Simple_RL ( <NUM_LIT> , <NUM_LIT> ) <EOL> rl . run ( <NUM_LIT:1000> ) <EOL> print rl . Q_mat </s>
