def select(name): if name.startswith("linear{"): args = parse_args(name) n = args["n"] variance = args["var"] real_means = linear_means(n) bandit = linear_bandit(n, variance) return (real_means, bandit) elif name.startswith( "polynomial{"): args = parse_args(name) n = args["n"] variance = args["var"] real_means = polynomial_means(n) bandit = polynomial_bandit(n, variance) return (real_means, bandit) elif name.startswith("linear-linvar{"): args = parse_args(name) n = args["n"] max_variance = args["max_var"] min_variance = args["min_var"] real_means = linear_means(n) bandit = gj_linvar.linear_bandit(n, max_variance, min_variance) return (real_means, bandit) elif name.startswith( "polynomial-linvar{"): args = parse_args(name) n = args["n"] max_variance = args["max_var"] min_variance = args["min_var"] real_means = polynomial_means() bandit = gj_linvar.polynomial_bandit(n, max_variance, min_variance) return (real_means, bandit) elif name.startswith( "captions{"): args = parse_args(name) n = args["n"] real_means = captions_means(n) bandit =captions_bandit(n) return (real_means, bandit) elif name.startswith( "poisson_olivier{"): args = parse_args(name) n = args["n"] real_means = poisson_oli_means(n) bandit = poisson_oli_bandit(n) return (real_means, bandit) elif name.startswith( "poisson-exp{"): real_means = poisson_exp_means() bandit = poisson_exp_bandit() return (real_means, bandit) elif name.startswith( "scaled_gaussian{"): args = parse_args(name) n = args["n"] real_means = scaled_gaussian_means()[:n] bandit = scaled_gaussian_bandit(n) return (real_means, bandit) else: choices = \ ["linear{n,var}", "polynomial{n,var}", \ "captions{n}", "poisson-olivier{n}", "scaled_gaussian{n}"] raise ValueError("Invalid environment \"" + name + "\", " + \ "choose from " + \ "[" + ", ".join(choices) + "]")
def select_means(arm_n, bandit, R_0): if bandit == "linear": means_value = linear_means(arm_n) elif bandit == "sparse": means_value = sparse_means(arm_n) elif bandit == "capition": means_value = captions_means() elif bandit == "influenza": means_value = preventive_means(R_0) else: means_value = polynomial_means(arm_n) return means_value
import matplotlib.pyplot as plt import numpy as np from algorithms.uniform import Uniform from environments.gaussian_jun import linear_bandit, linear_means n = 200 steps = 150000 m = 10 arms_n = 1000 variance = 0.25 # lin_bandits = [linear_bandit(arms_n, variance) for i in range(steps)] linearmeans = linear_means(arms_n) J_t_result = np.zeros((2,steps)) for i in range(n): #lin_bandits = linear_bandit(arms_n, variance, steps) lin_bandits = linear_bandit(arms_n, variance) J_t_n = np.zeros((2,steps)) algo = Uniform(lin_bandits, m) for t in range(steps): J_t = algo.step(t) top_m = [linearmeans[int(i)] for i in J_t] J_t_n[0][t] = np.min(top_m) J_t_n[1][t] = np.sum(top_m) J_t_result = J_t_result+J_t_n result = J_t_result/n plt.figure(1) plt.title('Linear m = 10', fontweight='bold')
def linear_bandit(n, max_variance, min_variance): means = linear_means(n) variances_ = variances(n, max_variance, min_variance) return create_bandit(means, variances_)