Пример #1
0
def main():

    from environments import MultiArmedBandit
    import seaborn as sns
    sns.set(style="white", palette="muted", color_codes=True)

    T = 100
    env = MultiArmedBandit(T)
    pm = RescorlaWagner(env, env.d_x)

    obs = env.get_observations()
    hst = env.get_hidden_states()

    #use isres for optimisation of one dimensional functions
    from optmethods import isres
    bounds = {'ub': np.array([1.]), 'lb': np.array([0.])}
    f_opt, x_opt, res = isres(pm.get_free_energy, 1, 1e-6, 1e-8, bounds,
                              np.array([0.5]))
    print(f_opt, x_opt, res)

    post = pm.get_beliefs(alpha=x_opt)

    ax = obs.plot(y=r'$o_t$', style='go')
    ax = hst.plot(y=r'$p_t$', style='k--', ax=ax)

    ax = post.plot(y=r'$\mu_t$', style='r-', ax=ax)
    ax.legend(numpoints=1)

    #optimize preceptual surprise over multiple experimental blocks

    def total_fe(x, n_pars, blocks):
        fe = 0
        for b in blocks:
            pm = RescorlaWagner(b, b.d_x)
            fe += pm.get_free_energy(x)

        return fe

    n = 100
    T = 100
    exp_blocks = [MultiArmedBandit(T)] * 100

    fe = lambda x, p: total_fe(x, p, exp_blocks)
    f_opt, x_opt, res = isres(fe, 1, 1e-6, 1e-8, bounds, np.array([0.5]))
    print(f_opt / n, x_opt, res)

    post = pm.get_beliefs(alpha=x_opt)

    ax = obs.plot(y=r'$o_t$', style='go')
    ax = hst.plot(y=r'$p_t$', style='k--', ax=ax)

    ax = post.plot(y=r'$\mu_t$', style='r-', ax=ax)
    ax.legend(numpoints=1)
Пример #2
0
def main():
    
    from environments import MultiArmedBandit
    import seaborn as sns
    sns.set(style = "white", palette="muted", color_codes=True)
    
    T = 100
    env = MultiArmedBandit(T)
    pm = RescorlaWagner(env, env.d_x)
    
    obs = env.get_observations()
    hst = env.get_hidden_states()
    
    #use isres for optimisation of one dimensional functions
    from optmethods import isres
    bounds = {'ub': np.array([1.]), 'lb': np.array([0.])}
    f_opt, x_opt, res = isres( pm.get_free_energy, 1, 1e-6, 1e-8, bounds, np.array([0.5]) )
    print(f_opt, x_opt, res)
    
    post = pm.get_beliefs(alpha = x_opt)

    
    ax = obs.plot(y = r'$o_t$', style = 'go')
    ax = hst.plot(y = r'$p_t$', style = 'k--', ax = ax)
    
    ax = post.plot(y = r'$\mu_t$', style = 'r-', ax = ax)
    ax.legend(numpoints = 1)
    
    #optimize preceptual surprise over multiple experimental blocks
    
    def total_fe(x, n_pars, blocks):
        fe = 0
        for b in blocks:
            pm = RescorlaWagner(b, b.d_x)
            fe += pm.get_free_energy(x)
            
        return fe
    
    n = 100
    T = 100
    exp_blocks = [MultiArmedBandit(T)]*100 
        
    fe = lambda x,p: total_fe(x, p, exp_blocks)
    f_opt, x_opt, res = isres( fe, 1, 1e-6, 1e-8, bounds, np.array([0.5]) )
    print(f_opt/n, x_opt, res)
    
    post = pm.get_beliefs(alpha = x_opt)
    
    ax = obs.plot(y = r'$o_t$', style = 'go')
    ax = hst.plot(y = r'$p_t$', style = 'k--', ax = ax)
    
    ax = post.plot(y = r'$\mu_t$', style = 'r-', ax = ax)
    ax.legend(numpoints = 1)