.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "info/auto_examples/plot_new_algo.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_algo.py: New Algorithm ===================== A (dummy) example to implement a new PyXAB algorithm. First import all the useful packages .. GENERATED FROM PYTHON SOURCE LINES 7-14 .. code-block:: default from PyXAB.synthetic_obj.Garland import Garland from PyXAB.algos.Algo import Algorithm from PyXAB.partition.BinaryPartition import BinaryPartition import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 15-17 Now let us suppose that we want to implement a new (dummy) algorithm that always select all nodes on each layer and then evaluate them for a number of times .. GENERATED FROM PYTHON SOURCE LINES 17-55 .. code-block:: default class Dummy(Algorithm): def __init__(self, evaluation=5, rounds=1000, domain=None, partition=None): super(Dummy, self).__init__() if domain is None: raise ValueError("Parameter space is not given.") if partition is None: raise ValueError("Partition of the parameter space is not given.") self.partition = partition(domain=domain) self.iteration = 0 self.evaluation = evaluation self.rounds = rounds self.partition.deepen() self.iterator = 0 # we need to re-write the pull function def pull(self, time): depth = self.partition.get_depth() index = self.iterator // self.evaluation nodes = self.partition.get_node_list() point = nodes[depth][index].get_cpoint() self.iterator += 1 # get a point and increase the iterator if self.iterator >= self.evaluation * len(nodes[depth]): # If every point is evaluated, deepen the partition self.partition.deepen() return point # we need to re-write the receive_reward function def receive_reward(self, time, reward): # No update given the reward for the dummy algorithm pass def get_last_point(self): return self.pull(0) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Define the number of rounds, the target, the domain, the partition, and the algorithm for the learning process .. GENERATED FROM PYTHON SOURCE LINES 57-64 .. code-block:: default T = 100 target = Garland() domain = [[0, 1]] partition = BinaryPartition algo = Dummy(rounds=T, domain=domain, partition=partition) # The new algorithm .. GENERATED FROM PYTHON SOURCE LINES 65-66 As shown below, the Dummy algorithm now optimizes the objective .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. 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) #print(point) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.004 seconds) .. _sphx_glr_download_info_auto_examples_plot_new_algo.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_algo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_new_algo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_