Esempio n. 1
0
                1 if a_desc == 'R' else 0, s[1] - 1.5 if a_desc == 'R' else 0,
                (s[0] * s[1] - 3) / 3 if a_desc == 'R' else 0,
                (s[0] * s[0] - 2) / 2 if a_desc == 'R' else 0,
                (s[1] * s[1] - 4.5) / 4.5 if a_desc == 'R' else 0,
                1 if a_desc == 'R' else 0, 1
            ])

        return x_vector


learn_tracker = LearnTracker()
gridworld = get_gridworld(step_reward=-0.1)

NUM_EPISODES = 20000

alpha_obj = Alpha(alpha=0.1)
alpha_obj.set_half_life_for_N_episodes(Nepisodes=NUM_EPISODES,
                                       alpha_final=0.03333333333333)

eps_obj = EpsilonGreedy(epsilon=0.5)
eps_obj.set_half_life_for_N_episodes(Nepisodes=NUM_EPISODES,
                                     epsilon_final=0.16666666666666)

agent = SA_SemiGradAgent(environment=gridworld,
                         update_type='qlearn',
                         sa_linear_function=LazyProgrammerMaze(gridworld),
                         learn_tracker=learn_tracker,
                         gamma=0.9,
                         alpha=alpha_obj,
                         epsilon=eps_obj)
Esempio n. 2
0
import numpy as np

from introrl.black_box_sims.random_walk_1000 import RandomWalk_1000Simulation
from introrl.agent_supt.episode_maker import make_episode
from introrl.policy import Policy
from introrl.utils.tiles_rectangles import PartitionedSegment
from introrl.agent_supt.alpha_calc import Alpha

NUM_EPISODES = 100000

RW = RandomWalk_1000Simulation()
policy = Policy(environment=RW)
policy.intialize_policy_to_equiprobable(env=RW)

alpha_obj = Alpha(alpha=0.01)
alpha_obj.set_half_life_for_N_episodes(Nepisodes=NUM_EPISODES,
                                       alpha_final=2.0E-5)

pseg = PartitionedSegment(lo_val=0, hi_val=1000, num_regions=10)
#pseg.summ_print()


def get_x_vector(state):
    """Return the x vector that represents the state."""
    x_vector = pseg.get_numpy_encoding(state)
    return x_vector


def VsEst(state):
    """Return the current estimate for V(s) from linear function eval."""
    x_vector = get_x_vector(state)
Esempio n. 3
0
def td0_prediction(
    policy,
    state_value_coll,
    all_start_states=False,
    do_summ_print=True,
    show_last_change=True,
    alpha=0.1,
    const_alpha=True,
    alpha_half_life=200,
    max_num_episodes=1000,
    min_num_episodes=10,
    max_abserr=0.001,
    gamma=1.0,
    result_list='abserr',
    true_valueD=None,
    value_snapshot_loopL=None
):  # if input, save V(s) snapshot at iteration steps indicated
    """
    ... GIVEN A POLICY TO EVALUATE  apply TD(0), Temperal Difference(0) Prediction
    
    Terminates when abserr < max_abserr
    
    Assume that Q(s,a), action_value_coll, has been initialized prior to call.
    (Note tht the StateValues object has a reference to the Environment object)
    
    Assume environment attached to policy will have method "get_any_action_state_hash"
    in order to begin at any start state.
    
    action_value_coll WILL BE CHANGED... policy WILL NOT.
    """

    resultL = []  # based on result_list, can be "rms" or "abserr"
    value_snapD = {}  # index=loop counter, value=dict of {s_hash:Vs, ...}

    # ==> Note: the reference to Environment object as "state_value_coll.environment"
    Env = state_value_coll.environment

    alpha_obj = Alpha(alpha=alpha,
                      const_alpha=const_alpha,
                      half_life=alpha_half_life)

    if do_summ_print:
        print(
            '=============== TD(0) EVALUATING THE FOLLOWING POLICY ===================='
        )
        policy.summ_print(verbosity=0,
                          environment=Env,
                          show_env_states=False,
                          none_str='*')

    if all_start_states:
        s = 'Starting a Maximum of %i TD(0) All-Start-State Episodes\nGamma = %g'%\
            (max_num_episodes, gamma)
        start_stateL = [s_hash for s_hash in Env.iter_all_action_states()]
    else:
        s = 'Starting a Maximum of %i TD(0) Episodes from state "%s"\nGamma = %g'%\
            (max_num_episodes, str(Env.start_state_hash), gamma)
        start_stateL = [Env.start_state_hash]

    banner(s, banner_char='', leftMargin=0, just='center')

    num_episodes = 0
    keep_looping = True

    # value-iteration stopping criteria

    progress_str = ''
    while (num_episodes <= max_num_episodes - 1) and keep_looping:

        keep_looping = False
        abserr = 0.0  # calculated below as part of termination criteria

        # policy evaluation
        for start_hash in start_stateL:
            num_episodes += 1
            if num_episodes > max_num_episodes:
                break

            s_hash = start_hash
            a_desc = policy.get_single_action(s_hash)
            for _ in range(max_num_episodes):

                sn_hash, reward = Env.get_action_snext_reward(
                    s_hash, a_desc)  # prob-weighted choice

                state_value_coll.td0_update(s_hash=s_hash,
                                            alpha=alpha_obj(),
                                            gamma=gamma,
                                            sn_hash=sn_hash,
                                            reward=reward)

                if (sn_hash in Env.terminal_set) or (sn_hash is None):
                    break

                # get ready for next step
                s_hash = sn_hash

                a_desc = policy.get_single_action(s_hash)
                if a_desc is None:
                    print('a_desc is None for policy.get_single_action( "%s" ) ='%\
                          str(s_hash), a_desc)

        abserr = state_value_coll.get_biggest_action_state_err()
        if abserr > max_abserr:
            keep_looping = True

        if num_episodes < min_num_episodes:
            keep_looping = True  # must loop for min_num_episodes at least

        pc_done = 100.0 * float(num_episodes) / float(max_num_episodes)
        out_str = '%i%%' % (5 * (int(pc_done / 5.0)))
        if out_str != progress_str:
            print(out_str, end=' ')
            progress_str = out_str

        if result_list == 'rms':
            resultL.append(state_value_coll.calc_rms_error(true_valueD))
        if result_list == 'abserr':
            resultL.append(abserr)
        else:
            pass  # don't save anything to resultL

    if value_snapshot_loopL is not None and num_episodes in value_snapshot_loopL:
        value_snapD[num_episodes] = state_value_coll.get_snapshot()

    if do_summ_print:
        s = ''
        if num_episodes >= max_num_episodes:
            s = '   (NOTE: STOPPED ON MAX-ITERATIONS)'

        print('Exited MC Every-Visit Policy Evaluation', s)
        print('   num_episodes   =', num_episodes,
              ' (min limit=%i)' % min_num_episodes,
              ' (max limit=%i)' % max_num_episodes)
        print('   gamma          =', gamma)
        print('   estimated err  =', abserr)
        print('   Error limit    =', max_abserr)

        state_value_coll.summ_print(show_last_change=show_last_change,
                                    show_states=True)

    return resultL, value_snapD
