def run_userfcn(userfcn, stage, *args): """Runs the userfcn callbacks for a given stage. Example:: ppc = om.get_mpc() om = run_userfcn(ppc['userfcn'], 'formulation', om) @param userfcn: the 'userfcn' field of ppc, populated by L{add_userfcn} @param stage: the name of the callback stage begin executed (additional arguments) some stages require additional arguments. @see: L{add_userfcn}, L{remove_userfcn}, L{toggle_reserves}, L{toggle_iflims}, L{runopf_w_res}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ rv = args[0] if (len(userfcn) > 0) and (stage in userfcn): for k in range(len(userfcn[stage])): if 'args' in userfcn[stage][k]: args = userfcn[stage][k]['args'] else: args = [] if stage in ['ext2int', 'formulation', 'int2ext']: # ppc = userfcn_*_ext2int(ppc, args) # om = userfcn_*_formulation(om, args) # results = userfcn_*_int2ext(results, args) rv = userfcn[stage][k]['fcn'](rv, args) elif stage in ['printpf', 'savecase']: # results = userfcn_*_printpf(results, fd, ppopt, args) # ppc = userfcn_*_savecase(mpc, fd, prefix, args) rv = feval(userfcn[stage][k]['fcn'], rv, args[1], args[2], args) return rv
def mosek_options(overrides=None, ppopt=None): """Sets options for MOSEK. Inputs are all optional, second argument must be either a string (C{fname}) or a dict (C{ppopt}): - C{overrides} - dict containing values to override the defaults - C{fname} name of user-supplied function called after default options are set to modify them. Calling syntax is:: modified_opt = fname(default_opt) - C{ppopt} PYPOWER options vector, uses the following entries: - C{OPF_VIOLATION} used to set opt.MSK_DPAR_INTPNT_TOL_PFEAS - C{VERBOSE} not currently used here - C{MOSEK_LP_ALG} - used to set opt.MSK_IPAR_OPTIMIZER - C{MOSEK_MAX_IT} used to set opt.MSK_IPAR_INTPNT_MAX_ITERATIONS - C{MOSEK_GAP_TOL} used to set opt.MSK_DPAR_INTPNT_TOL_REL_GAP - C{MOSEK_MAX_TIME} used to set opt.MSK_DPAR_OPTIMIZER_MAX_TIME - C{MOSEK_NUM_THREADS} used to set opt.MSK_IPAR_INTPNT_NUM_THREADS - C{MOSEK_OPT} user option file, if ppopt['MOSEK_OPT'] is non-zero it is appended to 'mosek_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax:: modified_opt = fname(default_opt, ppopt) Output is a param dict to pass to MOSEKOPT. Example: If PPOPT['MOSEK_OPT'] = 3, then after setting the default MOSEK options, L{mosek_options} will execute the following user-defined function to allow option overrides:: opt = mosek_user_options_3(opt, ppopt) The contents of mosek_user_options_3.py, could be something like:: def mosek_user_options_3(opt, ppopt): opt = {} opt.MSK_DPAR_INTPNT_TOL_DFEAS = 1e-9 opt.MSK_IPAR_SIM_MAX_ITERATIONS = 5000000 return opt See the Parameters reference in Appix E of "The MOSEK optimization toolbox for MATLAB manaul" for details on the available options. U{http://www.mosek.com/documentation/} @see: C{mosekopt}, L{ppoption}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ##----- initialization and arg handling ----- ## defaults verbose = 2 gaptol = 0 fname = '' ## get symbolic constant names r, res = mosekopt('symbcon echo(0)') sc = res['symbcon'] ## second argument if ppopt == None: if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is ppopt (MATPOWER options vector) have_ppopt = True verbose = ppopt['VERBOSE'] if ppopt['MOSEK_OPT']: fname = 'mosek_user_options_#d' # ppopt['MOSEK_OPT'] else: have_ppopt = False opt = {} ##----- set default options for MOSEK ----- ## solution algorithm if have_ppopt: alg = ppopt['MOSEK_LP_ALG'] if alg == sc['MSK_OPTIMIZER_FREE'] or \ alg == sc['MSK_OPTIMIZER_INTPNT'] or \ alg == sc['MSK_OPTIMIZER_PRIMAL_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_DUAL_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_PRIMAL_DUAL_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_FREE_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_CONCURRENT']: opt['MSK_IPAR_OPTIMIZER'] = alg else: opt['MSK_IPAR_OPTIMIZER'] = sc['MSK_OPTIMIZER_FREE']; ## (make default OPF_VIOLATION correspond to default MSK_DPAR_INTPNT_TOL_PFEAS) opt['MSK_DPAR_INTPNT_TOL_PFEAS'] = ppopt['OPF_VIOLATION'] / 500 if ppopt['MOSEK_MAX_IT']: opt['MSK_IPAR_INTPNT_MAX_ITERATIONS'] = ppopt['MOSEK_MAX_IT'] if ppopt['MOSEK_GAP_TOL']: opt['MSK_DPAR_INTPNT_TOL_REL_GAP'] = ppopt['MOSEK_GAP_TOL'] if ppopt['MOSEK_MAX_TIME']: opt['MSK_DPAR_OPTIMIZER_MAX_TIME'] = ppopt['MOSEK_MAX_TIME'] if ppopt['MOSEK_NUM_THREADS']: opt['MSK_IPAR_INTPNT_NUM_THREADS'] = ppopt['MOSEK_NUM_THREADS'] else: opt['MSK_IPAR_OPTIMIZER'] = sc['MSK_OPTIMIZER_FREE'] # opt['MSK_DPAR_INTPNT_TOL_PFEAS'] = 1e-8 ## primal feasibility tol # opt['MSK_DPAR_INTPNT_TOL_DFEAS'] = 1e-8 ## dual feasibility tol # opt['MSK_DPAR_INTPNT_TOL_MU_RED'] = 1e-16 ## relative complementarity gap tol # opt['MSK_DPAR_INTPNT_TOL_REL_GAP'] = 1e-8 ## relative gap termination tol # opt['MSK_IPAR_INTPNT_MAX_ITERATIONS'] = 400 ## max iterations for int point # opt['MSK_IPAR_SIM_MAX_ITERATIONS'] = 10000000 ## max iterations for simplex # opt['MSK_DPAR_OPTIMIZER_MAX_TIME'] = -1 ## max time allowed (< 0 --> Inf) # opt['MSK_IPAR_INTPNT_NUM_THREADS'] = 1 ## number of threads # opt['MSK_IPAR_PRESOLVE_USE'] = sc['MSK_PRESOLVE_MODE_OFF'] # if verbose == 0: # opt['MSK_IPAR_LOG'] = 0 # ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = overrides.keys() for k in range(len(names)): opt[names[k]] = overrides[names[k]]
def cplex_options(overrides=None, ppopt=None): """Sets options for CPLEX. Sets the values for the options dict normally passed to C{cplexoptimset}. Inputs are all optional, second argument must be either a string (C{fname}) or a dict (C{ppopt}): Output is an options dict to pass to C{cplexoptimset}. Example: If C{ppopt['CPLEX_OPT'] = 3}, then after setting the default CPLEX options, CPLEX_OPTIONS will execute the following user-defined function to allow option overrides:: opt = cplex_user_options_3(opt, ppopt) The contents of cplex_user_options_3.py, could be something like:: def cplex_user_options_3(opt, ppopt): opt = {} opt['threads'] = 2 opt['simplex']['refactor'] = 1 opt['timelimit'] = 10000 return opt For details on the available options, see the I{"Parameters Reference Manual"} section of the CPLEX documentation at: U{http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r2/} @param overrides: - dict 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) @param ppopt: PYPOWER options vector, uses the following entries: - OPF_VIOLATION - used to set opt.simplex.tolerances.feasibility - VERBOSE - used to set opt.barrier.display, opt.conflict.display, opt.mip.display, opt.sifting.display, opt.simplex.display, opt.tune.display - CPLEX_LPMETHOD - used to set opt.lpmethod - CPLEX_QPMETHOD - used to set opt.qpmethod - CPLEX_OPT - user option file, if ppopt['CPLEX_OPT'] is non-zero it is appended to 'cplex_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax:: modified_opt = fname(default_opt, ppopt) @see: C{cplexlp}, C{cplexqp}, L{ppoption}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ##----- initialization and arg handling ----- ## defaults verbose = 1 feastol = 1e-6 fname = '' ## second argument if ppopt != None: if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is ppopt (MATPOWER options vector) have_ppopt = True ## (make default OPF_VIOLATION correspond to default CPLEX feastol) feastol = ppopt['OPF_VIOLATION'] / 5 verbose = ppopt['VERBOSE'] lpmethod = ppopt['CPLEX_LPMETHOD'] qpmethod = ppopt['CPLEX_QPMETHOD'] if ppopt['CPLEX_OPT']: fname = 'cplex_user_options_#d' % ppopt['CPLEX_OPT'] else: have_ppopt = False ##----- set default options for CPLEX ----- opt = cplexoptimset('cplex') opt['simplex']['tolerances']['feasibility'] = feastol ## printing vrb = max([0, verbose - 1]) opt['barrier']['display'] = vrb opt['conflict']['display'] = vrb opt['mip']['display'] = vrb opt['sifting']['display'] = vrb opt['simplex']['display'] = vrb opt['tune']['display'] = vrb ## solution algorithm if have_ppopt: opt['lpmethod'] = lpmethod opt['qpmethod'] = qpmethod #else: # opt['lpmethod'] = 2 # opt['qpmethod'] = 2 ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = overrides.keys() for k in range(len(names)): if isinstance(overrides[names[k]], dict): names2 = overrides[names[k]].keys() for k2 in range(len(names2)): if isinstance(overrides[names[k]][names2[k2]], dict): names3 = overrides[names[k]][names2[k2]].keys() for k3 in range(len(names3)): opt[names[k]][names2[k2]][names3[k3]] = overrides[names[k]][names2[k2]][names3[k3]] else: opt[names[k]][names2[k2]] = overrides[names[k]][names2[k2]] else: opt[names[k]] = overrides[names[k]] return opt
def gurobi_options(overrides=None, ppopt=None): """Sets options for GUROBI. Sets the values for the options dict normally passed to GUROBI_MEX. Inputs are all optional, second argument must be either a string (fname) or a vector (ppopt): overrides - dict 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) ppopt - PYPOWER options vector, uses the following entries: OPF_VIOLATION (16) - used to set opt.FeasibilityTol VERBOSE (31) - used to set opt.DisplayInterval, opt.Display GRB_METHOD (121) - used to set opt.Method GRB_TIMELIMIT (122) - used to set opt.TimeLimit (seconds) GRB_THREADS (123) - used to set opt.Threads GRB_OPT (124) - user option file, if PPOPT(124) is non-zero it is appended to 'gurobi_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax: modified_opt = fname(default_opt, mpopt) Output is an options struct to pass to GUROBI_MEX. Example: If ppopt['GRB_OPT'] = 3, then after setting the default GUROBI options, GUROBI_OPTIONS will execute the following user-defined function to allow option overrides: opt = gurobi_user_options_3(opt, ppopt) The contents of gurobi_user_options_3.py, could be something like: def gurobi_user_options_3(opt, ppopt): opt = {} opt['OptimalityTol'] = 1e-9 opt['IterationLimit'] = 3000 opt['BarIterLimit'] = 200 opt['Crossover'] = 0 opt['Presolve'] = 0 return opt For details on the available options, see the "Parameters" section of the "Gurobi Optimizer Reference Manual" at: http://www.gurobi.com/doc/45/refman/ @see: L{gurobi_mex}, L{ppoption}. """ ##----- initialization and arg handling ----- ## defaults verbose = True fname = '' ## second argument if ppopt != None: if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is MPOPT (MATPOWER options vector) have_ppopt = True verbose = ppopt['VERBOSE'] if ppopt['GRB_OPT']: fname = 'gurobi_user_options_%d', ppopt['GRB_OPT'] else: have_ppopt = False ##----- set default options for CPLEX ----- opt = {} # opt['OptimalityTol'] = 1e-6 ## -1 - auto, 0 - no, 1 - conserv, 2 - aggressive= # opt['Presolve'] = -1 # opt['LogFile'] = 'qps_gurobi.log' if have_ppopt: ## (make default OPF_VIOLATION correspond to default FeasibilityTol) opt['FeasibilityTol'] = ppopt['OPF_VIOLATION'] / 5 opt['Method'] = ppopt['GRB_METHOD'] opt['TimeLimit'] = ppopt['GRB_TIMELIMIT'] opt['Threads'] = ppopt['GRB_THREADS'] else: opt['Method'] = 1 ## dual simplex opt['Display'] = min(verbose, 3) if verbose: opt['DisplayInterval'] = 1 else: opt['DisplayInterval'] = Inf ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = overrides.keys() for k in range(len(names)): opt[names[k]] = overrides[names[k]] return opt
def mosek_options(overrides=None, ppopt=None): """Sets options for MOSEK. Inputs are all optional, second argument must be either a string (C{fname}) or a dict (C{ppopt}): - C{overrides} - dict containing values to override the defaults - C{fname} name of user-supplied function called after default options are set to modify them. Calling syntax is:: modified_opt = fname(default_opt) - C{ppopt} PYPOWER options vector, uses the following entries: - C{OPF_VIOLATION} used to set opt.MSK_DPAR_INTPNT_TOL_PFEAS - C{VERBOSE} not currently used here - C{MOSEK_LP_ALG} - used to set opt.MSK_IPAR_OPTIMIZER - C{MOSEK_MAX_IT} used to set opt.MSK_IPAR_INTPNT_MAX_ITERATIONS - C{MOSEK_GAP_TOL} used to set opt.MSK_DPAR_INTPNT_TOL_REL_GAP - C{MOSEK_MAX_TIME} used to set opt.MSK_DPAR_OPTIMIZER_MAX_TIME - C{MOSEK_NUM_THREADS} used to set opt.MSK_IPAR_INTPNT_NUM_THREADS - C{MOSEK_OPT} user option file, if ppopt['MOSEK_OPT'] is non-zero it is appended to 'mosek_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax:: modified_opt = fname(default_opt, ppopt) Output is a param dict to pass to MOSEKOPT. Example: If PPOPT['MOSEK_OPT'] = 3, then after setting the default MOSEK options, L{mosek_options} will execute the following user-defined function to allow option overrides:: opt = mosek_user_options_3(opt, ppopt) The contents of mosek_user_options_3.py, could be something like:: def mosek_user_options_3(opt, ppopt): opt = {} opt.MSK_DPAR_INTPNT_TOL_DFEAS = 1e-9 opt.MSK_IPAR_SIM_MAX_ITERATIONS = 5000000 return opt See the Parameters reference in Appix E of "The MOSEK optimization toolbox for MATLAB manaul" for details on the available options. U{http://www.mosek.com/documentation/} @see: C{mosekopt}, L{ppoption}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ##----- initialization and arg handling ----- ## defaults verbose = 2 gaptol = 0 fname = '' ## get symbolic constant names r, res = mosekopt('symbcon echo(0)') sc = res['symbcon'] ## second argument if ppopt == None: if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is ppopt (MATPOWER options vector) have_ppopt = True verbose = ppopt['VERBOSE'] if ppopt['MOSEK_OPT']: fname = 'mosek_user_options_#d' # ppopt['MOSEK_OPT'] else: have_ppopt = False opt = {} ##----- set default options for MOSEK ----- ## solution algorithm if have_ppopt: alg = ppopt['MOSEK_LP_ALG'] if alg == sc['MSK_OPTIMIZER_FREE'] or \ alg == sc['MSK_OPTIMIZER_INTPNT'] or \ alg == sc['MSK_OPTIMIZER_PRIMAL_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_DUAL_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_PRIMAL_DUAL_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_FREE_SIMPLEX'] or \ alg == sc['MSK_OPTIMIZER_CONCURRENT']: opt['MSK_IPAR_OPTIMIZER'] = alg else: opt['MSK_IPAR_OPTIMIZER'] = sc['MSK_OPTIMIZER_FREE'] ## (make default OPF_VIOLATION correspond to default MSK_DPAR_INTPNT_TOL_PFEAS) opt['MSK_DPAR_INTPNT_TOL_PFEAS'] = ppopt['OPF_VIOLATION'] / 500 if ppopt['MOSEK_MAX_IT']: opt['MSK_IPAR_INTPNT_MAX_ITERATIONS'] = ppopt['MOSEK_MAX_IT'] if ppopt['MOSEK_GAP_TOL']: opt['MSK_DPAR_INTPNT_TOL_REL_GAP'] = ppopt['MOSEK_GAP_TOL'] if ppopt['MOSEK_MAX_TIME']: opt['MSK_DPAR_OPTIMIZER_MAX_TIME'] = ppopt['MOSEK_MAX_TIME'] if ppopt['MOSEK_NUM_THREADS']: opt['MSK_IPAR_INTPNT_NUM_THREADS'] = ppopt['MOSEK_NUM_THREADS'] else: opt['MSK_IPAR_OPTIMIZER'] = sc['MSK_OPTIMIZER_FREE'] # opt['MSK_DPAR_INTPNT_TOL_PFEAS'] = 1e-8 ## primal feasibility tol # opt['MSK_DPAR_INTPNT_TOL_DFEAS'] = 1e-8 ## dual feasibility tol # opt['MSK_DPAR_INTPNT_TOL_MU_RED'] = 1e-16 ## relative complementarity gap tol # opt['MSK_DPAR_INTPNT_TOL_REL_GAP'] = 1e-8 ## relative gap termination tol # opt['MSK_IPAR_INTPNT_MAX_ITERATIONS'] = 400 ## max iterations for int point # opt['MSK_IPAR_SIM_MAX_ITERATIONS'] = 10000000 ## max iterations for simplex # opt['MSK_DPAR_OPTIMIZER_MAX_TIME'] = -1 ## max time allowed (< 0 --> Inf) # opt['MSK_IPAR_INTPNT_NUM_THREADS'] = 1 ## number of threads # opt['MSK_IPAR_PRESOLVE_USE'] = sc['MSK_PRESOLVE_MODE_OFF'] # if verbose == 0: # opt['MSK_IPAR_LOG'] = 0 # ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = overrides.keys() for k in range(len(names)): opt[names[k]] = overrides[names[k]]
def cplex_options(overrides=None, ppopt=None): """Sets options for CPLEX. Sets the values for the options dict normally passed to C{cplexoptimset}. Inputs are all optional, second argument must be either a string (C{fname}) or a dict (C{ppopt}): Output is an options dict to pass to C{cplexoptimset}. Example: If C{ppopt['CPLEX_OPT'] = 3}, then after setting the default CPLEX options, CPLEX_OPTIONS will execute the following user-defined function to allow option overrides:: opt = cplex_user_options_3(opt, ppopt) The contents of cplex_user_options_3.py, could be something like:: def cplex_user_options_3(opt, ppopt): opt = {} opt['threads'] = 2 opt['simplex']['refactor'] = 1 opt['timelimit'] = 10000 return opt For details on the available options, see the I{"Parameters Reference Manual"} section of the CPLEX documentation at: U{http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r2/} @param overrides: - dict 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) @param ppopt: PYPOWER options vector, uses the following entries: - OPF_VIOLATION - used to set opt.simplex.tolerances.feasibility - VERBOSE - used to set opt.barrier.display, opt.conflict.display, opt.mip.display, opt.sifting.display, opt.simplex.display, opt.tune.display - CPLEX_LPMETHOD - used to set opt.lpmethod - CPLEX_QPMETHOD - used to set opt.qpmethod - CPLEX_OPT - user option file, if ppopt['CPLEX_OPT'] is non-zero it is appended to 'cplex_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax:: modified_opt = fname(default_opt, ppopt) @see: C{cplexlp}, C{cplexqp}, L{ppoption}. @author: Ray Zimmerman (PSERC Cornell) """ ##----- initialization and arg handling ----- ## defaults verbose = 1 feastol = 1e-6 fname = '' ## second argument if ppopt != None: if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is ppopt (MATPOWER options vector) have_ppopt = True ## (make default OPF_VIOLATION correspond to default CPLEX feastol) feastol = ppopt['OPF_VIOLATION'] / 5 verbose = ppopt['VERBOSE'] lpmethod = ppopt['CPLEX_LPMETHOD'] qpmethod = ppopt['CPLEX_QPMETHOD'] if ppopt['CPLEX_OPT']: fname = 'cplex_user_options_#d' % ppopt['CPLEX_OPT'] else: have_ppopt = False ##----- set default options for CPLEX ----- opt = cplexoptimset('cplex') opt['simplex']['tolerances']['feasibility'] = feastol ## printing vrb = max([0, verbose - 1]) opt['barrier']['display'] = vrb opt['conflict']['display'] = vrb opt['mip']['display'] = vrb opt['sifting']['display'] = vrb opt['simplex']['display'] = vrb opt['tune']['display'] = vrb ## solution algorithm if have_ppopt: opt['lpmethod'] = lpmethod opt['qpmethod'] = qpmethod #else: # opt['lpmethod'] = 2 # opt['qpmethod'] = 2 ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = overrides.keys() for k in range(len(names)): if isinstance(overrides[names[k]], dict): names2 = overrides[names[k]].keys() for k2 in range(len(names2)): if isinstance(overrides[names[k]][names2[k2]], dict): names3 = overrides[names[k]][names2[k2]].keys() for k3 in range(len(names3)): opt[names[k]][names2[k2]][names3[k3]] = overrides[ names[k]][names2[k2]][names3[k3]] else: opt[names[k]][names2[k2]] = overrides[names[k]][ names2[k2]] else: opt[names[k]] = overrides[names[k]] return opt
def cplex_options(overrides=None, ppopt=None): """Sets options for CPLEX. Sets the values for the options dict normally passed to C{cplexoptimset}. Inputs are all optional, second argument must be either a string (C{fname}) or a dict (C{ppopt}): Output is an options dict to pass to C{cplexoptimset}. Example: If C{ppopt['CPLEX_OPT'] = 3}, then after setting the default CPLEX options, CPLEX_OPTIONS will execute the following user-defined function to allow option overrides:: opt = cplex_user_options_3(opt, ppopt) The contents of cplex_user_options_3.py, could be something like:: def cplex_user_options_3(opt, ppopt): opt = {} opt['threads'] = 2 opt['simplex']['refactor'] = 1 opt['timelimit'] = 10000 return opt For details on the available options, see the I{"Parameters Reference Manual"} section of the CPLEX documentation at: U{http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r2/} @param overrides: - dict 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) @param ppopt: PYPOWER options vector, uses the following entries: - OPF_VIOLATION - used to set opt.simplex.tolerances.feasibility - VERBOSE - used to set opt.barrier.display, opt.conflict.display, opt.mip.display, opt.sifting.display, opt.simplex.display, opt.tune.display - CPLEX_LPMETHOD - used to set opt.lpmethod - CPLEX_QPMETHOD - used to set opt.qpmethod - CPLEX_OPT - user option file, if ppopt['CPLEX_OPT'] is non-zero it is appended to 'cplex_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax:: modified_opt = fname(default_opt, ppopt) @see: C{cplexlp}, C{cplexqp}, L{ppoption}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ##----- initialization and arg handling ----- ## defaults verbose = 1 feastol = 1e-6 fname = '' ## second argument if ppopt != None: if isinstance(ppopt, basestring): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is ppopt (MATPOWER options vector) have_ppopt = True ## (make default OPF_VIOLATION correspond to default CPLEX feastol) feastol = ppopt['OPF_VIOLATION'] / 5 verbose = ppopt['VERBOSE'] lpmethod = ppopt['CPLEX_LPMETHOD'] qpmethod = ppopt['CPLEX_QPMETHOD'] if ppopt['CPLEX_OPT']: fname = 'cplex_user_options_#d' % ppopt['CPLEX_OPT'] else: have_ppopt = False ##----- set default options for CPLEX ----- opt = cplexoptimset('cplex') opt['simplex']['tolerances']['feasibility'] = feastol ## printing vrb = max([0, verbose - 1]) opt['barrier']['display'] = vrb opt['conflict']['display'] = vrb opt['mip']['display'] = vrb opt['sifting']['display'] = vrb opt['simplex']['display'] = vrb opt['tune']['display'] = vrb ## solution algorithm if have_ppopt: opt['lpmethod'] = lpmethod opt['qpmethod'] = qpmethod #else: # opt['lpmethod'] = 2 # opt['qpmethod'] = 2 ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = overrides.keys() for k in range(len(names)): if isinstance(overrides[names[k]], dict): names2 = overrides[names[k]].keys() for k2 in range(len(names2)): if isinstance(overrides[names[k]][names2[k2]], dict): names3 = overrides[names[k]][names2[k2]].keys() for k3 in range(len(names3)): opt[names[k]][names2[k2]][names3[k3]] = overrides[names[k]][names2[k2]][names3[k3]] else: opt[names[k]][names2[k2]] = overrides[names[k]][names2[k2]] else: opt[names[k]] = overrides[names[k]] return opt #-------------------------- Default Options Struct -------------------------- # as returned by ... # >> opt = cplexoptimset('cplex') # # opt = # advance: 1 # barrier: [1x1 struct] # algorithm: 0 # colnonzeros: 0 # convergetol: 1.0000e-08 # crossover: 0 # display: 1 # limits: [1x1 struct] # corrections: -1 # growth: 1.0000e+12 # iteration: 9.2234e+18 # objrange: 1.0000e+20 # ordering: 0 # qcpconvergetol: 1.0000e-07 # startalg: 1 # clocktype: 2 # conflict: [1x1 struct] # display: 1 # diagnostics: 'off' # emphasis: [1x1 struct] # memory: 0 # mip: 0 # numerical: 0 # exportmodel: '' # feasopt: [1x1 struct] # mode: 0 # tolerance: 1.0000e-06 # lpmethod: 0 # mip: [1x1 struct] # cuts: [1x1 struct] # cliques: 0 # covers: 0 # disjunctive: 0 # flowcovers: 0 # gomory: 0 # gubcovers: 0 # implied: 0 # mcfcut: 0 # mircut: 0 # pathcut: 0 # zerohalfcut: 0 # display: 2 # interval: 0 # limits: [1x1 struct] # aggforcut: 3 # auxrootthreads: 0 # cutpasses: 0 # cutsfactor: 4 # eachcutlimit: 2.1000e+09 # gomorycand: 200 # gomorypass: 0 # nodes: 9.2234e+18 # polishtime: 0 # populate: 20 # probetime: 1.0000e+75 # repairtries: 0 # solutions: 9.2234e+18 # strongcand: 10 # strongit: 0 # submipnodelim: 500 # treememory: 1.0000e+75 # ordertype: 0 # polishafter: [1x1 struct] # absmipgap: 0 # mipgap: 0 # nodes: 9.2234e+18 # solutions: 9.2234e+18 # time: 1.0000e+75 # pool: [1x1 struct] # absgap: 1.0000e+75 # capacity: 2.1000e+09 # intensity: 0 # relgap: 1.0000e+75 # replace: 0 # strategy: [1x1 struct] # backtrack: 0.9999 # bbinterval: 7 # branch: 0 # dive: 0 # file: 1 # fpheur: 0 # heuristicfreq: 0 # kappastats: 0 # lbheur: 0 # miqcpstrat: 0 # nodeselect: 1 # order: 1 # presolvenode: 0 # probe: 0 # rinsheur: 0 # search: 0 # startalgorithm: 0 # subalgorithm: 0 # variableselect: 0 # tolerances: [1x1 struct] # absmipgap: 1.0000e-06 # integrality: 1.0000e-05 # lowercutoff: -1.0000e+75 # mipgap: 1.0000e-04 # objdifference: 0 # relobjdifference: 0 # uppercutoff: 1.0000e+75 # output: [1x1 struct] # clonelog: 1 # intsolfileprefix: '' # mpslong: 1 # writelevel: 0 # parallel: 0 # preprocessing: [1x1 struct] # aggregator: -1 # boundstrength: -1 # coeffreduce: -1 # depency: -1 # dual: 0 # fill: 10 # linear: 1 # numpass: -1 # presolve: 1 # qpmakepsd: 1 # reduce: 3 # relax: -1 # repeatpresolve: -1 # symmetry: -1 # qpmethod: 0 # read: [1x1 struct] # apiencoding: '' # constraints: 30000 # datacheck: 0 # fileencoding: 'ISO-8859-1' # nonzeros: 250000 # qpnonzeros: 5000 # scale: 0 # variables: 60000 # sifting: [1x1 struct] # algorithm: 0 # display: 1 # iterations: 9.2234e+18 # simplex: [1x1 struct] # crash: 1 # dgradient: 0 # display: 1 # limits: [1x1 struct] # iterations: 9.2234e+18 # lowerobj: -1.0000e+75 # perturbation: 0 # singularity: 10 # upperobj: 1.0000e+75 # perturbation: [1x1 struct] # indicator: 0 # constant: 1.0000e-06 # pgradient: 0 # pricing: 0 # refactor: 0 # tolerances: [1x1 struct] # feasibility: 1.0000e-06 # markowitz: 0.0100 # optimality: 1.0000e-06 # solutiontarget: 0 # threads: 0 # timelimit: 1.0000e+75 # tune: [1x1 struct] # display: 1 # measure: 1 # repeat: 1 # timelimit: 10000 # workdir: '.' # workmem: 128
def gurobi_options(overrides=None, ppopt=None): """Sets options for GUROBI. Sets the values for the options dict normally passed to GUROBI_MEX. Inputs are all optional, second argument must be either a string (fname) or a vector (ppopt): overrides - dict 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) ppopt - PYPOWER options vector, uses the following entries: OPF_VIOLATION (16) - used to set opt.FeasibilityTol VERBOSE (31) - used to set opt.DisplayInterval, opt.Display GRB_METHOD (121) - used to set opt.Method GRB_TIMELIMIT (122) - used to set opt.TimeLimit (seconds) GRB_THREADS (123) - used to set opt.Threads GRB_OPT (124) - user option file, if PPOPT(124) is non-zero it is appended to 'gurobi_user_options_' to form the name of a user-supplied function used as C{fname} described above, except with calling syntax: modified_opt = fname(default_opt, mpopt) Output is an options struct to pass to GUROBI_MEX. Example: If ppopt['GRB_OPT'] = 3, then after setting the default GUROBI options, GUROBI_OPTIONS will execute the following user-defined function to allow option overrides: opt = gurobi_user_options_3(opt, ppopt) The contents of gurobi_user_options_3.py, could be something like: def gurobi_user_options_3(opt, ppopt): opt = {} opt['OptimalityTol'] = 1e-9 opt['IterationLimit'] = 3000 opt['BarIterLimit'] = 200 opt['Crossover'] = 0 opt['Presolve'] = 0 return opt For details on the available options, see the "Parameters" section of the "Gurobi Optimizer Reference Manual" at: http://www.gurobi.com/doc/45/refman/ @see: L{gurobi_mex}, L{ppoption}. """ ##----- initialization and arg handling ----- ## defaults verbose = True fname = '' ## second argument if ppopt != None: if isinstance(ppopt, str): ## 2nd arg is FNAME (string) fname = ppopt have_ppopt = False else: ## 2nd arg is MPOPT (MATPOWER options vector) have_ppopt = True verbose = ppopt['VERBOSE'] if ppopt['GRB_OPT']: fname = 'gurobi_user_options_%d', ppopt['GRB_OPT'] else: have_ppopt = False ##----- set default options for CPLEX ----- opt = {} # opt['OptimalityTol'] = 1e-6 ## -1 - auto, 0 - no, 1 - conserv, 2 - aggressive= # opt['Presolve'] = -1 # opt['LogFile'] = 'qps_gurobi.log' if have_ppopt: ## (make default OPF_VIOLATION correspond to default FeasibilityTol) opt['FeasibilityTol'] = ppopt['OPF_VIOLATION'] / 5 opt['Method'] = ppopt['GRB_METHOD'] opt['TimeLimit'] = ppopt['GRB_TIMELIMIT'] opt['Threads'] = ppopt['GRB_THREADS'] else: opt['Method'] = 1 ## dual simplex opt['Display'] = min(verbose, 3) if verbose: opt['DisplayInterval'] = 1 else: opt['DisplayInterval'] = Inf ##----- call user function to modify defaults ----- if len(fname) > 0: if have_ppopt: opt = feval(fname, opt, ppopt) else: opt = feval(fname, opt) ##----- apply overrides ----- if overrides is not None: names = list(overrides.keys()) for k in range(len(names)): opt[names[k]] = overrides[names[k]] return opt