class_docstr: Penalized Limited-memory BFGS (L-BFGS) optimizer.
  
  Performs constrained optimization via penalized L-BFGS. The penalty term is
  adaptively adjusted to make sure that the constraint is satisfied.
  
  Args:
      max_opt_itr (int): Maximum iteration for update.
      initial_penalty (float): Initial penalty.
      min_penalty (float): Minimum penalty allowed. Penalty will be clipped
          if lower than this value.
      max_penalty (float): Maximum penalty allowed. Penalty will be clipped
          if higher than this value.
      increase_penalty_factor (float): Factor to increase penalty in each
          penalty iteration.
      decrease_penalty_factor (float): Factor to decrease penalty in each
          penalty iteration.
      max_penalty_itr (int): Maximum penalty iterations to perform.
      adapt_penalty (bool): Whether the penalty is adaptive or not. If false,
          penalty will not change.
class_docstr_tok: ["Penalized","Limited-memory","BFGS","(","L-BFGS",")","optimizer",".","Performs","constrained","optimization","via","penalized","L-BFGS",".","The","penalty","term","is","adaptively","adjusted","to","make","sure","that","the","constraint","is","satisfied",".","Args",":","max_opt_itr","(","int",")",":","Maximum","iteration","for","update",".","initial_penalty","(","float",")",":","Initial","penalty",".","min_penalty","(","float",")",":","Minimum","penalty","allowed",".","Penalty","will","be","clipped","if","lower","than","this","value",".","max_penalty","(","float",")",":","Maximum","penalty","allowed",".","Penalty","will","be","clipped","if","higher","than","this","value",".","increase_penalty_factor","(","float",")",":","Factor","to","increase","penalty","in","each","penalty","iteration",".","decrease_penalty_factor","(","float",")",":","Factor","to","decrease","penalty","in","each","penalty","iteration",".","max_penalty_itr","(","int",")",":","Maximum","penalty","iterations","to","perform",".","adapt_penalty","(","bool",")",":","Whether","the","penalty","is","adaptive","or","not",".","If","false",",","penalty","will","not","change","."]
docstr: Perform optimization.
  
  Args:
      inputs (list[numpy.ndarray]): List of input values.
      name (str): Name scope.
  
  Raises:
      Exception: If loss function is None, i.e. not defined.
docstr_tok: ["Perform","optimization",".","Args",":","inputs","(","list","[","numpy.ndarray","]",")",":","List","of","input","values",".","name","(","str",")",":","Name","scope",".","Raises",":","Exception",":","If","loss","function","is","None",",","i.e",".","not","defined","."]
code: def optimize(self, inputs, name="optimize"):
      """Perform optimization.
  
      Args:
          inputs (list[numpy.ndarray]): List of input values.
          name (str): Name scope.
  
      Raises:
          Exception: If loss function is None, i.e. not defined.
  
      """
      if self._opt_fun is None:
          raise Exception(
              "Use update_opt() to setup the loss function first."
          )
  
      with tf.name_scope(name):
          inputs = tuple(inputs)
  
          try_penalty = np.clip(
              self._penalty, self._min_penalty, self._max_penalty
          )
  
          penalty_scale_factor = None
          f_opt = self._opt_fun["f_opt"]
          f_penalized_loss = self._opt_fun["f_penalized_loss"]
  
          def gen_f_opt(penalty):  # noqa: D202
              """Return a function that set parameters values.
  
              Args:
                  penalty (float): Penalty.
  
              Returns:
                  callable: Function that set parameters values.
  
              """
  
              def f(flat_params):
                  """Helper function to set parameters values.
  
                  Args:
                      flat_params (numpy.ndarray): Flatten parameter values.
  
                  Returns:
                      list[tf.Tensor]: Penalized loss and gradient tensor.
  
                  """
                  self._target.set_param_values(flat_params)
                  return f_opt(*(inputs + (penalty,)))
  
              return f
  
          cur_params = self._target.get_param_values().astype("float64")
          opt_params = cur_params
  
          for penalty_itr in range(self._max_penalty_itr):
              logger.log("trying penalty=%.3f..." % try_penalty)
  
              itr_opt_params, _, _ = scipy.optimize.fmin_l_bfgs_b(
                  func=gen_f_opt(try_penalty),
                  x0=cur_params,
                  maxiter=self._max_opt_itr,
              )
  
              _, try_loss, try_constraint_val = f_penalized_loss(
                  *(inputs + (try_penalty,))
              )
  
              logger.log(
                  "penalty %f => loss %f, %s %f"
                  % (
                      try_penalty,
                      try_loss,
                      self._constraint_name,
                      try_constraint_val,
                  )
              )
  
              # Either constraint satisfied, or we are at the last iteration
              # already and no alternative parameter satisfies the constraint
              if try_constraint_val < self._max_constraint_val or (
                  penalty_itr == self._max_penalty_itr - 1
                  and opt_params is None
              ):
                  opt_params = itr_opt_params
  
              if not self._adapt_penalty:
                  break
  
              # Decide scale factor on the first iteration, or if constraint
              # violation yields numerical error
              if penalty_scale_factor is None or np.isnan(
                  try_constraint_val
              ):
                  # Increase penalty if constraint violated, or if constraint
                  # term is NAN
                  if (
                      try_constraint_val > self._max_constraint_val
                      or np.isnan(try_constraint_val)
                  ):
                      penalty_scale_factor = (
                          self._increase_penalty_factor
                      )
                  else:
                      # Otherwise (i.e. constraint satisfied), shrink penalty
                      penalty_scale_factor = (
                          self._decrease_penalty_factor
                      )
                      opt_params = itr_opt_params
              else:
                  if (
                      penalty_scale_factor > 1
                      and try_constraint_val <= self._max_constraint_val
                  ):
                      break
                  if (
                      penalty_scale_factor < 1
                      and try_constraint_val >= self._max_constraint_val
                  ):
                      break
              try_penalty *= penalty_scale_factor
              try_penalty = np.clip(
                  try_penalty, self._min_penalty, self._max_penalty
              )
              self._penalty = try_penalty
  
          self._target.set_param_values(opt_params)
  
file_path: garage/src/garage/tf/optimizers/penalty_lbfgs_optimizer.py
imports: from dowel import logger
  import numpy
  import scipy.optimize
  import tensorflow
  from garage.tf import compile_function, flatten_tensor_variables
  from garage.tf.optimizers._dtypes import LazyDict
label_desc: Use image node_id 5 for calling the PenaltyLBFGSOptimizer obj's underlying member method code with example usage: obj.optimize(inputs, name) and returns: f, f_opt