OSQP_OPTIONS Sets options for OSQP. OPT = OSQP_OPTIONS OPT = OSQP_OPTIONS(OVERRIDES) OPT = OSQP_OPTIONS(OVERRIDES, FNAME) OPT = OSQP_OPTIONS(OVERRIDES, MPOPT) Sets the values for the settings struct normally passed to OSQP. Inputs are all optional, second argument must be either a string (FNAME) or a struct (MPOPT): OVERRIDES - struct containing values to override the defaults FNAME - name of user-supplied function called after default options are set to modify them. Calling syntax is: MODIFIED_OPT = FNAME(DEFAULT_OPT); MPOPT - MATPOWER options struct, uses the following fields: opf.violation - used to set opt.eps_prim_inf verbose - used to set opt.verbose osqp.opts - struct containing values to use as OVERRIDES osqp.opt_fname - name of user-supplied function used as FNAME, except with calling syntax: MODIFIED_OPT = FNAME(DEFAULT_OPT, MPOPT); Output is a settings struct to pass to OSQP. There are multiple ways of providing values to override the default options. Their precedence and order of application are as follows: With inputs OVERRIDES and FNAME 1. FNAME is called 2. OVERRIDES are applied With inputs OVERRIDES and MPOPT 1. FNAME (from osqp.opt_fname) is called 2. osqp.opts (if not empty) are applied 3. OVERRIDES are applied Example: If osqp.opt_fname = 'osqp_user_options_3', then after setting the default OSQP options, OSQP_OPTIONS will execute the following user-defined function to allow option overrides: opt = osqp_user_options_3(opt, mpopt); The contents of osqp_user_options_3.m, could be something like: function opt = osqp_user_options_3(opt, mpopt) opt.polish = 1; opt.alpha = 1; opt.max_iter = 5000; See the OSQP Solver settings page. https://osqp.org/docs/interfaces/solver_settings.html See also OSQP, MPOPTION.
0001 function opt = osqp_options(overrides, mpopt) 0002 %OSQP_OPTIONS Sets options for OSQP. 0003 % 0004 % OPT = OSQP_OPTIONS 0005 % OPT = OSQP_OPTIONS(OVERRIDES) 0006 % OPT = OSQP_OPTIONS(OVERRIDES, FNAME) 0007 % OPT = OSQP_OPTIONS(OVERRIDES, MPOPT) 0008 % 0009 % Sets the values for the settings struct normally passed to OSQP. 0010 % 0011 % Inputs are all optional, second argument must be either a string 0012 % (FNAME) or a struct (MPOPT): 0013 % 0014 % OVERRIDES - struct containing values to override the defaults 0015 % FNAME - name of user-supplied function called after default 0016 % options are set to modify them. Calling syntax is: 0017 % MODIFIED_OPT = FNAME(DEFAULT_OPT); 0018 % MPOPT - MATPOWER options struct, uses the following fields: 0019 % opf.violation - used to set opt.eps_prim_inf 0020 % verbose - used to set opt.verbose 0021 % osqp.opts - struct containing values to use as OVERRIDES 0022 % osqp.opt_fname - name of user-supplied function used as FNAME, 0023 % except with calling syntax: 0024 % MODIFIED_OPT = FNAME(DEFAULT_OPT, MPOPT); 0025 % 0026 % Output is a settings struct to pass to OSQP. 0027 % 0028 % There are multiple ways of providing values to override the default 0029 % options. Their precedence and order of application are as follows: 0030 % 0031 % With inputs OVERRIDES and FNAME 0032 % 1. FNAME is called 0033 % 2. OVERRIDES are applied 0034 % With inputs OVERRIDES and MPOPT 0035 % 1. FNAME (from osqp.opt_fname) is called 0036 % 2. osqp.opts (if not empty) are applied 0037 % 3. OVERRIDES are applied 0038 % 0039 % Example: 0040 % 0041 % If osqp.opt_fname = 'osqp_user_options_3', then after setting the 0042 % default OSQP options, OSQP_OPTIONS will execute the following 0043 % user-defined function to allow option overrides: 0044 % 0045 % opt = osqp_user_options_3(opt, mpopt); 0046 % 0047 % The contents of osqp_user_options_3.m, could be something like: 0048 % 0049 % function opt = osqp_user_options_3(opt, mpopt) 0050 % opt.polish = 1; 0051 % opt.alpha = 1; 0052 % opt.max_iter = 5000; 0053 % 0054 % See the OSQP Solver settings page. 0055 % 0056 % https://osqp.org/docs/interfaces/solver_settings.html 0057 % 0058 % See also OSQP, MPOPTION. 0059 0060 % MP-Opt-Model 0061 % Copyright (c) 2010-2020, Power Systems Engineering Research Center (PSERC) 0062 % by Ray Zimmerman, PSERC Cornell 0063 % 0064 % This file is part of MP-Opt-Model. 0065 % Covered by the 3-clause BSD License (see LICENSE file for details). 0066 % See https://github.com/MATPOWER/mp-opt-model for more info. 0067 0068 %%----- initialization and arg handling ----- 0069 %% defaults 0070 verbose = 2; 0071 fname = ''; 0072 0073 %% second argument 0074 if nargin > 1 && ~isempty(mpopt) 0075 if ischar(mpopt) %% 2nd arg is FNAME (string) 0076 fname = mpopt; 0077 have_mpopt = 0; 0078 else %% 2nd arg is MPOPT (MATPOWER options struct) 0079 have_mpopt = 1; 0080 verbose = mpopt.verbose; 0081 if isfield(mpopt.osqp, 'opt_fname') && ~isempty(mpopt.osqp.opt_fname) 0082 fname = mpopt.osqp.opt_fname; 0083 end 0084 end 0085 else 0086 have_mpopt = 0; 0087 end 0088 0089 %%----- set default options for OSQP ----- 0090 opt = struct( ... 0091 ); 0092 % 'polish', 1 ... 0093 % 'alpha', 1 ... %% default = 1.6 (unless problem dependent) 0094 % 'eps_abs', 1e-9, ... %% default = 1e-3 0095 % 'eps_rel', 1e-9, ... %% default = 1e-3 0096 % 'eps_prim_inf', 1e-7, ... %% default = 1e-4 0097 % 'eps_dual_inf', 1e-7, ... %% default = 1e-4 0098 if have_mpopt 0099 opt.polish = 1; 0100 opt.eps_prim_inf = mpopt.opf.violation; 0101 % opt.eps_abs = 1e-4; 0102 % opt.eps_rel = 1e-4; 0103 end 0104 0105 %%----- call user function to modify defaults ----- 0106 if ~isempty(fname) 0107 if have_mpopt 0108 opt = feval(fname, opt, mpopt); 0109 else 0110 opt = feval(fname, opt); 0111 end 0112 end 0113 0114 %%----- apply overrides ----- 0115 if have_mpopt && isfield(mpopt.osqp, 'opts') && ~isempty(mpopt.osqp.opts) 0116 opt = nested_struct_copy(opt, mpopt.osqp.opts); 0117 end 0118 if nargin > 0 && ~isempty(overrides) 0119 opt = nested_struct_copy(opt, overrides); 0120 end