.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "getting_started/auto_examples/plot_HCT_Himmelblau.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_HCT_Himmelblau.py: 2-D Example =================== In this example, we run the HCT algorithm on the normalized Himmelblau objective. First, import all the functions needed .. GENERATED FROM PYTHON SOURCE LINES 7-19 .. code-block:: default from PyXAB.synthetic_obj.Himmelblau import Himmelblau_Normalized # the objective from PyXAB.algos.HCT import HCT # 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-27 .. code-block:: default T = 1000 # the number of rounds is 1000 target = Himmelblau_Normalized() # the objective to optimize is the normalized Himmelblau domain = [[-5, 5], [-5, 5]] # the domain is [[-5, 5], [-5, 5]] partition = BinaryPartition # the partition chosen is BinaryPartition algo = HCT(domain=domain, partition=partition) # the algorithm is HCT .. GENERATED FROM PYTHON SOURCE LINES 28-29 To plot the regret, we can initialize the cumulative regret and the cumulative regret list .. GENERATED FROM PYTHON SOURCE LINES 29-34 .. code-block:: default cumulative_regret = 0 cumulative_regret_list = [] .. GENERATED FROM PYTHON SOURCE LINES 35-38 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 38-56 .. 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='HCT') .. image-sg:: /getting_started/auto_examples/images/sphx_glr_plot_HCT_Himmelblau_001.png :alt: plot HCT Himmelblau :srcset: /getting_started/auto_examples/images/sphx_glr_plot_HCT_Himmelblau_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-58 The following lines of code are only for creating thumbnails and do not need to be used .. GENERATED FROM PYTHON SOURCE LINES 58-72 .. code-block:: default # sphinx_gallery_thumbnail_number = 2 import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.linspace(domain[0][0], domain[0][1], 1000) y = np.linspace(domain[0][0], domain[0][1], 1000) xx, yy = np.meshgrid(x, y) z = (- (xx ** 2 + yy - 11) ** 2 - (xx + yy ** 2 - 7) ** 2) / 890 ax.plot_surface(xx, yy, 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_HCT_Himmelblau_002.png :alt: plot HCT Himmelblau :srcset: /getting_started/auto_examples/images/sphx_glr_plot_HCT_Himmelblau_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.884 seconds) .. _sphx_glr_download_getting_started_auto_examples_plot_HCT_Himmelblau.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_HCT_Himmelblau.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_HCT_Himmelblau.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_