R/136_reductions_flip_objective.R

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/reductions/flip_objective.R
#####

## CVXPY SOURCE: reductions/flip_objective.py
## FlipObjective -- flip Maximize to Minimize and vice versa


FlipObjective <- new_class("FlipObjective", parent = Reduction, package = "CVXR",
  constructor = function() {
    new_object(S7_object(),
      .cache = new.env(parent = emptyenv())
    )
  }
)

## accepts: always TRUE
## CVXPY SOURCE: flip_objective.py line 26-27
method(reduction_accepts, FlipObjective) <- function(x, problem, ...) TRUE

## apply: flip objective type, negate expression
## CVXPY SOURCE: flip_objective.py lines 29-48
method(reduction_apply, FlipObjective) <- function(x, problem, ...) {
  is_maximize <- S7_inherits(problem@objective, Maximize)
  obj_cls <- if (is_maximize) Minimize else Maximize
  new_obj <- obj_cls(-problem@objective@args[[1L]])
  new_problem <- Problem(new_obj, problem@constraints)
  list(new_problem, list())
}

## invert: negate optimal value back
## CVXPY SOURCE: flip_objective.py lines 50-67
method(reduction_invert, FlipObjective) <- function(x, solution, inverse_data, ...) {
  neg_val <- if (!is.na(solution@opt_val)) -solution@opt_val else NA_real_
  Solution(status      = solution@status,
           opt_val     = neg_val,
           primal_vars = solution@primal_vars,
           dual_vars   = solution@dual_vars,
           attr        = solution@attr)
}

Try the CVXR package in your browser

Any scripts or data that you put into this service are public.

CVXR documentation built on March 6, 2026, 9:10 a.m.