Esempio n. 4
0
def mc_every_visit_prediction(
    policy,
    state_value_coll,
    all_start_states=False,
    do_summ_print=True,
    show_last_change=True,
    show_banner=True,
    max_episode_steps=10000,
    alpha=0.1,
    const_alpha=True,
    alpha_half_life=200,
    max_num_episodes=1000,
    min_num_episodes=10,
    max_abserr=0.001,
    gamma=0.9,
    result_list='abserr',
    true_valueD=None,
    value_snapshot_loopL=None
):  # if input, save V(s) snapshot at iteration steps indicated
    """
    ... GIVEN A POLICY TO EVALUATE  apply Monte Carlo Every Visit Prediction
    
    Use Episode Discounted Returns to find V(s), State-Value Function
    
    Terminates when abserr < max_abserr
    
    Assume that V(s), state_value_coll, has been initialized prior to call.
    (Note tht the StateValues object has a reference to the Environment object)
    
    Assume environment attached to policy will have method "get_any_action_state_hash"
    in order to begin at any start state.
    
    state_value_coll WILL BE CHANGED... policy WILL NOT.
    """

    resultL = []  # based on result_list, can be "rms" or "abserr"
    value_snapD = {}  # index=loop counter, value=dict of {s_hash:Vs, ...}

    # ==> Note: the reference to Environment object as "state_value_coll.environment"
    Env = state_value_coll.environment
    episode = Episode(Env.name + ' Episode')

    alpha_obj = Alpha(alpha=alpha,
                      const_alpha=const_alpha,
                      half_life=alpha_half_life)

    if do_summ_print:
        print(
            '=============== EVALUATING THE FOLLOWING POLICY ===================='
        )
        policy.summ_print(verbosity=0,
                          environment=Env,
                          show_env_states=False,
                          none_str='*')

    if all_start_states:
        s = 'Starting a Maximum of %i Monte Carlo All-Start-State Iterations\nGamma = %g' % (
            max_num_episodes, gamma)
        start_stateL = [s_hash for s_hash in Env.iter_all_action_states()]
    else:
        s = 'Starting a Maximum of %i Monte Carlo Iterations from state "%s"\nGamma = %g' % (
            max_num_episodes, str(Env.start_state_hash), gamma)
        start_stateL = [Env.start_state_hash]

    if show_banner:
        banner(s, banner_char='', leftMargin=0, just='center')

    num_episodes = 0
    keep_looping = True

    # value-iteration stopping criteria

    progress_str = ''
    while (num_episodes <= max_num_episodes - 1) and keep_looping:

        keep_looping = False
        abserr = 0.0  # calculated below as part of termination criteria

        # policy evaluation
        random.shuffle(start_stateL)
        for start_hash in start_stateL:

            # break from inner loop if max_num_episodes is hit.
            if num_episodes >= max_num_episodes:
                break

            make_episode(start_hash,
                         policy,
                         Env,
                         Env.terminal_set,
                         episode=episode,
                         max_steps=max_episode_steps,
                         eps_greedy=None)

            num_episodes += 1

            for dr in episode.get_rev_discounted_returns(gamma=gamma):
                (s_hash, a_desc, reward, sn_hash, G) = dr
                state_value_coll.mc_update(s_hash, alpha_obj(), G)

        abserr = state_value_coll.get_biggest_action_state_err()
        if abserr > max_abserr:
            keep_looping = True

        if num_episodes < min_num_episodes:
            keep_looping = True  # must loop for min_num_episodes at least

        pc_done = 100.0 * float(num_episodes) / float(max_num_episodes)
        out_str = '%i%%' % (5 * (int(pc_done / 5.0)))
        if out_str != progress_str:
            print(out_str, end=' ')
            progress_str = out_str

        if result_list == 'rms':
            resultL.append(state_value_coll.calc_rms_error(true_valueD))
        if result_list == 'abserr':
            resultL.append(abserr)
        else:
            pass  # don't save anything to resultL

    if value_snapshot_loopL is not None and num_episodes in value_snapshot_loopL:
        value_snapD[num_episodes] = state_value_coll.get_snapshot()

    if do_summ_print:
        s = ''
        if num_episodes >= max_num_episodes:
            s = '   (NOTE: STOPPED ON MAX-ITERATIONS)'

        print('Exited MC Every-Visit Policy Evaluation', s)
        print('   num episodes   =', num_episodes,
              ' (min limit=%i)' % min_num_episodes,
              ' (max limit=%i)' % max_num_episodes)
        print('   gamma          =', gamma)
        print('   estimated err  =', abserr)
        print('   Error limit    =', max_abserr)

        state_value_coll.summ_print(show_last_change=show_last_change,
                                    show_states=True)

    return resultL, value_snapD
