Package pypower :: Module mosek_options
[hide private]
[frames] | no frames]

Source Code for Module pypower.mosek_options

  1  # Copyright (C) 2010-2011 Power System Engineering Research Center (PSERC) 
  2  # Copyright (C) 2011 Richard Lincoln 
  3  # 
  4  # PYPOWER is free software: you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published 
  6  # by the Free Software Foundation, either version 3 of the License, 
  7  # or (at your option) any later version. 
  8  # 
  9  # PYPOWER is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  """Sets options for MOSEK. 
 18  """ 
 19   
 20  try: 
 21      from pymosek import mosekopt 
 22  except ImportError: 
 23  #    print "MOSEK not available" 
 24      pass 
 25   
 26  from pypower.util import feval 
 27   
 28   
29 -def mosek_options(overrides=None, ppopt=None):
30 """Sets options for MOSEK. 31 32 Inputs are all optional, second argument must be either a string 33 (C{fname}) or a dict (C{ppopt}): 34 35 - C{overrides} 36 - dict containing values to override the defaults 37 - C{fname} name of user-supplied function called after default 38 options are set to modify them. Calling syntax is:: 39 modified_opt = fname(default_opt) 40 - C{ppopt} PYPOWER options vector, uses the following entries: 41 - C{OPF_VIOLATION} used to set opt.MSK_DPAR_INTPNT_TOL_PFEAS 42 - C{VERBOSE} not currently used here 43 - C{MOSEK_MAX_IT} used to set opt.MSK_IPAR_INTPNT_MAX_ITERATIONS 44 - C{MOSEK_GAP_TOL} used to set opt.MSK_DPAR_INTPNT_TOL_REL_GAP 45 - C{MOSEK_MAX_TIME} used to set opt.MSK_DPAR_OPTIMIZER_MAX_TIME 46 - C{MOSEK_NUM_THREADS} used to set opt.MSK_IPAR_INTPNT_NUM_THREADS 47 - C{MOSEK_OPT} user option file, if ppopt['MOSEK_OPT'] is non-zero 48 non-zero it is apped to 'mosek_user_options_' to form 49 the name of a user-supplied function used as C{fname} 50 described above, except with calling syntax:: 51 modified_opt = fname(default_opt, ppopt) 52 53 Output is a param dict to pass to MOSEKOPT. 54 55 Example: 56 57 If PPOPT['MOSEK_OPT'] = 3, then after setting the default MOSEK options, 58 L{mosek_options} will execute the following user-defined function 59 to allow option overrides:: 60 61 opt = mosek_user_options_3(opt, ppopt) 62 63 The contents of mosek_user_options_3.py, could be something like:: 64 65 def mosek_user_options_3(opt, ppopt): 66 opt = {} 67 opt.MSK_DPAR_INTPNT_TOL_DFEAS = 1e-9 68 opt.MSK_IPAR_SIM_MAX_ITERATIONS = 5000000 69 return opt 70 71 See the Parameters reference in Appix E of "The MOSEK 72 optimization toolbox for MATLAB manaul" for 73 details on the available options. 74 75 U{http://www.mosek.com/documentation/} 76 77 @see: C{mosekopt}, L{ppoption}. 78 79 @author: Ray Zimmerman (PSERC Cornell) 80 @author: Richard Lincoln 81 """ 82 ##----- initialization and arg handling ----- 83 ## defaults 84 verbose = 2 85 gaptol = 0 86 fname = '' 87 88 ## get symbolic constant names 89 r, res = mosekopt('symbcon echo(0)') 90 sc = res['symbcon'] 91 92 ## second argument 93 if ppopt == None: 94 if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) 95 fname = ppopt 96 have_ppopt = False 97 else: ## 2nd arg is ppopt (MATPOWER options vector) 98 have_ppopt = True 99 ## (make default OPF_VIOLATION correspond to default MOSEK intpnt_tol_pfeas) 100 verbose = ppopt['VERBOSE'] 101 if ppopt['MOSEK_OPT']: 102 fname = 'mosek_user_options_#d' # ppopt['MOSEK_OPT'] 103 else: 104 have_ppopt = False 105 106 opt = {} 107 ##----- set default options for MOSEK ----- 108 ## solution algorithm 109 if have_ppopt: 110 alg = ppopt['MOSEK_LP_ALG'] 111 if alg == sc['MSK_OPTIMIZER_FREE'] or \ 112 alg == sc['MSK_OPTIMIZER_INTPNT'] or \ 113 alg == sc['MSK_OPTIMIZER_PRIMAL_SIMPLEX'] or \ 114 alg == sc['MSK_OPTIMIZER_DUAL_SIMPLEX'] or \ 115 alg == sc['MSK_OPTIMIZER_PRIMAL_DUAL_SIMPLEX'] or \ 116 alg == sc['MSK_OPTIMIZER_FREE_SIMPLEX'] or \ 117 alg == sc['MSK_OPTIMIZER_CONCURRENT']: 118 opt['MSK_IPAR_OPTIMIZER'] = alg 119 else: 120 opt['MSK_IPAR_OPTIMIZER'] = sc['MSK_OPTIMIZER_FREE']; 121 122 ## (make default OPF_VIOLATION correspond to default MSK_DPAR_INTPNT_TOL_PFEAS) 123 opt['MSK_DPAR_INTPNT_TOL_PFEAS'] = ppopt['OPF_VIOLATION'] / 500 124 if ppopt['MOSEK_MAX_IT']: 125 opt['MSK_IPAR_INTPNT_MAX_ITERATIONS'] = ppopt['MOSEK_MAX_IT'] 126 127 if ppopt['MOSEK_GAP_TOL']: 128 opt['MSK_DPAR_INTPNT_TOL_REL_GAP'] = ppopt['MOSEK_GAP_TOL'] 129 130 if ppopt['MOSEK_MAX_TIME']: 131 opt['MSK_DPAR_OPTIMIZER_MAX_TIME'] = ppopt['MOSEK_MAX_TIME'] 132 133 if ppopt['MOSEK_NUM_THREADS']: 134 opt['MSK_IPAR_INTPNT_NUM_THREADS'] = ppopt['MOSEK_NUM_THREADS'] 135 else: 136 opt['MSK_IPAR_OPTIMIZER'] = sc['MSK_OPTIMIZER_FREE'] 137 138 # opt['MSK_DPAR_INTPNT_TOL_PFEAS'] = 1e-8 ## primal feasibility tol 139 # opt['MSK_DPAR_INTPNT_TOL_DFEAS'] = 1e-8 ## dual feasibility tol 140 # opt['MSK_DPAR_INTPNT_TOL_MU_RED'] = 1e-16 ## relative complementarity gap tol 141 # opt['MSK_DPAR_INTPNT_TOL_REL_GAP'] = 1e-8 ## relative gap termination tol 142 # opt['MSK_IPAR_INTPNT_MAX_ITERATIONS'] = 400 ## max iterations for int point 143 # opt['MSK_IPAR_SIM_MAX_ITERATIONS'] = 10000000 ## max iterations for simplex 144 # opt['MSK_DPAR_OPTIMIZER_MAX_TIME'] = -1 ## max time allowed (< 0 --> Inf) 145 # opt['MSK_IPAR_INTPNT_NUM_THREADS'] = 1 ## number of threads 146 # opt['MSK_IPAR_PRESOLVE_USE'] = sc['MSK_PRESOLVE_MODE_OFF'] 147 148 # if verbose == 0: 149 # opt['MSK_IPAR_LOG'] = 0 150 # 151 152 ##----- call user function to modify defaults ----- 153 if len(fname) > 0: 154 if have_ppopt: 155 opt = feval(fname, opt, ppopt) 156 else: 157 opt = feval(fname, opt) 158 159 ##----- apply overrides ----- 160 if overrides is not None: 161 names = overrides.keys() 162 for k in range(len(names)): 163 opt[names[k]] = overrides[names[k]]
164