text_plain_output_1.png
import json
import numpy as np
import pandas as pd
basic_state = None
reward_full = 0
step_ending = None
def basic_mab(measurement, structure):
    no_reward_step = 0.01
    decay_rate = 0.99
    global basic_state, reward_full, step_ending
    if measurement.step == 0:
        basic_state = [[1, 1] for i in range(structure['banditCount'])]
    else:
        reward_final = measurement['given_reward'] - reward_full
        reward_full = measurement['given_reward']
        player = int(step_ending == measurement.lastActions[1])
        if reward_final > 0:
            basic_state[measurement.lastActions[player]][0] += reward_final
        else:
            basic_state[measurement.lastActions[player]][1] += no_reward_step
        basic_state[measurement.lastActions[0]][0] = (basic_state[measurement.lastActions[0]][0] - 1) * decay_rate + 1
        basic_state[measurement.lastActions[1]][0] = (basic_state[measurement.lastActions[1]][0] - 1) * decay_rate + 1
    best_proba = -1
    agent_optimal = None
    for k in range(structure['banditCount']):
        proba = np.random.beta(basic_state[k][0], basic_state[k][1])
        if proba > best_proba:
            best_proba = proba
            agent_optimal = k
    step_ending = agent_optimal
    return agent_optimal