Esempio n. 5
0
def sarsa_epsilon_greedy( environment,  learn_tracker=None, # track progress of learning
                          initial_Qsa=0.0, # init non-terminal_set of V(s) (terminal_set=0.0)
                          initial_action_value_coll=None, # if input, use it.
                          read_pickle_file='', 
                          save_pickle_file='',
                          use_list_of_start_states=False, # use list OR single start state of environment.
                          do_summ_print=True, show_last_change=True, fmt_Q='%g', fmt_R='%g',
                          pcent_progress_print=10,
                          show_banner = True,
                          max_num_episodes=1000, min_num_episodes=10, max_abserr=0.001, 
                          gamma=0.9,
                          iteration_prints=0,
                          max_episode_steps=sys.maxsize,
                          epsilon=0.1, const_epsilon=True, epsilon_half_life=200,
                          alpha=0.1, const_alpha=True, alpha_half_life=200,
                          N_episodes_wo_decay=0):
    """
    ... GIVEN AN ENVIRONMENT ... 
    apply SARSA Temporal Difference to find the OPTIMAL POLICY and STATE VALUES
    
    Returns: Policy and ActionValueColl objects
    
    Use Episode Discounted Returns to find V(s), State-Value Function
    
    Terminates when abserr < max_abserr
    
    Assume that V(s), action_value_coll, has been initialized prior to call.
    
    Assume environment attached to policy will have method "get_any_action_state_hash"
    in order to begin at any action state.
    
    CREATES BOTH policy AND action_value_coll OBJECTS.
    """
    
    # create EpsilonGreedy, Alpha and ActionValueColl objects
    eg = EpsilonGreedy(epsilon=epsilon, const_epsilon=const_epsilon, half_life=epsilon_half_life,
                       N_episodes_wo_decay=N_episodes_wo_decay)

    
    alpha_obj = Alpha( alpha=alpha, const_alpha=const_alpha, half_life=alpha_half_life )


    if initial_action_value_coll is None:
        action_value_coll = ActionValueColl( environment, init_val=initial_Qsa )
    else:
        action_value_coll = initial_action_value_coll
    #action_value_coll.summ_print()
    num_s_hash = len( environment.get_all_action_state_hashes() )

    if read_pickle_file:
        action_value_coll.init_from_pickle_file( read_pickle_file )
    
    if do_summ_print:
        print('================== EPSILON GREEDY DEFINED AS ========================')
        eg.summ_print()
        
        print('================== LEARNING RATE DEFINED AS ========================')
        alpha_obj.summ_print()
    
    if show_banner:
        s = 'Starting a Maximum of %i SARSA Epsilon Greedy Episodes'%max_num_episodes +\
            '\nfor "%s" with Gamma = %g, Alpha = %g'%( environment.name, gamma, alpha_obj() )
        banner(s, banner_char='', leftMargin=0, just='center')
        
    # Iterate over a list of known possible start states
    if use_list_of_start_states:
        loop_stateL = environment.limited_start_state_list()
    else:
        #loop_stateL = [ random.choice( environment.limited_start_state_list() ) ]
        loop_stateL = [ environment.start_state_hash ]
        
    if show_banner:
        print('======================= Iterating over Start States ==================================')
        print( loop_stateL )
        print('======================================================================================')

        
    # set counter and flag
    episode_loop_counter = 0
    keep_looping = True
    
    progress_str = ''
    while (episode_loop_counter<=max_num_episodes-1) and keep_looping :
            
        keep_looping = False
        abserr = 0.0 # calculated below as part of termination criteria
        Nterminal_episodes = set() # tracks if start_hash got to terminal_set or max_num_episodes
        
        for start_hash in loop_stateL:
            episode_loop_counter += 1
            if episode_loop_counter > max_num_episodes:
                break
            
            if learn_tracker is not None:
                learn_tracker.add_new_episode()
            
            s_hash = start_hash
            a_desc = action_value_coll.get_best_eps_greedy_action( s_hash, epsgreedy_obj=eg )
            
            for n_episode_steps in range( max_episode_steps ):
                
                # Begin an episode
                if a_desc is None:
                    Nterminal_episodes.add( start_hash )
                    print('break for a_desc==None')
                    break
                else:
                    sn_hash, reward = environment.get_action_snext_reward( s_hash, a_desc )
                    if learn_tracker is not None:
                        learn_tracker.add_sarsn_to_current_episode( s_hash, a_desc, 
                                                                    reward, sn_hash)
                    
                    if sn_hash is None:
                        Nterminal_episodes.add( start_hash )
                        print('break for sn_hash==None')
                        break
                    else:
                        an_desc = action_value_coll.get_best_eps_greedy_action( sn_hash, 
                                                                                epsgreedy_obj=eg )
            
                        action_value_coll.sarsa_update( s_hash=s_hash, a_desc=a_desc, 
                                                        alpha=alpha_obj(), gamma=gamma, 
                                                        sn_hash=sn_hash, an_desc=an_desc, 
                                                        reward=reward)
                        
                        if sn_hash in environment.terminal_set:
                            Nterminal_episodes.add( start_hash )
                            if (n_episode_steps==0) and (num_s_hash>2):
                                print('1st step break for sn_hash in terminal_set', sn_hash, 
                                      ' s_hash=%s'%str(s_hash), ' a_desc=%s'%str(a_desc))
                            break
                        s_hash = sn_hash
                        a_desc = an_desc
        
        # increment episode counter on EpsilonGreedy and Alpha objects
        eg.inc_N_episodes()
        alpha_obj.inc_N_episodes()
                
        abserr = action_value_coll.get_biggest_action_state_err()
        if abserr > max_abserr:
            keep_looping = True
            
        if episode_loop_counter < min_num_episodes:
            keep_looping = True # must loop for min_num_episodes at least
            
        pc_done = 100.0 * float(episode_loop_counter) / float(max_num_episodes)
        
        if pcent_progress_print > 0:
            out_str = '%3i%%'%( pcent_progress_print*(int(pc_done/float(pcent_progress_print)) ) )
        else:
            out_str = progress_str
        
        if out_str != progress_str:
            #score = environment.get_policy_score( policy=policy, start_state_hash=None, step_limit=1000)
            #print(out_str, ' score=%s'%str(score), ' = (r_sum, n_steps, msg)', end=' ')
            
            print(out_str, end=' ')
            print( 'Nterminal episodes =', len(Nterminal_episodes),' of ', len(loop_stateL))
            progress_str = out_str
    #print()
    
    policy = action_value_coll.get_policy()
    
    if do_summ_print:
        s = ''
        if episode_loop_counter >= max_num_episodes:
            s = '   (NOTE: STOPPED ON MAX-ITERATIONS)'

        print( 'Exited Epsilon Greedy, TD(0) Value Iteration', s )
        print( '   # episodes      =', episode_loop_counter, ' (min limit=%i)'%min_num_episodes, ' (max limit=%i)'%max_num_episodes )
        print( '   gamma           =', gamma )
        print( '   estimated err   =', abserr )
        print( '   Error limit     =', max_abserr )
        print( 'Nterminal episodes =', len(Nterminal_episodes),' of ', len(loop_stateL))
    
        action_value_coll.summ_print(show_last_change=show_last_change, fmt_Q=fmt_Q )
        policy.summ_print(  environment=environment, verbosity=0, show_env_states=False  )
        
        try: # sims may not have a layout_print
            environment.layout_print( vname='reward', fmt=fmt_R, show_env_states=False, none_str='*')
        except:
            pass

        print('================== EPSILON GREEDY DEFINED AS ========================')
        eg.summ_print()
        
        print('================== LEARNING RATE DEFINED AS ========================')
        alpha_obj.summ_print()

    if save_pickle_file:
        policy.save_to_pickle_file( save_pickle_file )
        action_value_coll.save_to_pickle_file( save_pickle_file )
        
    return policy, action_value_coll #, steps_per_episodeL, reward_sum_per_episodeL
