### Set up virtualenv and install requirements Source: https://smpybandits.github.io/How_to_run_the_code.html Shows the process of creating a virtualenv, activating it, checking pip, and installing requirements. The output shows the installation of packages. ```bash $ virtualenv . Using base prefix '/usr' New python executable in /your/path/to/SMPyBandits/bin/python3 Also creating executable in /your/path/to/SMPyBandits/bin/python Installing setuptools, pip, wheel...done. $ source bin/activate # in bash, use activate.csh or activate.fish if needed $ type pip # just to check pip is /your/path/to/SMPyBandits/bin/pip $ pip install -r requirements.txt Collecting numpy (from -r requirements.txt (line 5)) ... Installing collected packages: numpy, scipy, cycler, pytz, python-dateutil, matplotlib, joblib, pandas, seaborn, tqdm, sphinx-rtd-theme, commonmark, docutils, recommonmark Successfully installed commonmark-0.5.4 cycler-0.10.0 docutils-0.13.1 joblib-0.11 matplotlib-2.0.0 numpy-1.12.1 pandas-0.19.2 python-dateutil-2.6.0 pytz-2016.10 recommonmark-0.4.0 scipy-0.19.0 seaborn-0.7.1 sphinx-rtd-theme-0.2.4 tqdm-4.11.2 ``` -------------------------------- ### Instantiate MusicalChairNoSensing and configure multi-player setups Source: https://smpybandits.github.io/_modules/Policies/MusicalChairNoSensing.html Shows how to instantiate a single MusicalChairNoSensing player and how to configure multi-player setups using either the Selfish wrapper or a list comprehension. The examples are from the class docstring and assume nbPlayers, nbArms, and horizon are defined. ```python >>> nbPlayers, nbArms, horizon = 3, 9, 10000 >>> player1 = MusicalChairNoSensing(nbPlayers, nbArms, horizon) ``` ```python >>> configuration["players"] = Selfish(NB_PLAYERS, MusicalChairNoSensing, nbArms, nbPlayers=nbPlayers, horizon=horizon).children ``` ```python >>> configuration["players"] = [ MusicalChairNoSensing(nbPlayers=nbPlayers, nbArms=nbArms, horizon=horizon) for _ in range(NB_PLAYERS) ] ``` -------------------------------- ### Use Makefile to install and run Source: https://smpybandits.github.io/How_to_run_the_code.html Use the provided Makefile to install requirements and run the main.py script. ```bash make install # install the requirements make multiplayers # run and log the main.py script ``` -------------------------------- ### Experiment Environment and Policy Initialization Source: https://smpybandits.github.io/notebooks/Unsupervised_Learning_for_Bandit_problem.html Logs showing the evaluation environment setup and the configuration of various bandit policies including UnsupervisedLearning, UCB, Thompson, and klUCB. ```text Evaluating environment: MAB(nbArms: 9, arms: [G(0.3, 0.25), G(0.35, 0.25), G(0.4, 0.25), G(0.45, 0.25), G(0.5, 0.25), G(0.55, 0.25), G(0.6, 0.25), G(0.65, 0.25), G(0.7, 0.25)], minArm: 0.3, maxArm: 0.7) - Adding policy #1 = {'archtype': , 'params': {'bandwidth': 0.2, 'meanOf': 200, 'T_0': 100, 'kernel': 'gaussian', 'estimator': , 'fit_every': 1000}} ... Creating this policy from a dictionnary 'self.cfg['policies'][0]' = {'archtype': , 'params': {'bandwidth': 0.2, 'meanOf': 200, 'T_0': 100, 'kernel': 'gaussian', 'estimator': , 'fit_every': 1000}} ... - Adding policy #2 = {'archtype': , 'params': {'estimator': , 'fit_every': 1000, 'meanOf': 200, 'T_0': 100}} ... Creating this policy from a dictionnary 'self.cfg['policies'][1]' = {'archtype': , 'params': {'estimator': , 'fit_every': 1000, 'meanOf': 200, 'T_0': 100}} ... - Adding policy #3 = {'archtype': , 'params': {}} ... Creating this policy from a dictionnary 'self.cfg['policies'][2]' = {'archtype': , 'params': {}} ... - Adding policy #4 = {'archtype': , 'params': {}} ... Creating this policy from a dictionnary 'self.cfg['policies'][3]' = {'archtype': , 'params': {}} ... - Adding policy #5 = {'archtype': , 'params': {'klucb': }} ... Creating this policy from a dictionnary 'self.cfg['policies'][4]' = {'archtype': , 'params': {'klucb': }} ... - Evaluating policy #1/5: UnsupervisedLearning(KernelDensity, $T_0=100$, $T_1=1e+03$, $M=200$) ... ``` -------------------------------- ### SimpleGaussianKernel Policy Evaluation Source: https://smpybandits.github.io/notebooks/Unsupervised_Learning_for_Bandit_problem.html Evaluation start log for the SimpleGaussianKernel policy. ```text - Evaluating policy #2/5: UnsupervisedLearning(SimpleGaussianKernel, $T_0=100$, $T_1=1e+03$, $M=200$) ... ``` -------------------------------- ### walk examples Source: https://smpybandits.github.io/_modules/Environment/pykov.html Examples for the walk method. The first call generates a 10-step random walk with default start and stop; the second call starts at 'B' and stops when 'B' is reached. ```python >>> T = pykov.Chain({('A','B'): .3, ('A','A'): .7, ('B','A'): 1.}) >>> T.walk(10) ['B', 'A', 'B', 'A', 'A', 'B', 'A', 'A', 'A', 'B', 'A'] >>> T.walk(10,'B','B') ['B', 'A', 'A', 'A', 'A', 'A', 'B'] ``` -------------------------------- ### Example: rhoRandSticky usage with UCB Source: https://smpybandits.github.io/docs/PoliciesMultiPlayers.rhoRandSticky.html Shows how to instantiate rhoRandSticky with UCB players, set a stickyTime, and get initial choices from the children. The example uses a fixed random seed for reproducibility. ```python >>> from Policies import * >>> import random; random.seed(0); import numpy as np; np.random.seed(0) >>> nbArms = 17 >>> nbPlayers = 6 >>> stickyTime = 5 >>> s = rhoRandSticky(nbPlayers, nbArms, UCB, stickyTime=stickyTime) >>> [ child.choice() for child in s.children ] [12, 15, 0, 3, 3, 7] >>> [ child.choice() for child in s.children ] [9, 4, 6, 12, 1, 6] ``` -------------------------------- ### Main Server Initialization Source: https://smpybandits.github.io/_modules/policy_server.html Constructs the learning policy from a JSON configuration and starts the server. ```python [docs]def main(args): """ Take args, construct the learning policy and starts the server. """ host = str(args['--host']) port = int(args['--port']) try: means = str(args['--means']) means = means.replace('[', '').replace(']', '') means = [ float(m) for m in means.split(',') ] means = np.asarray(means, dtype=float) except ValueError: means = None json_configuration = args[''] configuration = read_configuration_policy(json_configuration) nbArms = int(configuration['nbArms']) # try to map strings in the dictionary to variables, e.g., policies params = configuration['params'] transform_str(params) print("Params =", params) policy = globals()[configuration['archtype']](nbArms, **params) print("Using the policy", policy) return server(policy, host, port, means=means) if __name__ == '__main__': arguments = docopt(__doc__, version=version) # print("arguments =", arguments) # DEBUG main(arguments) ``` -------------------------------- ### Import MAB environment Source: https://smpybandits.github.io/_sources/notebooks/Easily_creating_MAB_problems.ipynb.txt Initial setup to import the MAB class from the Environment package after ensuring SMPyBandits is installed. ```python Requirement already satisfied: SMPyBandits in ./venv3/lib/python3.6/site-packages (0.9.4) Requirement already satisfied: watermark in ./venv3/lib/python3.6/site-packages (1.7.0) Requirement already satisfied: scikit-learn in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (0.20.0) Requirement already satisfied: scikit-optimize in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (0.5.2) Requirement already satisfied: scipy>0.9 in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (1.1.0) Requirement already satisfied: numpy in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (1.15.4) Requirement already satisfied: matplotlib>=2 in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (3.0.2) Requirement already satisfied: seaborn in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (0.9.0) Requirement already satisfied: joblib in ./venv3/lib/python3.6/site-packages (from SMPyBandits) (0.13.0) Requirement already satisfied: ipython in ./venv3/lib/python3.6/site-packages (from watermark) (7.1.1) ``` -------------------------------- ### Inspect kde.sample method Source: https://smpybandits.github.io/notebooks/Unsupervised_Learning_for_Bandit_problem.html Displays the signature and documentation for the kde.sample method. ```python kde.sample? ``` ```text Signature: kde.sample(n_samples=1, random_state=None) Docstring: Generate random samples from the model. Currently, this is implemented only for gaussian and tophat kernels. Parameters ---------- n_samples : int, optional Number of samples to generate. Defaults to 1. random_state : RandomState or an int seed (0 by default) A random number generator instance. Returns ------- X : array_like, shape (n_samples, n_features) List of samples. File: /usr/local/lib/python3.5/dist-packages/sklearn/neighbors/kde.py Type: method ``` -------------------------------- ### Import and setup for performance comparison Source: https://smpybandits.github.io/docs/Policies.kullback_cython.html Illustrates the import of the C and Cython modules for performance comparison. The code is a placeholder for illustration purposes. ```python >>> r = np.random.random >>> import kullback_c; import kullback_cython # fake, just for illustration ``` -------------------------------- ### nrows_ncols usage example Source: https://smpybandits.github.io/docs/Environment.plotsettings.html Shows how to use nrows_ncols to get the number of rows and columns for subplots for N from 1 to 21, printing the result. The output demonstrates the grid sizes for each N. ```python >>> for N in range(1, 22): ... nrows, ncols = nrows_ncols(N) ... print("For N = {:>2}, {} rows and {} cols are enough.".format(N, nrows, ncols)) For N = 1, 1 rows and 1 cols are enough. For N = 2, 2 rows and 1 cols are enough. For N = 3, 2 rows and 2 cols are enough. For N = 4, 2 rows and 2 cols are enough. For N = 5, 3 rows and 2 cols are enough. For N = 6, 3 rows and 2 cols are enough. For N = 7, 3 rows and 3 cols are enough. For N = 8, 3 rows and 3 cols are enough. For N = 9, 3 rows and 3 cols are enough. For N = 10, 4 rows and 3 cols are enough. For N = 11, 4 rows and 3 cols are enough. For N = 12, 4 rows and 3 cols are enough. For N = 13, 4 rows and 4 cols are enough. For N = 14, 4 rows and 4 cols are enough. For N = 15, 4 rows and 4 cols are enough. For N = 16, 4 rows and 4 cols are enough. For N = 17, 5 rows and 4 cols are enough. For N = 18, 5 rows and 4 cols are enough. For N = 19, 5 rows and 4 cols are enough. For N = 20, 5 rows and 4 cols are enough. For N = 21, 5 rows and 5 cols are enough. ``` -------------------------------- ### Main execution function Source: https://smpybandits.github.io/_modules/env_client.html Constructs the learning policy and starts the server based on provided command-line arguments. ```python [docs]def main(arguments): """ Take arguments, construct the learning policy and starts the server. """ host = arguments['--host'] port = int(arguments['--port']) speed = float(arguments['--speed']) / 1000.0 is_markovian = arguments['--markovian'] is_dynamic = arguments['--dynamic'] json_configuration = arguments[''] configuration = read_configuration_env(json_configuration) transform_str(configuration) if is_dynamic: env = ChangingAtEachRepMAB(configuration) elif is_markovian: env = MarkovianMAB(configuration) else: env = MAB(configuration) print("Using the environment: ", env) # DEBUG print("Emitting regularly every", speed, "seconds.") # DEBUG return client(env, host, port, speed) if __name__ == '__main__': arguments = docopt(__doc__, version=version) # print("arguments =", arguments) # DEBUG main(arguments) ``` -------------------------------- ### Install and load watermark extension Source: https://smpybandits.github.io/notebooks/Exploring_different_doubling_tricks_for_different_kinds_of_regret_bounds.html Installs the watermark package and loads the IPython extension to display environment information. The output shows the installed versions and system details. ```python !pip install watermark %load_ext watermark %watermark -v -m -p numpy,matplotlib,seaborn -a "Lilian Besson and Emilie Kaufmann" ``` ```text Requirement already satisfied: watermark in ./venv3/lib/python3.6/site-packages (1.7.0) Requirement already satisfied: ipython in ./venv3/lib/python3.6/site-packages (from watermark) (7.1.1) Requirement already satisfied: pexpect; sys_platform != "win32" in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (4.6.0) Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (2.0.7) Requirement already satisfied: setuptools>=18.5 in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (40.6.2) Requirement already satisfied: pygments in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (2.2.0) Requirement already satisfied: pickleshare in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (0.7.5) Requirement already satisfied: decorator in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (4.3.0) Requirement already satisfied: jedi>=0.10 in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (0.13.1) Requirement already satisfied: traitlets>=4.2 in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (4.3.2) Requirement already satisfied: backcall in ./venv3/lib/python3.6/site-packages (from ipython->watermark) (0.1.0) Requirement already satisfied: ptyprocess>=0.5 in ./venv3/lib/python3.6/site-packages (from pexpect; sys_platform != "win32"->ipython->watermark) (0.6.0) Requirement already satisfied: six>=1.9.0 in ./venv3/lib/python3.6/site-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython->watermark) (1.11.0) Requirement already satisfied: wcwidth in ./venv3/lib/python3.6/site-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython->watermark) (0.1.7) Requirement already satisfied: parso>=0.3.0 in ./venv3/lib/python3.6/site-packages (from jedi>=0.10->ipython->watermark) (0.3.1) Requirement already satisfied: ipython-genutils in ./venv3/lib/python3.6/site-packages (from traitlets>=4.2->ipython->watermark) (0.2.0) Lilian Besson and Emilie Kaufmann CPython 3.6.6 IPython 7.1.1 numpy 1.15.4 matplotlib 3.0.2 seaborn 0.9.0 compiler : GCC 8.0.1 20180414 (experimental) [trunk revision 259383 system : Linux release : 4.15.0-38-generic machine : x86_64 processor : x86_64 CPU cores : 4 interpreter: 64bit ``` -------------------------------- ### Build Cython extension Source: https://smpybandits.github.io/docs/Policies.Experimentals.UCBcython.html Run this command using the setup.py script to build the Cython extension in-place. ```bash $ python setup.py build_ext --inplace ``` -------------------------------- ### Install Jupyter Notebook and IPython with pip Source: https://smpybandits.github.io/_sources/notebooks/README.md.txt Installs Jupyter Notebook and IPython using pip. Requires sudo privileges. After installation, the jupyter-notebook or jupyter command should be available in your PATH. ```bash sudo pip install jupyter ipython ``` -------------------------------- ### Initialize MCTopMOld Source: https://smpybandits.github.io/_modules/PoliciesMultiPlayers/RandTopM.html Example usage of the MCTopMOld policy, demonstrating initialization and choice selection for multiple players. ```python >>> from Policies import * >>> import random; random.seed(0); import numpy as np; np.random.seed(0) >>> nbArms = 17 >>> nbPlayers = 6 >>> s = MCTopMOld(nbPlayers, nbArms, UCB) >>> [ child.choice() for child in s.children ] [12, 15, 0, 3, 3, 7] ``` -------------------------------- ### new_rank example with forceChange=True Source: https://smpybandits.github.io/docs/PoliciesMultiPlayers.rhoRandALOHA.html Example of the new_rank function with forceChange=True, ensuring the new rank is different from the current one. The output is deterministic due to the seed set in the previous example. ```python >>> [ new_rank(1, 8, True) for _ in range(10) ] [2, 2, 6, 8, 9, 2, 6, 7, 6, 4] >>> [ new_rank(5, 8, True) for _ in range(10) ] [9, 8, 8, 9, 1, 9, 1, 2, 7, 1] ``` -------------------------------- ### Build Cython extension with setup.py Source: https://smpybandits.github.io/docs/Policies.Experimentals.UCBoost_faster_cython.html Compiles the Cython extension in-place using the setup.py script. Run this before importing the UCBoost_faster_cython module. ```bash $ python setup.py build_ext --inplace ``` -------------------------------- ### Install SMPyBandits dependencies Source: https://smpybandits.github.io/NonStationaryBandits.html Clone the repository and install the necessary requirements using the Makefile. ```bash # if not already installed, otherwise update with 'git pull' git clone https://github.com/SMPyBandits/SMPyBandits/ cd SMPyBandits make install # install the requirements ONLY ONCE ``` -------------------------------- ### mean_fairness doctest examples Source: https://smpybandits.github.io/_modules/Environment/fairnessMeasures.html Doctest examples for the mean_fairness function. The examples use seeded random arrays and show the fairness index for the original, more spread out (squared), and less spread out (square root) versions of the data. ```python >>> import numpy.random as rn; rn.seed(1) # for reproductibility >>> X = np.cumsum(rn.rand(10, 1000)) >>> mean_fairness(X) # doctest: +ELLIPSIS 0.607... >>> mean_fairness(X ** 2) # More spreadout # doctest: +ELLIPSIS 0.678... >>> mean_fairness(np.sqrt(np.abs(X))) # Less spreadout # doctest: +ELLIPSIS 0.523... >>> rn.seed(2) # for reproductibility >>> X = np.cumsum(rn.randint(0, 10, (10, 100))) >>> mean_fairness(X) # doctest: +ELLIPSIS 0.605... >>> mean_fairness(X ** 2) # More spreadout # doctest: +ELLIPSIS 0.834... >>> mean_fairness(np.sqrt(np.abs(X))) # Less spreadout # doctest: +ELLIPSIS 0.509... ``` -------------------------------- ### klUCBPlusPlus.__init__(nbArms, horizon=None, tolerance=0.0001, klucb=CPUDispatcher(), c=1.0, lower=0.0, amplitude=1.0) Source: https://smpybandits.github.io/docs/Policies.klUCBPlusPlus.html Initializes the klUCBPlusPlus policy. This is a new generic index policy. ```APIDOC ## klUCBPlusPlus.__init__(nbArms, horizon=None, tolerance=0.0001, klucb=CPUDispatcher(), c=1.0, lower=0.0, amplitude=1.0) ### Description Initializes the klUCBPlusPlus policy. This is a new generic index policy. ### Method Constructor ### Signature `klUCBPlusPlus.__init__(nbArms, horizon=None, tolerance=0.0001, klucb=CPUDispatcher(), c=1.0, lower=0.0, amplitude=1.0)` ### Parameters - **nbArms** (int) - Required - The number of arms. - **horizon** (int, optional) - Optional - Known horizon of the experiment. Default is None. - **tolerance** (float, optional) - Optional - Tolerance parameter. Default is 0.0001. - **klucb** (callable, optional) - Optional - Function for computing the KL upper confidence bound. Default is CPUDispatcher(). - **c** (float, optional) - Optional - Exploration parameter. Default is 1.0. - **lower** (float, optional) - Optional - Lower value of the rewards. Default is 0.0. - **amplitude** (float, optional) - Optional - Known amplitude of the rewards. Default is 1.0. ``` -------------------------------- ### rajjain_fairness doctest examples Source: https://smpybandits.github.io/_modules/Environment/fairnessMeasures.html Doctest examples for the rajjain_fairness function. The examples use a seeded random array and show the fairness index for the original, more spread out (squared), and less spread out (square root) versions of the data. ```python >>> rn.seed(2) # for reproductibility >>> X = np.cumsum(rn.randint(0, 10, (10, 100))) >>> rajjain_fairness(X) # doctest: +ELLIPSIS 0.246... >>> rajjain_fairness(X ** 2) # More spreadout # doctest: +ELLIPSIS 0.917... >>> rajjain_fairness(np.sqrt(np.abs(X))) # Less spreadout # doctest: +ELLIPSIS 0.107... ``` -------------------------------- ### TrekkingTSN __init__ method with doctest examples Source: https://smpybandits.github.io/_modules/Policies/TrekkingTSN.html Initializes the TrekkingTSN policy with parameters theta, epsilon, delta, lower, and amplitude. The docstring includes doctest examples for creating a single player and a multi-player configuration. ```python def __init__(self, nbArms, theta=0.01, epsilon=0.1, delta=0.05, lower=0., amplitude=1. ): # Named argument to give them in any order """ - nbArms: number of arms, Example: >>> nbArms = 8 >>> theta, epsilon, delta = 0.01, 0.1, 0.05 >>> player1 = TrekkingTSN(nbArms, theta=theta, epsilon=epsilon, delta=delta) For multi-players use: >>> configuration["players"] = Selfish(NB_PLAYERS, TrekkingTSN, nbArms, theta=theta, epsilon=epsilon, delta=delta).children """ super(TrekkingTSN, self).__init__(nbArms, lower=lower, amplitude=amplitude) self.state = State.NotStarted #: Current state # compute times T_RH, T_SH, T_TR = special_times(nbArms=nbArms, theta=theta, epsilon=epsilon, delta=delta) self.theta = theta #: Parameter :math:`\theta`. self.epsilon = epsilon #: Parameter :math:`\epsilon`. self.delta = delta #: Parameter :math:`\delta`. # Store parameters self.T_RH = T_RH #: Parameter :math:`T_{RH}` computed from :func:`special_times` self.T_SH = T_SH #: Parameter :math:`T_{SH}` computed from :func:`special_times` self.T_CC = T_RH + T_SH #: Parameter :math:`T_{CC} = T_{RH} + T_{SH}` self.T_TR = T_TR #: Parameter :math:`T_{TR}` computed from :func:`special_times` # Internal memory self.last_was_successful = False #: That's the l of the paper self.last_choice = None #: Keep memory of the last choice for CC phase self.cumulatedRewards = np.zeros(nbArms) #: That's the V_n of the paper self.nbObservations = np.zeros(nbArms, dtype=int) #: That's the S_n of the paper self.J = -1 self.lock_channel = False #: That's the L of the paper self.Y = np.zeros(nbArms, dtype=int) # That's the Yi of the paper self.M = np.zeros(nbArms) # That's the Mj of the paper self.index_sort = None # Implementation details self.t = -1 #: Internal times ``` -------------------------------- ### Explore Notebooks with Nix Source: https://smpybandits.github.io/README.html Runs a specific example notebook directly from the master branch using a Nix-enabled machine. ```bash $ nix-shell https://github.com/SMPYBandits/SMPyBandits/archive/master.tar.gz --run 'jupyter-notebook $EXAMPLE_NOTEBOOKS/Example_of_a_small_Multi-Player_Simulation__with_Centralized_Algorithms.ipynb' ``` -------------------------------- ### Install SMPyBandits via Pip Source: https://smpybandits.github.io/README.html Installs the package directly from the GitHub repository into a virtual environment. ```bash cd /tmp/ ; mkdir SMPyBandits ; cd SMPyBandits/ virtualenv venv . venv/bin/activate type pip # check it is /tmp/SMPyBandits/venv/bin/pip type python # check it is /tmp/SMPyBandits/venv/bin/python pip install git+https://github.com/SMPyBandits/SMPyBandits.git#egg=SMPyBandits[full] ```