R/260_zzz_R_specific_solver_opts.R

Defines functions .build_solver_params solver_opts

Documented in solver_opts

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/zzz_R_specific/solver_opts.R
#####

## R-SPECIFIC: solver_opts — unified solver options constructor
##
## Replaces CVXPY's implicit solver_opts dict with a structured list.
## The constructor's named parameters sort known options into fixed slots;
## unknown parameters (solver-native) go to $solver_specific.
##
## Used by psolve(), problem_data(), and construct_solving_chain().

#' Create Solver Options
#'
#' Constructs a structured list of solver options for use with
#' \code{\link{psolve}} and \code{\link{problem_data}}. Known parameters
#' are sorted into named slots; solver-specific parameters are collected
#' in \code{$solver_specific}.
#'
#' @param use_quad_obj Logical. If \code{TRUE} (default), quadratic
#'   objectives use the QP matrix path. If \code{FALSE}, forces conic
#'   decomposition via \code{quad_form_canon}.
#' @param feastol Feasibility tolerance (solver-agnostic). Translated to
#'   solver-native name by internal mapping. \code{NULL} uses solver default.
#' @param reltol Relative tolerance. \code{NULL} uses solver default.
#' @param abstol Absolute tolerance. \code{NULL} uses solver default.
#' @param num_iter Maximum iterations. \code{NULL} uses solver default.
#' @param ... Solver-specific parameters passed directly to the solver
#'   (e.g., \code{eps_abs}, \code{scip_params}, \code{mosek_params}).
#' @returns A named list with class \code{"solver_opts"}.
#' @examples
#' solver_opts(feastol = 1e-6)
#' solver_opts(use_quad_obj = FALSE, eps_abs = 1e-7)
#' solver_opts(scip_params = list("limits/time" = 10))
#' @export
solver_opts <- function(use_quad_obj = TRUE,
                        feastol = NULL, reltol = NULL,
                        abstol = NULL, num_iter = NULL, ...) {
  structure(
    list(
      use_quad_obj    = as.logical(use_quad_obj),
      feastol         = feastol,
      reltol          = reltol,
      abstol          = abstol,
      num_iter        = num_iter,
      solver_specific = list(...)
    ),
    class = "solver_opts"
  )
}

## ── .build_solver_params ─────────────────────────────────────────
## Translates a solver_opts list into the flat named list that
## solve_via_data() expects. Replaces .apply_std_params().
##
## 1. Start with solver_specific (pass-through)
## 2. Translate standard params (feastol/reltol/abstol/num_iter)
##    to solver-native names using .SOLVER_DEFAULT_PARAM mapping
## 3. Standard params do NOT override solver-native names already
##    present in solver_specific
##
## @param solver_name Character, e.g. "CLARABEL", "OSQP"
## @param opts A solver_opts list
## @returns Named list of solver-native parameters

.build_solver_params <- function(solver_name, opts) {
  ## Start with solver-specific params (pass-through)
  result <- opts$solver_specific

  ## Translate standard params to solver-native names
  mapping <- .SOLVER_DEFAULT_PARAM[[solver_name]]
  if (!is.null(mapping)) {
    std_vals <- list(feastol = opts$feastol, reltol = opts$reltol,
                     abstol = opts$abstol, num_iter = opts$num_iter)
    for (std_name in names(std_vals)) {
      val <- std_vals[[std_name]]
      if (is.null(val)) next
      info <- mapping[[std_name]]
      if (is.null(info)) next
      ## Standard param only applies if user hasn't set the native name directly
      if (is.null(result[[info$name]])) {
        if (std_name == "num_iter") val <- as.integer(val)
        result[[info$name]] <- val
      }
    }
  }
  result
}

Try the CVXR package in your browser

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

CVXR documentation built on April 4, 2026, 9:08 a.m.