Esempio n. 6
0
    def __init__(
        self,
        environment,
        learn_tracker=None,  # track progress of learning
        sa_linear_function=None,  # if input, use it.
        update_type='sarsa',  # can be 'sarsa', 'qlearn'
        read_pickle_file='',
        save_pickle_file='',
        do_summ_print=True,
        show_last_change=True,
        pcent_progress_print=10,
        show_banner=True,
        gamma=0.9,
        iteration_prints=0,
        max_episode_steps=sys.maxsize,
        epsilon=0.1,  # can be constant or EpsilonGreedy object
        alpha=0.1):  # can be constant or Alpha object
        """
        ... GIVEN AN ENVIRONMENT ... 
        Use basic SARSA or Qlearning algorithm to solve for linear approximation of
        STATE-ACTION VALUES, Q(s,a)
        
        Each action is forced to be a DETERMINISTIC action leading to one state and reward.
        (If the next state or reward changes, only the new values will be considered)
            
        attribute: self.action_value_linfunc is the linear approximation, Q(s,a) object
        
        A DETERMINISTIC policy can be created externally from the self.action_value_linfunc attribute.
        """
        self.environment = environment
        self.learn_tracker = learn_tracker
        self.save_pickle_file = save_pickle_file

        self.do_summ_print = do_summ_print
        self.show_last_change = show_last_change
        self.pcent_progress_print = pcent_progress_print

        self.gamma = gamma
        self.iteration_prints = iteration_prints
        self.max_episode_steps = max_episode_steps

        self.num_episodes = 0
        self.num_updates = 0

        # if input epsilon is a float, use it to create an EpsilonGreedy object
        if type(epsilon) == type(0.1):
            self.epsilon_obj = EpsilonGreedy(epsilon=epsilon,
                                             const_epsilon=True)
        else:
            self.epsilon_obj = epsilon

        # if input alpha is a float, use it to create an Alpha object
        if type(alpha) == type(0.1):
            self.alpha_obj = Alpha(alpha=alpha, const_alpha=True)
        else:
            self.alpha_obj = alpha

        # create the action_value_linfunc for the environment.
        self.action_value_linfunc = sa_linear_function
        self.update_type = update_type

        if read_pickle_file:
            self.action_value_linfunc.init_from_pickle_file(read_pickle_file)

        if do_summ_print:
            print(
                '================== EPSILON GREEDY DEFINED AS ========================'
            )
            self.epsilon_obj.summ_print()

            print(
                '================== LEARNING RATE DEFINED AS ========================'
            )
            self.alpha_obj.summ_print()

        if show_banner:
            s = 'Starting a Maximum of %i %s Semi-Gradient Epsilon Greedy Steps/Episode'%(self.max_episode_steps, update_type.upper()) +\
                '\nfor "%s" with Gamma = %g, Alpha = %g'%( environment.name, self.gamma, self.alpha_obj() )
            banner(s, banner_char='', leftMargin=0, just='center')
