.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "getting_started/auto_examples/plot_HOO_Garland.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_getting_started_auto_examples_plot_HOO_Garland.py: 1-D Example ================ In this example, we run the T_HOO algorithm on the Garland objective. First, import all the functions needed .. GENERATED FROM PYTHON SOURCE LINES 7-19 .. code-block:: default from PyXAB.synthetic_obj.Garland import Garland # the objective from PyXAB.algos.HOO import T_HOO # the algorithm from PyXAB.partition.BinaryPartition import BinaryPartition # the partition # the other useful packages/functions import numpy as np from PyXAB.utils.plot import plot_regret .. GENERATED FROM PYTHON SOURCE LINES 20-21 Define the number of rounds, the target, the domain, the partition, and the algorithm for the learning process .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: default T = 1000 # the number of rounds is 1000 target = Garland() # the objective to optimize is Garland domain = [[0, 1]] # the domain is [[0, 1]] partition = BinaryPartition # the partition chosen is BinaryPartition algo = T_HOO(rounds=1000, domain=domain, partition=partition) # the algorithm is T_HOO .. GENERATED FROM PYTHON SOURCE LINES 29-30 To plot the regret, we can initialize the cumulative regret and the cumulative regret list .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: default cumulative_regret = 0 cumulative_regret_list = [] .. GENERATED FROM PYTHON SOURCE LINES 36-39 In each iteration of the learning process, the algorithm calls the ``pull(t)`` function to obtain a point, and then the reward for the point is returned to the algorithm by calling ``receive_reward(t, reward)``. For a stochastic learning process, uniform noise is added to the reward. .. GENERATED FROM PYTHON SOURCE LINES 39-59 .. code-block:: default for t in range(1, T+1): point = algo.pull(t) reward = target.f(point) + np.random.uniform(-0.1, 0.1) # uniform noise algo.receive_reward(t, reward) # the following lines are for the regret inst_regret = target.fmax - target.f(point) cumulative_regret += inst_regret cumulative_regret_list.append(cumulative_regret) # plot the regret plot_regret(np.array(cumulative_regret_list), name='HOO') .. image-sg:: /getting_started/auto_examples/images/sphx_glr_plot_HOO_Garland_001.png :alt: plot HOO Garland :srcset: /getting_started/auto_examples/images/sphx_glr_plot_HOO_Garland_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-61 The following lines of code are only for creating thumbnails and do not need to be used .. GENERATED FROM PYTHON SOURCE LINES 61-72 .. code-block:: default # sphinx_gallery_thumbnail_number = 2 import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) x = np.linspace(domain[0][0], domain[0][1], 1000) z = x * (1-x) * (4 - np.sqrt(np.abs(np.sin(60 * x)))) ax.plot(x, z, alpha=0.9) fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) .. image-sg:: /getting_started/auto_examples/images/sphx_glr_plot_HOO_Garland_002.png :alt: plot HOO Garland :srcset: /getting_started/auto_examples/images/sphx_glr_plot_HOO_Garland_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.101 seconds) .. _sphx_glr_download_getting_started_auto_examples_plot_HOO_Garland.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_HOO_Garland.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_HOO_Garland.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_