code: def test_optimizer1():
      """Tests ``Optimizer`` class.
      This is a simple test where the optimization is used to find the roots
      of the polynomial: ``x*2 + 2*x + 1``.
      In this simple, example the optimization does not depend on ``data``.
      """
  
      def distance_to_zero(x):
          """This is the reward function which checks how close to zero
          the result is."""
          return -abs(x)
  
      reward = Reward(reward_func=distance_to_zero)
  
      optimizer = Optimizer(
          reward=reward,
          param_iterable=[{"x": x} for x in np.arange(-5, 5, 0.1)],
      )
  
      def calc_with_param(param, data=None):
          """This is the calculation step with ``param`` and ``data``.
          In this simple example, this does not depend on ``data.``
          """
          x = param["x"]
          return CalcResult(data=x**2 + 2 * x + 1, model=None)
  
      optimizer.calc_with_param = calc_with_param
  
      # ``data`` is not needed to be passed below
      # because it does not appear in ``calc_with_param`` definition
      optim_res = optimizer.optimize_param()
  
      best_param = optim_res["best_param"]
      best_param_full = optim_res["best_param_full"]
      assert round(best_param["x"], 2) == -1.00
      assert round(best_param_full["x"], 2) == -1.00
  
      param_obj_list = optim_res["param_obj_list"]
      param_obj_df = pd.DataFrame.from_records(param_obj_list)
  
      fig = plot_lines_markers(
          df=param_obj_df, x_col="x", line_cols=["obj_value"]
      )
      fig.layout.update(
          title="`Optimizer` parameter search for roots of `x**2 + 2*x + 1`"
      )
      assert fig is not None
  
file_path: greykite/greykite/tests/detection/detector/test_optimizer.py
imports: import numpy
  import pandas
  from greykite.common.viz.timeseries_annotate import plot_lines_markers
  from greykite.detection.detector.optimizer import CalcResult
  from greykite.detection.detector.optimizer import Optimizer
  from greykite.detection.detector.reward import Reward
label_desc: Use image node_id 2 for calling a global function with example usage: test_optimizer1() and returns: unknown, CalcResult