Esempio n. 7
0
class SA_SemiGradAgent(object):
    """
    SARSA or Qlearning semi-gradient agent for linear function approximator.
    """
    def __init__(
        self,
        environment,
        learn_tracker=None,  # track progress of learning
        sa_linear_function=None,  # if input, use it.
        update_type='sarsa',  # can be 'sarsa', 'qlearn'
        read_pickle_file='',
        save_pickle_file='',
        do_summ_print=True,
        show_last_change=True,
        pcent_progress_print=10,
        show_banner=True,
        gamma=0.9,
        iteration_prints=0,
        max_episode_steps=sys.maxsize,
        epsilon=0.1,  # can be constant or EpsilonGreedy object
        alpha=0.1):  # can be constant or Alpha object
        """
        ... GIVEN AN ENVIRONMENT ... 
        Use basic SARSA or Qlearning algorithm to solve for linear approximation of
        STATE-ACTION VALUES, Q(s,a)
        
        Each action is forced to be a DETERMINISTIC action leading to one state and reward.
        (If the next state or reward changes, only the new values will be considered)
            
        attribute: self.action_value_linfunc is the linear approximation, Q(s,a) object
        
        A DETERMINISTIC policy can be created externally from the self.action_value_linfunc attribute.
        """
        self.environment = environment
        self.learn_tracker = learn_tracker
        self.save_pickle_file = save_pickle_file

        self.do_summ_print = do_summ_print
        self.show_last_change = show_last_change
        self.pcent_progress_print = pcent_progress_print

        self.gamma = gamma
        self.iteration_prints = iteration_prints
        self.max_episode_steps = max_episode_steps

        self.num_episodes = 0
        self.num_updates = 0

        # if input epsilon is a float, use it to create an EpsilonGreedy object
        if type(epsilon) == type(0.1):
            self.epsilon_obj = EpsilonGreedy(epsilon=epsilon,
                                             const_epsilon=True)
        else:
            self.epsilon_obj = epsilon

        # if input alpha is a float, use it to create an Alpha object
        if type(alpha) == type(0.1):
            self.alpha_obj = Alpha(alpha=alpha, const_alpha=True)
        else:
            self.alpha_obj = alpha

        # create the action_value_linfunc for the environment.
        self.action_value_linfunc = sa_linear_function
        self.update_type = update_type

        if read_pickle_file:
            self.action_value_linfunc.init_from_pickle_file(read_pickle_file)

        if do_summ_print:
            print(
                '================== EPSILON GREEDY DEFINED AS ========================'
            )
            self.epsilon_obj.summ_print()

            print(
                '================== LEARNING RATE DEFINED AS ========================'
            )
            self.alpha_obj.summ_print()

        if show_banner:
            s = 'Starting a Maximum of %i %s Semi-Gradient Epsilon Greedy Steps/Episode'%(self.max_episode_steps, update_type.upper()) +\
                '\nfor "%s" with Gamma = %g, Alpha = %g'%( environment.name, self.gamma, self.alpha_obj() )
            banner(s, banner_char='', leftMargin=0, just='center')

    def run_episode(self, start_state, iter_sarsn=None):
        """
        Run a single episode of SARSA or Qlearning Semi-Gradient algorithm
        If iter_sarsn is input, use it instead of action_value_linfunc calculations.
        (Note: the start_state should NOT be in terminal_set if iter_sarsn is input.)
        """

        # increment episode counters
        self.num_episodes += 1
        self.epsilon_obj.inc_N_episodes()
        self.alpha_obj.inc_N_episodes()

        if self.learn_tracker is not None:
            self.learn_tracker.add_new_episode()

        # do SARSA or Qlearning Semi-Gradient loops until sn_hash in terminal_set
        s_hash = start_state

        n_steps_in_episode = 1
        while s_hash not in self.environment.terminal_set:

            if iter_sarsn is None:
                # get best epsilon-greedy action
                a_desc = self.action_value_linfunc.get_best_eps_greedy_action( \
                                                s_hash, epsgreedy_obj=self.epsilon_obj )
                # check for bad action value
                if a_desc is None:
                    print('break for a_desc==None at s_hash=%s' % str(s_hash))
                    break

                # get next state and reward
                sn_hash, reward = self.environment.get_action_snext_reward(
                    s_hash, a_desc)
            else:
                # retracing an existing episode
                s_hash, a_desc, reward, sn_hash = next(iter_sarsn)

            if self.learn_tracker is not None:
                self.learn_tracker.add_sarsn_to_current_episode(
                    s_hash, a_desc, reward, sn_hash)

            if sn_hash is None:
                print('break for sn_hash==None, #steps=', n_steps_in_episode,
                      ' s_hash=%s' % str(s_hash), ' a_desc=%s' % str(a_desc))
                break

            # do RL update of Q(s,a) value
            if self.update_type == 'sarsa':
                an_desc = self.action_value_linfunc.get_best_eps_greedy_action( \
                                                sn_hash, epsgreedy_obj=self.epsilon_obj )

                self.action_value_linfunc.sarsa_update(s_hash=s_hash,
                                                       a_desc=a_desc,
                                                       alpha=self.alpha_obj(),
                                                       gamma=self.gamma,
                                                       sn_hash=sn_hash,
                                                       an_desc=an_desc,
                                                       reward=reward)
            elif self.update_type == 'qlearn':

                self.action_value_linfunc.qlearning_update(
                    s_hash=s_hash,
                    a_desc=a_desc,
                    alpha=self.alpha_obj(),
                    gamma=self.gamma,
                    sn_hash=sn_hash,
                    reward=reward)

            self.num_updates += 1

            # keep a lid on the max number of episode steps.
            if n_steps_in_episode >= self.max_episode_steps:
                break

            # get ready for next loop
            n_steps_in_episode += 1
            s_hash = sn_hash

        #print(n_steps_in_episode, end=' ')

    def summ_print(self, long=True):  # pragma: no cover
        """Show State objects in sorted state_hash order."""
        print('___ Policy Evaluation Agent Summary ___')
        print('    Environment        = %s' % self.environment.name)
        print('    Update Type        = %s' % self.update_type)
        print('    Number of Episodes = %g' % self.num_episodes)

        print(
            '================== EPSILON GREEDY FINAL ========================')
        self.epsilon_obj.summ_print()

        print(
            '================== LEARNING RATE FINAL ========================')
        self.alpha_obj.summ_print()
