Experiment functions

Here we describe the Experiment class. This class, and its associated methods, allow accessing and storing policy parameters, logging data, and nesting experiments.

Note that the Experiment class is used in two ways in StreamingBandit:

  1. The getaction and setreward code of an experiment are both executed within the scope of the Experiment class. Hence, here you can use self.METHOD to use the methods described below.

  2. Within an experiment, you can instantiate a new experiment by referring to its experiment id (thus by using exp_nested = Experiment(exp_id = id1))). You can subsequently execute the getaction and setreward code of this experiment. This functionality allows nesting of multiple experiments.

class core.experiment.Experiment(exp_id, key='notUsedForLoopBack')

Class that organizes experiments.

Variables
  • exp_id (int) – The exp_id that is tied to the experiment and the database.

  • key (string) – The key that is tied to the experiment, to validate the user.

get_theta(key=None, value=None, name='_theta', all_float=False)

Get the theta (parameters) from the database.

Parameters
  • key (string) – The key with which the theta will be associated. If only a key is given, all the thetas that belong to that key will be returned. Typically a key distinguishes experiments from each other.

  • value (string) – The value with which the theta will be assiocated. Typically the value distinguishes the different versions within an experiment. If no value is given, all thetas belonging to the key/experiment will be returned.

  • name (string) – The name of the parameters. Typically theta is okay.

  • all_float (bool) – If all_float is True, it will try to convert every value within the theta to a float.

Returns

A dictionary with the parameter set.

log_data(value)

Manual logging that is used in the get_action and set_reward codes.

Parameters

value (dict) – The value that needs to be logged. Since MongoDB is used, a dictionary is needed.

Returns

True if executed correctly.

run_action_code(context, action=None)

Takes get_action code from Redis and executes it.

Parameters
  • context (dict) – Context is a dictionary with the context for the getAction algorithm

  • action (dict) – Action is pre-created such that the exec(code) function can return an action dict for this function (this is because of the behavior of Python).

Returns

A dict of action of which the content is determined by the get_action code.

run_context_code(context=None)

Takes get_context code from Redis and executes it.

Parameters

context (dict) – Context is pre-created such that the exec(code) function can return an context dict for this function (this is because of the behavior of Python).

Returns

A dict of context of which the content is determined by the get_context code.

run_get_reward_code(context, action, reward=None)

Takes get_reward code from Redis and executes it.

Parameters
  • context (dict) – The context that may be needed for the algorithm.

  • action (string) – The action that is needed for the algorithm. Is actually free of type, but generally a string is used. but must be specified by used algorithm.

  • reward (dict) – Reward is pre-created such that the exec(code) function can return an reward dict for this function (this is because of the behavior of Python).

Returns

True if executed correctly.

run_reward_code(context, action, reward)

Takes set_reward code from Redis and executes it.

Parameters
  • context (dict) – The context that may be needed for the algorithm.

  • action (string) – The action that is needed for the algorithm. Is actually free of type, but generally a string is used.

  • reward (int) – Generally an int, in 0 or 1. Can be of other type, but must be specified by used algorithm.

Returns

True if executed correctly.

set_theta(thetas, key=None, value=None, name='_theta')

Set the new theta (parameters) in the database.

Parameters
  • thetas (dict) – The thetas that will eb stored. Typically a dictionary or a class of base.py. The function will check if it is a class and whether it has a get_dict function. It is okay to give a class with these conditions - it will call the get_dict function and store the dictionary.

  • key (string) – The key with which the theta will be associated. If only a key is given, all the thetas that belong to that key will be returned. Typically a key distinguishes experiments from each other.

  • value (string) – The value with which the theta will be assiocated. Typically the value distinguishes the different versions within an experiment. If no value is given, all thetas belonging to the key/experiment will be returned.

  • name (string) – The name of the parameter set.