def create_command_line(self, executable, problem_files): # # Define log file # if self._log_file is None: self._log_file = TempfileManager.create_tempfile( suffix='.glpk.log') # # Define solution file # self._glpfile = TempfileManager.create_tempfile(suffix='.glpk.glp') self._rawfile = TempfileManager.create_tempfile(suffix='.glpk.raw') self._soln_file = self._rawfile # # Define command line # cmd = [executable] if self._timer: cmd.insert(0, self._timer) for key in self.options: opt = self.options[key] if opt is None or (isinstance(opt, string_types) and opt.strip() == ''): # Handle the case for options that must be # specified without a value cmd.append("--%s" % key) else: cmd.extend(["--%s" % key, str(opt)]) #if isinstance(opt, basestring) and ' ' in opt: # cmd.append('--%s "%s"' % (key, str(opt))) #else: # cmd.append('--%s %s' % (key, str(opt))) if self._timelimit is not None and self._timelimit > 0.0: cmd.extend(['--tmlim', str(self._timelimit)]) cmd.extend(['--write', self._rawfile]) cmd.extend(['--wglp', self._glpfile]) if self._problem_format == ProblemFormat.cpxlp: cmd.extend(['--cpxlp', problem_files[0]]) elif self._problem_format == ProblemFormat.mps: cmd.extend(['--freemps', problem_files[0]]) elif self._problem_format == ProblemFormat.mod: cmd.extend(['--math', problem_files[0]]) for fname in problem_files[1:]: cmd.extend(['--data', fname]) return Bunch(cmd=cmd, log_file=self._log_file, env=None)
def create_command_line(self, executable, problem_files): # # Define log file # if self._log_file is None: self._log_file = TempfileManager.create_tempfile(suffix='.glpk.log') # # Define solution file # self._glpfile = TempfileManager.create_tempfile(suffix='.glpk.glp') self._rawfile = TempfileManager.create_tempfile(suffix='.glpk.raw') self._soln_file = self._rawfile # # Define command line # cmd = [executable] if self._timer: cmd.insert(0, self._timer) for key in self.options: opt = self.options[key] if opt is None or (isinstance(opt, string_types) and opt.strip() == ''): # Handle the case for options that must be # specified without a value cmd.append("--%s" % key) else: cmd.extend(["--%s" % key, str(opt)]) #if isinstance(opt, basestring) and ' ' in opt: # cmd.append('--%s "%s"' % (key, str(opt))) #else: # cmd.append('--%s %s' % (key, str(opt))) if self._timelimit is not None and self._timelimit > 0.0: cmd.extend(['--tmlim', str(self._timelimit)]) cmd.extend(['--write', self._rawfile]) cmd.extend(['--wglp', self._glpfile]) if self._problem_format == ProblemFormat.cpxlp: cmd.extend(['--cpxlp', problem_files[0]]) elif self._problem_format == ProblemFormat.mps: cmd.extend(['--freemps', problem_files[0]]) elif self._problem_format == ProblemFormat.mod: cmd.extend(['--math', problem_files[0]]) for fname in problem_files[1:]: cmd.extend(['--data', fname]) return Bunch(cmd=cmd, log_file=self._log_file, env=None)
def solve(self, model, timer: HierarchicalTimer = None): self.available(exception_flag=True) if timer is None: timer = HierarchicalTimer() try: TempfileManager.push() if self.config.filename is None: self._filename = TempfileManager.create_tempfile() else: self._filename = self.config.filename TempfileManager.add_tempfile(self._filename + '.lp', exists=False) TempfileManager.add_tempfile(self._filename + '.soln', exists=False) TempfileManager.add_tempfile(self._filename + '.log', exists=False) timer.start('write lp file') self._writer.write(model, self._filename+'.lp', timer=timer) timer.stop('write lp file') res = self._apply_solver(timer) if self.config.report_timing: logger.info('\n' + str(timer)) return res finally: # finally, clean any temporary files registered with the # temp file manager, created/populated *directly* by this # plugin. TempfileManager.pop(remove=not self.config.keepfiles) if not self.config.keepfiles: self._filename = None if self.config.report_timing: print(timer)
def run(args=None): # # Parse command-line options. # try: options_parser = \ construct_options_parser("scenariotreeserver [options] hostname") (options, arguments) = options_parser.parse_args(args=args) if arguments: assert len(arguments) == 1 options.host = arguments[0] else: options.host = None except SystemExit as _exc: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. return _exc.code # for a one-pass execution, garbage collection doesn't make # much sense - so it is disabled by default. Because: It drops # the run-time by a factor of 3-4 on bigger instances. if options.disable_gc: gc.disable() else: gc.enable() if pstats_available and options.profile > 0: # # Call the main PH routine with profiling. # tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('run_server(options)', globals(), locals(), tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('cumulative', 'calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) TempfileManager.clear_tempfiles() ans = [tmp, None] else: # # Call the main routine without profiling. # ans = run_server(options) gc.enable() return ans
def run(args=None): # # Parse command-line options. # try: options_parser = \ construct_options_parser("scenariotreeserver [options] hostname") (options, arguments) = options_parser.parse_args(args=args) if arguments: assert len(arguments) == 1 options.host = arguments[0] else: options.host = None except SystemExit as _exc: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. return _exc.code # for a one-pass execution, garbage collection doesn't make # much sense - so it is disabled by default. Because: It drops # the run-time by a factor of 3-4 on bigger instances. if options.disable_gc: gc.disable() else: gc.enable() if pstats_available and options.profile > 0: # # Call the main PH routine with profiling. # tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('run_server(options)',globals(),locals(),tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('cumulative','calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) TempfileManager.clear_tempfiles() ans = [tmp, None] else: # # Call the main routine without profiling. # ans = run_server(options) gc.enable() return ans
def generate_scenario_tree_image(options): with ScenarioTreeInstanceFactory(options.model_location, options.scenario_tree_location) as factory: scenario_tree = factory.generate_scenario_tree( downsample_fraction=options.scenario_tree_downsample_fraction, bundles=options.scenario_bundle_specification, random_bundles=options.create_random_bundles, random_seed=options.scenario_tree_random_seed, verbose=options.verbose, ) with TempfileManager.push(): tmpdotfile = TempfileManager.create_tempfile(suffix=".dot") scenario_tree.save_to_dot(tmpdotfile) os.system("dot -Tpdf -o %s %s" % (options.output_file, tmpdotfile)) print("Output Saved To: %s" % (options.output_file))
def generate_scenario_tree_image(options): with ScenarioTreeInstanceFactory( options.model_location, options.scenario_tree_location) as factory: scenario_tree = factory.generate_scenario_tree( downsample_fraction=options.scenario_tree_downsample_fraction, bundles=options.scenario_bundle_specification, random_bundles=options.create_random_bundles, random_seed=options.scenario_tree_random_seed, verbose=options.verbose) with TempfileManager.push(): tmpdotfile = TempfileManager.create_tempfile(suffix=".dot") scenario_tree.save_to_dot(tmpdotfile) os.system('dot -Tpdf -o %s %s' % (options.output_file, tmpdotfile)) print("Output Saved To: %s" % (options.output_file))
def run_command(command=None, parser=None, args=None, name='unknown', data=None, options=None): """ Execute a function that processes command-line arguments and then calls a command-line driver. This function provides a generic facility for executing a command function is rather generic. This function is segregated from the driver to enable profiling of the command-line execution. Required: command: The name of a function that will be executed to perform process the command-line options with a parser object. parser: The parser object that is used by the command-line function. Optional: options: If this is not None, then ignore the args option and use this to specify command options. args: Command-line arguments that are parsed. If this value is `None`, then the arguments in `sys.argv` are used to parse the command-line. name: Specifying the name of the command-line (for error messages). data: A container of labeled data. Returned: retval: Return values from the command-line execution. errorcode: 0 if Pyomo ran successfully """ # # # Parse command-line options # # retval = None errorcode = 0 if options is None: try: if type(args) is argparse.Namespace: _options = args else: _options = parser.parse_args(args=args) # Replace the parser options object with a pyutilib.misc.Options object options = pyutilib.misc.Options() for key in dir(_options): if key[0] != '_': val = getattr(_options, key) if not isinstance(val, types.MethodType): options[key] = val except SystemExit: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. return Container(retval=retval, errorcode=errorcode) # # Configure loggers # configure_loggers(options=options) # # Setup I/O redirect to a file # logfile = options.runtime.logfile if not logfile is None: pyutilib.misc.setup_redirect(logfile) # # Call the main Pyomo runner with profiling # TempfileManager.push() pcount = options.runtime.profile_count if pcount > 0: if not pstats_available: if not logfile is None: pyutilib.misc.reset_redirect() msg = "Cannot use the 'profile' option. The Python 'pstats' " \ 'package cannot be imported!' raise ValueError(msg) tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx( command.__name__ + '(options=options,parser=parser)', command.__globals__, locals(), tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(pcount) p.print_callers(pcount) p.print_callees(pcount) p = p.sort_stats('cumulative', 'calls') p.print_stats(pcount) p.print_callers(pcount) p.print_callees(pcount) p = p.sort_stats('calls') p.print_stats(pcount) p.print_callers(pcount) p.print_callees(pcount) retval = tmp else: # # Call the main Pyomo runner without profiling # TempfileManager.push() try: retval = command(options=options, parser=parser) except SystemExit: err = sys.exc_info()[1] # # If debugging is enabled or the 'catch' option is specified, then # exit. Otherwise, print an "Exiting..." message. # if __debug__ and (options.runtime.logging == 'debug' or options.runtime.catch_errors): sys.exit(0) print('Exiting %s: %s' % (name, str(err))) errorcode = err.code except Exception: err = sys.exc_info()[1] # # If debugging is enabled or the 'catch' option is specified, then # pass the exception up the chain (to pyomo_excepthook) # if __debug__ and (options.runtime.logging == 'debug' or options.runtime.catch_errors): if not logfile is None: pyutilib.misc.reset_redirect() TempfileManager.pop(remove=not options.runtime.keep_files) raise if not options.model is None and not options.model.save_file is None: model = "model " + options.model.save_file else: model = "model" global filter_excepthook if filter_excepthook: action = "loading" else: action = "running" msg = "Unexpected exception while %s %s:\n" % (action, model) # # This handles the case where the error is propagated by a KeyError. # KeyError likes to pass raw strings that don't handle newlines # (they translate "\n" to "\\n"), as well as tacking on single # quotes at either end of the error message. This undoes all that. # errStr = str(err) if type(err) == KeyError and errStr != "None": errStr = str(err).replace(r"\n", "\n")[1:-1] logging.getLogger('pyomo.core').error(msg + errStr) errorcode = 1 if not logfile is None: pyutilib.misc.reset_redirect() if options.runtime.disable_gc: gc.enable() TempfileManager.pop(remove=not options.runtime.keep_files) return Container(retval=retval, errorcode=errorcode)
def _presolve(self, *args, **kwds): # create a context in the temporary file manager for # this plugin - is "pop"ed in the _postsolve method. TempfileManager.push() # if the first argument is a string (representing a filename), # then we don't have an instance => the solver is being applied # to a file. self._warm_start_solve = kwds.pop('warmstart', False) self._warm_start_file_name = _validate_file_name( self, kwds.pop('warmstart_file', None), "warm start") user_warmstart = self._warm_start_file_name is not None # the input argument can currently be one of two things: an instance or a filename. # if a filename is provided and a warm-start is indicated, we go ahead and # create the temporary file - assuming that the user has already, via some external # mechanism, invoked warm_start() with a instance to create the warm start file. if self._warm_start_solve and \ isinstance(args[0], basestring): # we assume the user knows what they are doing... pass elif self._warm_start_solve and \ (not isinstance(args[0], basestring)): # assign the name of the warm start file *before* calling the base class # presolve - the base class method ends up creating the command line, # and the warm start file-name is (obviously) needed there. if self._warm_start_file_name is None: assert not user_warmstart self._warm_start_file_name = TempfileManager.\ create_tempfile(suffix = '.cplex.mst') self._priorities_solve = kwds.pop("priorities", False) self._priorities_file_name = _validate_file_name( self, kwds.pop("priorities_file", None), "branching priorities") user_priorities = self._priorities_file_name is not None if (self._priorities_solve and not isinstance(args[0], basestring) and not user_priorities): self._priorities_file_name = TempfileManager.create_tempfile( suffix=".cplex.ord") # let the base class handle any remaining keywords/actions. ILMLicensedSystemCallSolver._presolve(self, *args, **kwds) # NB: we must let the base class presolve run first so that the # symbol_map is actually constructed! if (len(args) > 0) and (not isinstance(args[0], basestring)): if len(args) != 1: raise ValueError("CPLEX _presolve method can only handle a " "single problem instance - %s were supplied" % (len(args), )) # write the warm-start file - currently only supports MIPs. # we only know how to deal with a single problem instance. if self._warm_start_solve and (not user_warmstart): start_time = time.time() self._warm_start(args[0]) end_time = time.time() if self._report_timing: print("Warm start write time= %.2f seconds" % (end_time - start_time)) if self._priorities_solve and (not user_priorities): start_time = time.time() self._write_priorities_file(args[0]) end_time = time.time() if self._report_timing: print("Branching priorities write time= %.2f seconds" % (end_time - start_time))
def launch_command(command, options, cmd_args=None, cmd_kwds=None, error_label="", disable_gc=False, profile_count=0, traceback=False): if cmd_args is None: cmd_args = () if cmd_kwds is None: cmd_kwds = {} # # Control the garbage collector - more critical than I would like # at the moment. # with PauseGC(disable_gc) as pgc: # # Run command - precise invocation depends on whether we want # profiling output, traceback, etc. # rc = 0 if pstats_available and (profile_count > 0): # # Call the main routine with profiling. # tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('command(options, *cmd_args, **cmd_kwds)', globals(), locals(), tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) p = p.sort_stats('cumulative','calls') p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) p = p.sort_stats('calls') p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) TempfileManager.clear_tempfiles() rc = tmp else: # # Call the main PH routine without profiling. # if traceback: rc = command(options, *cmd_args, **cmd_kwds) else: try: try: rc = command(options, *cmd_args, **cmd_kwds) except ValueError: sys.stderr.write(error_label+"VALUE ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except KeyError: sys.stderr.write(error_label+"KEY ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except TypeError: sys.stderr.write(error_label+"TYPE ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except NameError: sys.stderr.write(error_label+"NAME ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except IOError: sys.stderr.write(error_label+"IO ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except ConverterError: sys.stderr.write(error_label+"CONVERTER ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except pyutilib.common.ApplicationError: sys.stderr.write(error_label+"APPLICATION ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except RuntimeError: sys.stderr.write(error_label+"RUN-TIME ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except: sys.stderr.write(error_label+ "Encountered unhandled exception:\n") if len(sys.exc_info()) > 1: sys.stderr.write(str(sys.exc_info()[1])+"\n") else: traceback.print_exc(file=sys.stderr) raise except: sys.stderr.write("\n") sys.stderr.write( "To obtain further information regarding the " "source of the exception, use the " "--traceback option\n") rc = 1 # # TODO: Once we incorporate options registration into # all of the PySP commands we will assume the # options object is always a PySPConfigBlock # if isinstance(options, PySPConfigBlock): ignored_options = dict((_c._name, _c.value(False)) for _c in options.unused_user_values()) if len(ignored_options): print("") print("*** WARNING: The following options were " "explicitly set but never accessed during " "execution of this command:") for name in ignored_options: print(" - %s: %s" % (name, ignored_options[name])) print("*** If you believe this is a bug, please report it " "to the PySP developers.") print("") return rc
def setUp(self): self.mock_model = self.get_mock_model() self.mock_cplex_shell = self.get_mock_cplex_shell(self.mock_model) self.mock_cplex_shell._priorities_file_name = TempfileManager.create_tempfile( suffix=".cplex.ord")
def run_command(command=None, parser=None, args=None, name='unknown', data=None, options=None): """ Execute a function that processes command-line arguments and then calls a command-line driver. This function provides a generic facility for executing a command function is rather generic. This function is segregated from the driver to enable profiling of the command-line execution. Required: command: The name of a function that will be executed to perform process the command-line options with a parser object. parser: The parser object that is used by the command-line function. Optional: options: If this is not None, then ignore the args option and use this to specify command options. args: Command-line arguments that are parsed. If this value is `None`, then the arguments in `sys.argv` are used to parse the command-line. name: Specifying the name of the command-line (for error messages). data: A container of labeled data. Returned: retval: Return values from the command-line execution. errorcode: 0 if Pyomo ran successfully """ # # # Parse command-line options # # retval = None errorcode = 0 if options is None: try: if type(args) is argparse.Namespace: _options = args else: _options = parser.parse_args(args=args) # Replace the parser options object with a pyutilib.misc.Options object options = pyutilib.misc.Options() for key in dir(_options): if key[0] != '_': val = getattr(_options, key) if not isinstance(val, types.MethodType): options[key] = val except SystemExit: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. return Container(retval=retval, errorcode=errorcode) # # Configure loggers # configure_loggers(options=options) # # Setup I/O redirect to a file # logfile = options.runtime.logfile if not logfile is None: pyutilib.misc.setup_redirect(logfile) # # Call the main Pyomo runner with profiling # TempfileManager.push() pcount = options.runtime.profile_count if pcount > 0: if not pstats_available: if not logfile is None: pyutilib.misc.reset_redirect() msg = "Cannot use the 'profile' option. The Python 'pstats' " \ 'package cannot be imported!' raise ValueError(msg) tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx( command.__name__ + '(options=options,parser=parser)', command.__globals__, locals(), tfile ) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(pcount) p.print_callers(pcount) p.print_callees(pcount) p = p.sort_stats('cumulative','calls') p.print_stats(pcount) p.print_callers(pcount) p.print_callees(pcount) p = p.sort_stats('calls') p.print_stats(pcount) p.print_callers(pcount) p.print_callees(pcount) retval = tmp else: # # Call the main Pyomo runner without profiling # TempfileManager.push() try: retval = command(options=options, parser=parser) except SystemExit: err = sys.exc_info()[1] # # If debugging is enabled or the 'catch' option is specified, then # exit. Otherwise, print an "Exiting..." message. # if __debug__ and (options.runtime.logging == 'debug' or options.runtime.catch_errors): sys.exit(0) print('Exiting %s: %s' % (name, str(err))) errorcode = err.code except Exception: err = sys.exc_info()[1] # # If debugging is enabled or the 'catch' option is specified, then # pass the exception up the chain (to pyomo_excepthook) # if __debug__ and (options.runtime.logging == 'debug' or options.runtime.catch_errors): if not logfile is None: pyutilib.misc.reset_redirect() TempfileManager.pop(remove=not options.runtime.keep_files) raise if not options.model is None and not options.model.save_file is None: model = "model " + options.model.save_file else: model = "model" global filter_excepthook if filter_excepthook: action = "loading" else: action = "running" msg = "Unexpected exception while %s %s:\n" % (action, model) # # This handles the case where the error is propagated by a KeyError. # KeyError likes to pass raw strings that don't handle newlines # (they translate "\n" to "\\n"), as well as tacking on single # quotes at either end of the error message. This undoes all that. # errStr = str(err) if type(err) == KeyError and errStr != "None": errStr = str(err).replace(r"\n","\n")[1:-1] logging.getLogger('pyomo.core').error(msg+errStr) errorcode = 1 if not logfile is None: pyutilib.misc.reset_redirect() if options.runtime.disable_gc: gc.enable() TempfileManager.pop(remove=not options.runtime.keep_files) return Container(retval=retval, errorcode=errorcode)
def create_command_line(self, executable, problem_files): # # Define log file # The log file in CPLEX contains the solution trace, but the solver status can be found in the solution file. # if self._log_file is None: self._log_file = TempfileManager.\ create_tempfile(suffix = '.gurobi.log') # # Define solution file # As indicated above, contains (in XML) both the solution and solver status. # if self._soln_file is None: self._soln_file = TempfileManager.\ create_tempfile(suffix = '.gurobi.txt') # # Write the GUROBI execution script # problem_filename = self._problem_files[0] solution_filename = self._soln_file warmstart_filename = self._warm_start_file_name # translate the options into a normal python dictionary, from a # pyutilib SectionWrapper - the # gurobi_run function doesn't know about pyomo, so the translation # is necessary. options_dict = {} for key in self.options: options_dict[key] = self.options[key] # NOTE: the gurobi shell is independent of Pyomo python virtualized environment, so any # imports - specifically that required to get GUROBI_RUN - must be handled explicitly. # NOTE: The gurobi plugin (GUROBI.py) and GUROBI_RUN.py live in the same directory. script = "import sys\n" script += "from gurobipy import *\n" script += "sys.path.append(%r)\n" % os.path.dirname(__file__) script += "from GUROBI_RUN import *\n" script += "gurobi_run(" mipgap = float(self.options.mipgap) if \ self.options.mipgap is not None else \ None for x in (problem_filename, warmstart_filename, solution_filename, None, options_dict, self._suffixes): script += "%r," % x script += ")\n" script += "quit()\n" # dump the script and warm-start file names for the # user if we're keeping files around. if self._keepfiles: script_fname = TempfileManager.create_tempfile( suffix='.gurobi.script') script_file = open(script_fname, 'w') script_file.write(script) script_file.close() print("Solver script file: '%s'" % script_fname) if self._warm_start_solve and \ (self._warm_start_file_name is not None): print("Solver warm-start file: " + self._warm_start_file_name) # # Define command line # cmd = [executable] if self._timer: cmd.insert(0, self._timer) return Bunch(cmd=cmd, script=script, log_file=self._log_file, env=None)
def launch_command(command, options, cmd_args=None, cmd_kwds=None, error_label="", disable_gc=False, profile_count=0, log_level=logging.INFO, traceback=False): # This is not the effective level, but the # level on the current logger. We want to # return the logger to its original state # before this function exits prev_log_level = logger.level logger.setLevel(log_level) if cmd_args is None: cmd_args = () if cmd_kwds is None: cmd_kwds = {} # # Control the garbage collector - more critical than I would like # at the moment. # with PauseGC(disable_gc) as pgc: # # Run command - precise invocation depends on whether we want # profiling output, traceback, etc. # rc = 0 if pstats_available and (profile_count > 0): # # Call the main routine with profiling. # try: tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('command(options, *cmd_args, **cmd_kwds)', globals(), locals(), tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) p = p.sort_stats('cumulative','calls') p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) p = p.sort_stats('calls') p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) TempfileManager.clear_tempfiles() rc = tmp finally: logger.setLevel(prev_log_level) else: # # Call the main PH routine without profiling. # if traceback: try: rc = command(options, *cmd_args, **cmd_kwds) finally: logger.setLevel(prev_log_level) else: try: try: rc = command(options, *cmd_args, **cmd_kwds) except ValueError: sys.stderr.write(error_label+"VALUE ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except KeyError: sys.stderr.write(error_label+"KEY ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except TypeError: sys.stderr.write(error_label+"TYPE ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except NameError: sys.stderr.write(error_label+"NAME ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except IOError: sys.stderr.write(error_label+"IO ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except ConverterError: sys.stderr.write(error_label+"CONVERTER ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except pyutilib.common.ApplicationError: sys.stderr.write(error_label+"APPLICATION ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except RuntimeError: sys.stderr.write(error_label+"RUN-TIME ERROR:\n") sys.stderr.write(str(sys.exc_info()[1])+"\n") raise except: sys.stderr.write(error_label+ "Encountered unhandled exception:\n") if len(sys.exc_info()) > 1: sys.stderr.write(str(sys.exc_info()[1])+"\n") else: traceback.print_exc(file=sys.stderr) raise except: sys.stderr.write("\n") sys.stderr.write( "To obtain further information regarding the " "source of the exception, use the " "--traceback option\n") rc = 1 # # TODO: Once we incorporate options registration into # all of the PySP commands we will assume the # options object is always a PySPConfigBlock # if isinstance(options, PySPConfigBlock): options.check_usage(error=False) logger.setLevel(prev_log_level) return rc
def create_command_line(self, executable, problem_files): # # Define the log file # if self._log_file is None: self._log_file = TempfileManager.create_tempfile(suffix=".cbc.log") # # Define the solution file # # the prefix of the problem filename is required because CBC has a specific # and automatic convention for generating the output solution filename. # the extracted prefix is the same name as the input filename, e.g., minus # the ".lp" extension. problem_filename_prefix = problem_files[0] if '.' in problem_filename_prefix: tmp = problem_filename_prefix.split('.') if len(tmp) > 2: problem_filename_prefix = '.'.join(tmp[:-1]) else: problem_filename_prefix = tmp[0] if self._results_format is ResultsFormat.sol: self._soln_file = problem_filename_prefix + ".sol" else: self._soln_file = problem_filename_prefix + ".soln" # # Define the results file (if the sol external parser is used) # # results in CBC are split across the log file (solver statistics) and # the solution file (solutions!) if self._results_format is ResultsFormat.sol: self._results_file = self._soln_file def _check_and_escape_options(options): for key, val in iteritems(self.options): tmp_k = str(key) _bad = ' ' in tmp_k tmp_v = str(val) if ' ' in tmp_v: if '"' in tmp_v: if "'" in tmp_v: _bad = True else: tmp_v = "'" + tmp_v + "'" else: tmp_v = '"' + tmp_v + '"' if _bad: raise ValueError("Unable to properly escape solver option:" "\n\t%s=%s" % (key, val)) yield (tmp_k, tmp_v) # # Define command line # cmd = [executable] if self._timer: cmd.insert(0, self._timer) if self._problem_format == ProblemFormat.nl: cmd.append(problem_files[0]) cmd.append('-AMPL') if self._timelimit is not None and self._timelimit > 0.0: cmd.extend(['-sec', str(self._timelimit)]) cmd.extend(['-timeMode', "elapsed"]) if "debug" in self.options: cmd.extend(["-log", "5"]) for key, val in _check_and_escape_options(self.options): if key == 'solver': continue cmd.append(key + "=" + val) os.environ['cbc_options'] = "printingOptions=all" #cmd.extend(["-printingOptions=all", #"-stat"]) else: if self._timelimit is not None and self._timelimit > 0.0: cmd.extend(['-sec', str(self._timelimit)]) cmd.extend(['-timeMode', "elapsed"]) if "debug" in self.options: cmd.extend(["-log", "5"]) # these must go after options that take a value action_options = [] for key, val in _check_and_escape_options(self.options): if val.strip() != '': cmd.extend(['-' + key, val]) else: action_options.append('-' + key) cmd.extend( ["-printingOptions", "all", "-import", problem_files[0]]) cmd.extend(action_options) if self._warm_start_solve: cmd.extend(["-mipstart", self._warm_start_file_name]) cmd.extend(["-stat=1", "-solve", "-solu", self._soln_file]) return Bunch(cmd=cmd, log_file=self._log_file, env=None)
def create_command_line(self, executable, problem_files): # # Define log file # The log file in XPRESS contains the solution trace, but the solver status can be found in the solution file. # if self._log_file is None: self._log_file = TempfileManager.\ create_tempfile(suffix = '.xpress.log') # # Define solution file # As indicated above, contains (in XML) both the solution and solver status. # self._soln_file = TempfileManager.\ create_tempfile(suffix = '.xpress.wrtsol') # # Write the XPRESS execution script # script = "" script = "setlogfile %s\n" % (self._log_file, ) if self._timelimit is not None and self._timelimit > 0.0: script += "maxtime=%s\n" % (self._timelimit, ) if (self.options.mipgap is not None) and (self.options.mipgap > 0.0): mipgap = self.options.pop('mipgap') script += "miprelstop=%s\n" % (mipgap, ) for option_name in self.options: script += "%s=%s\n" % (option_name, self.options[option_name]) script += "readprob %s\n" % (problem_files[0], ) # doesn't seem to be a global solve command for mip versus lp # solves if self.is_mip: script += "mipoptimize\n" else: script += "lpoptimize\n" # a quick explanation of the various flags used below: # p: outputs in full precision # n: output the name # t: output the type # a: output the activity (value) # c: outputs the costs for variables, slacks for constraints. # d: outputs the reduced costs for columns, duals for constraints script += "writesol %s -pnatcd\n" % (self._soln_file, ) script += "quit\n" # dump the script and warm-start file names for the # user if we're keeping files around. if self._keepfiles: script_fname = TempfileManager.create_tempfile( suffix='.xpress.script') tmp = open(script_fname, 'w') tmp.write(script) tmp.close() print("Solver script file=" + script_fname) # # Define command line # cmd = [executable] if self._timer: cmd.insert(0, self._timer) return Bunch(cmd=cmd, script=script, log_file=self._log_file, env=None)
def launch_command(command, options, cmd_args=None, cmd_kwds=None, error_label="", disable_gc=False, profile_count=0, log_level=logging.INFO, traceback=False): # This is not the effective level, but the # level on the current logger. We want to # return the logger to its original state # before this function exits prev_log_level = logger.level logger.setLevel(log_level) if cmd_args is None: cmd_args = () if cmd_kwds is None: cmd_kwds = {} # # Control the garbage collector - more critical than I would like # at the moment. # with PauseGC(disable_gc) as pgc: # # Run command - precise invocation depends on whether we want # profiling output, traceback, etc. # rc = 0 if pstats_available and (profile_count > 0): # # Call the main routine with profiling. # try: tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('command(options, *cmd_args, **cmd_kwds)', globals(), locals(), tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) p = p.sort_stats('cumulative', 'calls') p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) p = p.sort_stats('calls') p.print_stats(profile_count) p.print_callers(profile_count) p.print_callees(profile_count) TempfileManager.clear_tempfiles() rc = tmp finally: logger.setLevel(prev_log_level) else: # # Call the main PH routine without profiling. # if traceback: try: rc = command(options, *cmd_args, **cmd_kwds) finally: logger.setLevel(prev_log_level) else: try: try: rc = command(options, *cmd_args, **cmd_kwds) except ValueError: sys.stderr.write(error_label + "VALUE ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except KeyError: sys.stderr.write(error_label + "KEY ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except TypeError: sys.stderr.write(error_label + "TYPE ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except NameError: sys.stderr.write(error_label + "NAME ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except IOError: sys.stderr.write(error_label + "IO ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except ConverterError: sys.stderr.write(error_label + "CONVERTER ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except pyutilib.common.ApplicationError: sys.stderr.write(error_label + "APPLICATION ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except RuntimeError: sys.stderr.write(error_label + "RUN-TIME ERROR:\n") sys.stderr.write(str(sys.exc_info()[1]) + "\n") raise except: sys.stderr.write(error_label + "Encountered unhandled exception:\n") if len(sys.exc_info()) > 1: sys.stderr.write(str(sys.exc_info()[1]) + "\n") else: traceback.print_exc(file=sys.stderr) raise except: sys.stderr.write("\n") sys.stderr.write( "To obtain further information regarding the " "source of the exception, use the " "--traceback option\n") rc = 1 # # TODO: Once we incorporate options registration into # all of the PySP commands we will assume the # options object is always a PySPConfigBlock # if isinstance(options, PySPConfigBlock): options.check_usage(error=False) logger.setLevel(prev_log_level) return rc
def main(args=None): # # Top-level command that executes the extensive form writer. # This is segregated from run_ef_writer to enable profiling. # # # Import plugins # import pyomo.environ # # Parse command-line options. # try: options_parser = construct_smps_options_parser("runph [options]") (options, args) = options_parser.parse_args(args=args) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. return _exc.code # # Control the garbage collector - more critical than I would like # at the moment. # if options.disable_gc: gc.disable() else: gc.enable() # # Run PH - precise invocation depends on whether we want profiling # output. # # if an exception is triggered and traceback is enabled, 'ans' # won't have a value and the return statement from this function # will flag an error, masking the stack trace that you really want # to see. ans = None if pstats_available and options.profile > 0: # # Call the main routine with profiling. # tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('exec_pysp2smps(options)',globals(),locals(),tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('cumulative','calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) TempfileManager.clear_tempfiles() ans = [tmp, None] else: # # Call the main routine without profiling. # if options.traceback: ans = exec_pysp2smps(options) else: try: try: ans = exec_pysp2smps(options) except ValueError: str = sys.exc_info()[1] print("VALUE ERROR:") print(str) raise except KeyError: str = sys.exc_info()[1] print("KEY ERROR:") print(str) raise except TypeError: str = sys.exc_info()[1] print("TYPE ERROR:") print(str) raise except NameError: str = sys.exc_info()[1] print("NAME ERROR:") print(str) raise except IOError: str = sys.exc_info()[1] print("IO ERROR:") print(str) raise except pyutilib.common.ApplicationError: str = sys.exc_info()[1] print("APPLICATION ERROR:") print(str) raise except RuntimeError: str = sys.exc_info()[1] print("RUN-TIME ERROR:") print(str) raise except: print("Encountered unhandled exception") traceback.print_exc() raise except: print("\n") print("To obtain further information regarding the " "source of the exception, use the --traceback option") gc.enable() return ans
def main(args=None): # # Top-level command that executes the extensive form writer. # This is segregated from run_ef_writer to enable profiling. # # # Import plugins # import pyomo.environ # # Parse command-line options. # try: options_parser = construct_smps_options_parser("runph [options]") (options, args) = options_parser.parse_args(args=args) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified - catch # it to exit gracefully. return _exc.code # # Control the garbage collector - more critical than I would like # at the moment. # if options.disable_gc: gc.disable() else: gc.enable() # # Run PH - precise invocation depends on whether we want profiling # output. # # if an exception is triggered and traceback is enabled, 'ans' # won't have a value and the return statement from this function # will flag an error, masking the stack trace that you really want # to see. ans = None if pstats_available and options.profile > 0: # # Call the main routine with profiling. # tfile = TempfileManager.create_tempfile(suffix=".profile") tmp = profile.runctx('exec_pysp2smps(options)', globals(), locals(), tfile) p = pstats.Stats(tfile).strip_dirs() p.sort_stats('time', 'cumulative') p = p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('cumulative', 'calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) p = p.sort_stats('calls') p.print_stats(options.profile) p.print_callers(options.profile) p.print_callees(options.profile) TempfileManager.clear_tempfiles() ans = [tmp, None] else: # # Call the main routine without profiling. # if options.traceback: ans = exec_pysp2smps(options) else: try: try: ans = exec_pysp2smps(options) except ValueError: str = sys.exc_info()[1] print("VALUE ERROR:") print(str) raise except KeyError: str = sys.exc_info()[1] print("KEY ERROR:") print(str) raise except TypeError: str = sys.exc_info()[1] print("TYPE ERROR:") print(str) raise except NameError: str = sys.exc_info()[1] print("NAME ERROR:") print(str) raise except IOError: str = sys.exc_info()[1] print("IO ERROR:") print(str) raise except pyutilib.common.ApplicationError: str = sys.exc_info()[1] print("APPLICATION ERROR:") print(str) raise except RuntimeError: str = sys.exc_info()[1] print("RUN-TIME ERROR:") print(str) raise except: print("Encountered unhandled exception") traceback.print_exc() raise except: print("\n") print("To obtain further information regarding the " "source of the exception, use the --traceback option") gc.enable() return ans
def __init__(self, pyomo_model): """ Pyomo nonlinear program interface Parameters ---------- pyomo_model: pyomo.environ.ConcreteModel Pyomo concrete model """ TempfileManager.push() try: # get the temp file names for the nl file nl_file = TempfileManager.create_tempfile(suffix='pynumero.nl') # The current AmplInterface code only supports a single # objective function Therefore, we throw an error if there # is not one (and only one) active objective function. This # is better than adding a dummy objective that the user does # not know about (since we do not have a good place to # remove this objective later) # # TODO: extend the AmplInterface and the AslNLP to correctly # handle this # # This currently addresses issue #1217 objectives = list( pyomo_model.component_data_objects(ctype=pyo.Objective, active=True, descend_into=True)) if len(objectives) != 1: raise NotImplementedError( 'The ASL interface and PyomoNLP in PyNumero currently ' 'only support single objective problems. Deactivate ' 'any extra objectives you may have, or add a dummy ' 'objective (f(x)=0) if you have a square problem.') self._objective = objectives[0] # write the nl file for the Pyomo model and get the symbolMap fname, symbolMap = WriterFactory('nl')(pyomo_model, nl_file, lambda x: True, {}) # create component maps from vardata to idx and condata to idx self._vardata_to_idx = vdidx = ComponentMap() self._condata_to_idx = cdidx = ComponentMap() # TODO: Are these names totally consistent? for name, obj in six.iteritems(symbolMap.bySymbol): if name[0] == 'v': vdidx[obj()] = int(name[1:]) elif name[0] == 'c': cdidx[obj()] = int(name[1:]) # The NL writer advertises the external function libraries # through the PYOMO_AMPLFUNC environment variable; merge it # with any preexisting AMPLFUNC definitions amplfunc = "\n".join(val for val in ( os.environ.get('AMPLFUNC', ''), os.environ.get('PYOMO_AMPLFUNC', ''), ) if val) with CtypesEnviron(AMPLFUNC=amplfunc): super(PyomoNLP, self).__init__(nl_file) # keep pyomo model in cache self._pyomo_model = pyomo_model finally: # delete the nl file TempfileManager.pop()