.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "info/auto_examples/plot_new_obj.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_info_auto_examples_plot_new_obj.py: New Objective ===================== An example to implement a new blackbox objective for any PyXAB algorithm to optimize. First import all the useful packages .. GENERATED FROM PYTHON SOURCE LINES 7-13 .. code-block:: default from PyXAB.synthetic_obj.Objective import Objective from PyXAB.algos.HOO import T_HOO from PyXAB.partition.BinaryPartition import BinaryPartition .. GENERATED FROM PYTHON SOURCE LINES 14-17 We have already shown a ``Sine`` function example in the general instructions. Here, we give another very simple example. Suppose the objective is only a constant, i.e., ``f(x) = 1`` everywhere, then the class definition would be as simple as. .. GENERATED FROM PYTHON SOURCE LINES 17-27 .. code-block:: default class Constant_1(Objective): def __init__(self): self.fmax = 1 def f(self, x): return 1 .. GENERATED FROM PYTHON SOURCE LINES 28-30 The inheritance of the ``Objective`` class is unnecessary (but highly recommended for consistency). E.g., another possible definition could be .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: default class Constant_2(): def evaluate(self, x): return 1 .. GENERATED FROM PYTHON SOURCE LINES 38-40 Let us suppose the domain for this objective is ``[0, 10]``, and we use the binary partition for the domain and the HOO algorithm to optimize the objectives .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: default T = 100 target1 = Constant_1() target2 = Constant_2() domain = [[0, 10]] partition = BinaryPartition algo = T_HOO(domain=domain, partition=partition) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Now as can be seen below, the objectives are ready to be optimized .. GENERATED FROM PYTHON SOURCE LINES 50-65 .. code-block:: default # Optimize Objective Constant_1 for t in range(1, T+1): point = algo.pull(t) reward = target1.f(point) algo.receive_reward(t, reward) # Optimize Objective Constant_2 for t in range(1, T+1): point = algo.pull(t) reward = target2.evaluate(point) algo.receive_reward(t, reward) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.273 seconds) .. _sphx_glr_download_info_auto_examples_plot_new_obj.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_new_obj.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_new_obj.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_