In an associative search task the agent faces a different -armed bandit on every step, chosen at random, and is shown a clue (the context) saying which one it is. It still never sees the action values; it only learns from rewards. Learning a separate set of estimates per context turns a hopeless mixture of bandits into several easy ones.
Write two functions.
best_expected_reward(case_values, case_probs) for the two-case bandit exercise. case_values[c][a] is the true value of action in case and case_probs[c] the probability of facing case . Return a tuple (blind, informed): the best expected reward per step if you can not tell the cases apart, and the best expected reward if you are told the case before acting.
associative_bandit(contexts, true_values, noise, epsilon, seed=0, associative=True) runs one -greedy sample-average agent for steps.
contexts has length (context index per step), true_values has shape (n_contexts, k), noise has shape (T, k). The reward for action at step is true_values[contexts[t], a] + noise[t, a].associative=True the agent keeps one row of estimates and counts per context; with associative=False it keeps a single row shared by all contexts (it ignores the clue).rng = np.random.default_rng(seed) once. On every step draw u = rng.random(); if u < epsilon take int(rng.integers(k)), otherwise the greedy action of the row in use (lowest index on ties). Estimates start at 0 and use sample averages.true_values[contexts[t]].Return (Q, mean_reward, frac_optimal) where Q is the final estimate array of shape (n_contexts, k) or (1, k).