Esempio n. 8
0
    NUM_EPISODES = 5000
    UPDATE_TYPE = 'sarsa'
    GAMMA = 1.0  # 0.99

    eg = EpsilonGreedy(epsilon=0.01,
                       const_epsilon=True,
                       half_life=200,
                       N_episodes_wo_decay=0)
    eg.set_half_life_for_N_episodes(Nepisodes=NUM_EPISODES,
                                    epsilon_final=0.00001)
    #eg = None

    ALPHA = 0.05
    if hasattr(ff, 'num_tiles'):
        alpha_obj = Alpha(alpha=ALPHA / ff.num_tiles)
    else:
        alpha_obj = Alpha(alpha=ALPHA)

    #alpha_obj.set_half_life_for_N_episodes( Nepisodes=NUM_EPISODES, alpha_final=0.001)

    ep_lenL = []
    for loop_counter in range(NUM_EPISODES):
        if loop_counter % 100 == 0:
            print()
            print('Loop:', loop_counter)

        sim.reset()
        x_init = -0.6 + 0.2 * random.random()

        episode = run_episode((x_init, 0),
Esempio n. 9
0
    def __init__(
        self,
        environment,
        learn_tracker=None,  # track progress of learning
        initial_Qsa=0.0,  # init non-terminal_set of V(s) (terminal_set=0.0)
        initial_action_value_coll=None,  # if input, use it.
        read_pickle_file='',
        save_pickle_file='',
        do_summ_print=True,
        show_last_change=True,
        pcent_progress_print=10,
        show_banner=True,
        gamma=0.9,
        iteration_prints=0,
        max_episode_steps=sys.maxsize,
        epsilon=0.1,  # can be constant or EpsilonGreedy object
        alpha=0.1):  # can be constant or Alpha object
        """
        ... GIVEN AN ENVIRONMENT ... 
        Use basic Dyna-Q algorithm to solve for STATE-ACTION VALUES, Q(s,a)
        
        Each action is forced to be a DETERMINISTIC action leading to one state and reward.
        (If the next state or reward changes, only the new values will be considered)
            
        attribute: self.action_value_coll is the ActionValueColl, Q(s,a) object
        
        A DETERMINISTIC policy can be created externally from the self.action_value_coll attribute.
        """
        self.environment = environment
        self.learn_tracker = learn_tracker
        self.save_pickle_file = save_pickle_file

        self.do_summ_print = do_summ_print
        self.show_last_change = show_last_change
        self.pcent_progress_print = pcent_progress_print

        self.gamma = gamma
        self.iteration_prints = iteration_prints
        self.max_episode_steps = max_episode_steps

        self.num_episodes = 0
        self.num_updates = 0

        # if input epsilon is a float, use it to create an EpsilonGreedy object
        if type(epsilon) == type(0.1):
            self.epsilon_obj = EpsilonGreedy(epsilon=epsilon,
                                             const_epsilon=True)
        else:
            self.epsilon_obj = epsilon

        # if input alpha is a float, use it to create an Alpha object
        if type(alpha) == type(0.1):
            self.alpha_obj = Alpha(alpha=alpha, const_alpha=True)
        else:
            self.alpha_obj = alpha

        # create the action_value_coll for the environment.
        if initial_action_value_coll is None:
            self.action_value_coll = ActionValueColl(environment,
                                                     init_val=initial_Qsa)
        else:
            self.action_value_coll = initial_action_value_coll

        if read_pickle_file:
            self.action_value_coll.init_from_pickle_file(read_pickle_file)

        # initialize the model that will build from experience
        # do not build full model description on Model init, states not visited
        #  by the RL portion will have no returns values.
        self.model = Model(environment, build_initial_model=False)
        #for s_hash, aD in self.action_value_coll.QsaD.items():
        #    for a_desc, Q in aD.items():
        #        self.model.add_action( s_hash, a_desc )

        if do_summ_print:
            print(
                '================== EPSILON GREEDY DEFINED AS ========================'
            )
            self.epsilon_obj.summ_print()

            print(
                '================== LEARNING RATE DEFINED AS ========================'
            )
            self.alpha_obj.summ_print()

        if show_banner:
            s = 'Starting a Maximum of %i Dyna-Q Epsilon Greedy Steps/Episode'%self.max_episode_steps +\
                '\nfor "%s" with Gamma = %g, Alpha = %g'%( environment.name, self.gamma, self.alpha_obj() )
            banner(s, banner_char='', leftMargin=0, just='center')
Esempio n. 10
0
class DynaQAgent(object):
    """
    DynaQ Agent.
    """
    def __init__(
        self,
        environment,
        learn_tracker=None,  # track progress of learning
        initial_Qsa=0.0,  # init non-terminal_set of V(s) (terminal_set=0.0)
        initial_action_value_coll=None,  # if input, use it.
        read_pickle_file='',
        save_pickle_file='',
        do_summ_print=True,
        show_last_change=True,
        pcent_progress_print=10,
        show_banner=True,
        gamma=0.9,
        iteration_prints=0,
        max_episode_steps=sys.maxsize,
        epsilon=0.1,  # can be constant or EpsilonGreedy object
        alpha=0.1):  # can be constant or Alpha object
        """
        ... GIVEN AN ENVIRONMENT ... 
        Use basic Dyna-Q algorithm to solve for STATE-ACTION VALUES, Q(s,a)
        
        Each action is forced to be a DETERMINISTIC action leading to one state and reward.
        (If the next state or reward changes, only the new values will be considered)
            
        attribute: self.action_value_coll is the ActionValueColl, Q(s,a) object
        
        A DETERMINISTIC policy can be created externally from the self.action_value_coll attribute.
        """
        self.environment = environment
        self.learn_tracker = learn_tracker
        self.save_pickle_file = save_pickle_file

        self.do_summ_print = do_summ_print
        self.show_last_change = show_last_change
        self.pcent_progress_print = pcent_progress_print

        self.gamma = gamma
        self.iteration_prints = iteration_prints
        self.max_episode_steps = max_episode_steps

        self.num_episodes = 0
        self.num_updates = 0

        # if input epsilon is a float, use it to create an EpsilonGreedy object
        if type(epsilon) == type(0.1):
            self.epsilon_obj = EpsilonGreedy(epsilon=epsilon,
                                             const_epsilon=True)
        else:
            self.epsilon_obj = epsilon

        # if input alpha is a float, use it to create an Alpha object
        if type(alpha) == type(0.1):
            self.alpha_obj = Alpha(alpha=alpha, const_alpha=True)
        else:
            self.alpha_obj = alpha

        # create the action_value_coll for the environment.
        if initial_action_value_coll is None:
            self.action_value_coll = ActionValueColl(environment,
                                                     init_val=initial_Qsa)
        else:
            self.action_value_coll = initial_action_value_coll

        if read_pickle_file:
            self.action_value_coll.init_from_pickle_file(read_pickle_file)

        # initialize the model that will build from experience
        # do not build full model description on Model init, states not visited
        #  by the RL portion will have no returns values.
        self.model = Model(environment, build_initial_model=False)
        #for s_hash, aD in self.action_value_coll.QsaD.items():
        #    for a_desc, Q in aD.items():
        #        self.model.add_action( s_hash, a_desc )

        if do_summ_print:
            print(
                '================== EPSILON GREEDY DEFINED AS ========================'
            )
            self.epsilon_obj.summ_print()

            print(
                '================== LEARNING RATE DEFINED AS ========================'
            )
            self.alpha_obj.summ_print()

        if show_banner:
            s = 'Starting a Maximum of %i Dyna-Q Epsilon Greedy Steps/Episode'%self.max_episode_steps +\
                '\nfor "%s" with Gamma = %g, Alpha = %g'%( environment.name, self.gamma, self.alpha_obj() )
            banner(s, banner_char='', leftMargin=0, just='center')

    def run_episode(self, start_state, Nplanning_loops=5, iter_sarsn=None):
        """
        Run a single episode of Dyna-Q algorithm
        If iter_sarsn is input, use it instead of action_value_coll calculations.
        (Note: the start_state should NOT be in terminal_set if iter_sarsn is input.)
        """

        # increment episode counters
        self.num_episodes += 1
        self.epsilon_obj.inc_N_episodes()
        self.alpha_obj.inc_N_episodes()

        if self.learn_tracker is not None:
            self.learn_tracker.add_new_episode()

        # do dyna_q loops until sn_hash in terminal_set
        s_hash = start_state

        n_steps_in_episode = 1
        while s_hash not in self.environment.terminal_set:

            if iter_sarsn is None:
                # get best epsilon-greedy action
                a_desc = self.action_value_coll.get_best_eps_greedy_action( \
                                                s_hash, epsgreedy_obj=self.epsilon_obj )
                # check for bad action value
                if a_desc is None:
                    print('break for a_desc==None at s_hash=%s' % str(s_hash))
                    break

                # get next state and reward
                sn_hash, reward = self.environment.get_action_snext_reward(
                    s_hash, a_desc)
            else:
                # retracing an existing episode
                s_hash, a_desc, reward, sn_hash = next(iter_sarsn)

            if self.learn_tracker is not None:
                self.learn_tracker.add_sarsn_to_current_episode(
                    s_hash, a_desc, reward, sn_hash)

            if sn_hash is None:
                print('break for sn_hash==None, #steps=', n_steps_in_episode,
                      ' s_hash=%s' % str(s_hash), ' a_desc=%s' % str(a_desc))
                break

            # do RL update of Q(s,a) value
            self.action_value_coll.qlearning_update(s_hash=s_hash,
                                                    a_desc=a_desc,
                                                    sn_hash=sn_hash,
                                                    alpha=self.alpha_obj(),
                                                    gamma=self.gamma,
                                                    reward=reward)
            self.num_updates += 1
            # give the above experience to the model
            self.model.add_action(s_hash, a_desc)

            # force DETERMINISTIC next state and reward.
            self.model.save_deterministic_action_results(s_hash,
                                                         a_desc,
                                                         sn_hash,
                                                         reward_val=reward)

            # do NOT use simple save_action_results... it allows NON-DETERMINISTIC next state.
            #self.model.save_action_results( s_hash, a_desc, sn_hash, reward_val=reward)

            # --------------------------------- Planning Loop ------------------------
            # make Nplanning_loops calls to model
            for n_plan in range(Nplanning_loops):
                s_model = self.model.get_random_state()
                #print(s_model, end=' ')

                # vanilla DynaQ
                a_model = self.model.get_random_action(s_model)

                #sn_model, r_model = self.environment.get_action_snext_reward( s_model, a_model )
                sn_model, r_model = self.model.get_sample_sn_r(
                    s_model, a_model)

                # update for the DynaQ  results.
                self.action_value_coll.qlearning_update(s_hash=s_model,
                                                        a_desc=a_model,
                                                        sn_hash=sn_model,
                                                        alpha=self.alpha_obj(),
                                                        gamma=self.gamma,
                                                        reward=r_model)
                self.num_updates += 1

            # keep a lid on the max number of episode steps.
            if n_steps_in_episode >= self.max_episode_steps:
                break

            # get ready for next loop
            n_steps_in_episode += 1
            s_hash = sn_hash

        #print(n_steps_in_episode, end=' ')

    def summ_print(self, long=True):  # pragma: no cover
        """Show State objects in sorted state_hash order."""
        print('___ Policy Evaluation Agent Summary ___')
        print('    Environment        = %s' % self.environment.name)
        print('    Number of Episodes = %g' % self.num_episodes)