code: def optimize_df_with_constraints(
      df, objective_col, constraint_col, constraint_value
  ):
      """Function that solves the following constrained optimization problem.
  
           maximize ``df``[``objective_col``]
           subject to ``df``[``constraint_col``] >= ``constraint_value``.
  
       However, unlike traditional constrained optimization, which returns None when no
       values satisfy the constraint, this function maximizes ``df``[``constraint_col``].
       Note that in this case, since the constraint is not satisfied, this will get
       as close as possible to the ``constraint_value``.
  
       To understand the reasoning behind this choice, it is helpful to think
       about ``objective_col`` as precision and ``constraint_col`` as recall. Thus,
       the optimization problem becomes:
       maximize precision subject to recall >= target_recall.
  
       The algorithm proceeds as follows:
       1. Find rows which satisfy ``df``[``constraint_col``] >= ``constraint_value``.
           1.1. If such rows exist, find rows that have highest ``df``[``objective_col``].
               1.1.1. Find rows that have highest ``df``[``constraint_col``].
               1.1.2. Among these, find row that maximize ``df``[``objective_col``].
                      This solves for multiple ties, if any.
           1.2. If no such rows exist,
               1.2.1. Find rows that maximizes ``df``[``constraint_col``].
               1.2.2. Among these, find row that maximize ``df``[``objective_col``].
                      This solves for multiple ties, if any.
      2. Return corresponding ``df`` row.
  
       Parameters
       ----------
       df : `pandas.DataFrame`
           A data frame which includes minimally
               - the objective column (``objective_col``)
               - the constraint column (``constraint_col``).
       objective_col : `str`
           The column name of the variable to be optimized.
       constraint_col : `str`
           The column name of the constraint variable.
       constraint_value : `float`
           The value of the constraint.
  
       Returns
       -------
       optimal_dict : `dict`
           The row of the ``df`` which is the optimal of the
           corresponding optimization problem..
      """
      df = df.copy()
      constraint_match_indices = df[constraint_col] >= constraint_value
      if constraint_match_indices.any():
          log_message(
              f"Values satisfying the constraint are found.\n"
              f"Solving the following optimization problem:\n"
              f"Maximize {objective_col} subject to {constraint_col} >= {constraint_value}.",
              LoggingLevelEnum.INFO,
          )
          df = df[constraint_match_indices]
          df = df[df[objective_col] == max(df[objective_col])]
          df = df[df[constraint_col] == max(df[constraint_col])]
      else:
          log_message(
              f"No values satisfy the constraint.\n"
              f"Maximizing ``constraint_col`` ({constraint_col}) so that it is as "
              f"close as possible to the ``constraint_value`` ({constraint_value}).",
              LoggingLevelEnum.INFO,
          )
          df = df[df[constraint_col] == max(df[constraint_col])]
          df = df[df[objective_col] == max(df[objective_col])]
  
      return df.iloc[-1].to_dict()
  
file_path: greykite/greykite/detection/detector/ad_utils.py
imports: from functools import reduce
  import pandas
  from greykite.common.constants import ANOMALY_COL
  from greykite.common.constants import END_TIME_COL
  from greykite.common.constants import START_TIME_COL
  from greykite.common.constants import TIME_COL
  from greykite.common.features.outlier import ZScoreOutlierDetector
  from greykite.common.logging import LoggingLevelEnum
  from greykite.common.logging import log_message
  from greykite.detection.detector.constants import Z_SCORE_CUTOFF
label_desc: Use image node_id 6 for calling a global function with example usage: optimize_df_with_constraints(df, objective_col, constraint_col, constraint_value) and returns: df