def test_clear(self): """Test clear logic""" TempfileManager.push() OUTPUT = open(tempdir + 'pushpop2', 'w') OUTPUT.write('tempfile\n') OUTPUT.close() os.mkdir(tempdir + 'pushpopdir2') TempfileManager.add_tempfile(tempdir + 'pushpop2') TempfileManager.add_tempfile(tempdir + 'pushpopdir2') TempfileManager.push() OUTPUT = open(tempdir + 'pushpop2a', 'w') OUTPUT.write('tempfile\n') OUTPUT.close() os.mkdir(tempdir + 'pushpopdir2a') TempfileManager.add_tempfile(tempdir + 'pushpop2a') TempfileManager.add_tempfile(tempdir + 'pushpopdir2a') TempfileManager.clear_tempfiles() if os.path.exists(tempdir + 'pushpop2a'): self.fail("clear_tempfiles() failed to clean out files") if os.path.exists(tempdir + 'pushpopdir2a'): self.fail("clear_tempfiles() failed to clean out directories") if os.path.exists(tempdir + 'pushpop2'): self.fail("clear_tempfiles() failed to clean out files") if os.path.exists(tempdir + 'pushpopdir2'): self.fail("clear_tempfiles() failed to clean out directories")
def test_pushpop2_dir(self): """Test pushpop logic with directories""" TempfileManager.push() os.mkdir(tempdir + 'pushpop2') TempfileManager.add_tempfile(tempdir + 'pushpop2') TempfileManager.push() os.mkdir(tempdir + 'pushpop2a') TempfileManager.add_tempfile(tempdir + 'pushpop2a') TempfileManager.pop() if not os.path.exists(tempdir + 'pushpop2'): self.fail("pop() clean out all files") if os.path.exists(tempdir + 'pushpop2a'): self.fail("pop() failed to clean out files") TempfileManager.pop() if os.path.exists(tempdir + 'pushpop2'): self.fail("pop() failed to clean out files")
def _postsolve(self): stub = os.path.splitext(self._soln_file)[0] # There is a type file created by the solver to give the variable types # this is needed to read the trajectory data, and we want to include it # with other tmp files typ_file = stub + ".typ" TempfileManager.add_tempfile(typ_file) # If the vars_stub option was specified, copy the col and typ files to # the working directory. These files are needed to get the names and # types of variables and to make sense of trajectory data. if self._ts_vars_stub is not None: try: shutil.copyfile(f"{stub}.col", f"{self._ts_vars_stub}.col") except: pass try: shutil.copyfile(f"{stub}.typ", f"{self._ts_vars_stub}.typ") except: pass return ASL._postsolve(self)
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 solve(self, model, timer: HierarchicalTimer = None): StaleFlagManager.mark_all_as_stale() avail = self.available() if not avail: raise PyomoException(f'Solver {self.__class__} is not available ({avail}).') if self._last_results_object is not None: self._last_results_object.solution_loader.invalidate() 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) self._last_results_object = res 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 test_pushpop2(self): """Test pushpop logic""" TempfileManager.push() OUTPUT = open(tempdir + 'pushpop2', 'w') OUTPUT.write('tempfile\n') OUTPUT.close() TempfileManager.add_tempfile(tempdir + 'pushpop2') TempfileManager.push() OUTPUT = open(tempdir + 'pushpop2a', 'w') OUTPUT.write('tempfile\n') OUTPUT.close() TempfileManager.add_tempfile(tempdir + 'pushpop2a') TempfileManager.pop() if not os.path.exists(tempdir + 'pushpop2'): self.fail("pop() clean out all files") if os.path.exists(tempdir + 'pushpop2a'): self.fail("pop() failed to clean out files") TempfileManager.pop() if os.path.exists(tempdir + 'pushpop2'): self.fail("pop() failed to clean out files")
def nlwriter_asl_test(self, name): testFile = TempfileManager.create_tempfile(suffix=name + '.test.nl') testFile_row = testFile[:-2] + 'row' TempfileManager.add_tempfile(testFile_row, exists=False) testFile_col = testFile[:-2] + 'col' TempfileManager.add_tempfile(testFile_col, exists=False) cmd = [ '--output=' + testFile, '--file-determinism=3', '--symbolic-solver-labels', join(currdir, name + '_testCase.py') ] if os.path.exists(join(currdir, name + '.dat')): cmd.append(join(currdir, name + '.dat')) self.pyomo(cmd) # # compare AMPL and Pyomo nl file structure # testFile_json = testFile[:-2] + 'json' TempfileManager.add_tempfile(testFile_json, exists=False) # obtain the nl file summary information for comparison with ampl p = subprocess.run([ 'gjh_asl_json', testFile, 'rows=' + testFile_row, 'cols=' + testFile_col, 'json=' + testFile_json ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) self.assertTrue(p.returncode == 0, msg=p.stdout) baseFile = join(currdir, name + '.ampl.nl') amplFile = TempfileManager.create_tempfile(suffix=name + '.ampl.json') # obtain the nl file summary information for comparison with ampl p = subprocess.run([ 'gjh_asl_json', baseFile, 'rows=' + baseFile[:-2] + 'row', 'cols=' + baseFile[:-2] + 'col', 'json=' + amplFile ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) self.assertTrue(p.returncode == 0, msg=p.stdout) with open(testFile_json, 'r') as f1, open(amplFile, 'r') as f2: self.assertStructuredAlmostEqual(json.load(f1), json.load(f2), abstol=1e-8)
def test12b_output_option(self): # Run pyomo with --output option (configfile) log = join(currdir, 'test12b.log') TempfileManager.add_tempfile(log, exists=False) self.pyomo(join(currdir, 'test12b.json'), root=join(currdir, 'test12')) self.compare_json(join(currdir, "test12.jsn"), join(currdir, "test12.txt"))
def test12_output_option(self): #"""Run pyomo with --output option""" log = join(currdir, 'test12.log') TempfileManager.add_tempfile(log, exists=False) self.pyomo('--logfile=%s pmedian.py pmedian.dat' % (log,), root=join(currdir, 'test12')) self.compare_json(join(currdir, "test12.jsn"), join(currdir, "test12.txt"))
def apply(self, *args, **kwds): """ Generate a NL or LP file from Pyomo, and then do subsequent conversions. """ import pyomo.scripting.convert capabilities = kwds.pop("capabilities", None) # all non-consumed keywords are assumed to be options # that should be passed to the writer. io_options = {} for kwd, value in iteritems(kwds): io_options[kwd] = value kwds.clear() # basestring is gone in Python 3.x, merged with str. if PY3: compare_type = str else: compare_type = basestring if isinstance(args[2], compare_type): instance = None else: instance = args[2] if args[1] == ProblemFormat.cpxlp: problem_filename = TempfileManager.\ create_tempfile(suffix = '.pyomo.lp') if instance is not None: if isinstance(instance, IBlock): symbol_map_id = instance.write( problem_filename, format=ProblemFormat.cpxlp, _solver_capability=capabilities, _called_by_solver=True, **io_options) else: (problem_filename, symbol_map_id) = \ instance.write( filename=problem_filename, format=ProblemFormat.cpxlp, solver_capability=capabilities, io_options=io_options) return (problem_filename, ), symbol_map_id else: # # I'm simply exposing a fatal issue with # this code path. How would we convert the # collected keywords into command-line # arguments that can be sent to the writer? # if len(io_options): raise ValueError( "The following io_options will be ignored " "(please create a bug report):\n\t" + "\n\t".join("%s = %s" % (k, v) for k, v in iteritems(io_options))) ans = pyomo.scripting.convert.\ pyomo2lp(['--output',problem_filename,args[2]]) if ans.errorcode: raise RuntimeError("pyomo2lp conversion " "returned nonzero error code " "(%s)" % ans.errorcode) model = ans.retval problem_filename = model.filename symbol_map = model.symbol_map return (problem_filename, ), symbol_map elif args[1] == ProblemFormat.bar: problem_filename = TempfileManager.\ create_tempfile(suffix = '.pyomo.bar') if instance is not None: if isinstance(instance, IBlock): symbol_map_id = instance.write( problem_filename, format=ProblemFormat.bar, _solver_capability=capabilities, _called_by_solver=True, **io_options) else: (problem_filename, symbol_map_id) = \ instance.write( filename=problem_filename, format=ProblemFormat.bar, solver_capability=capabilities, io_options=io_options) return (problem_filename, ), symbol_map_id else: # # I'm simply exposing a fatal issue with # this code path. How would we convert the # collected keywords into command-line # arguments that can be sent to the writer? # if len(io_options): raise ValueError( "The following io_options will be ignored " "(please create a bug report):\n\t" + "\n\t".join("%s = %s" % (k, v) for k, v in iteritems(io_options))) ans = pyomo.scripting.convert.\ pyomo2bar(['--output',problem_filename,args[2]]) if ans.errorcode: raise RuntimeError("pyomo2bar conversion " "returned nonzero error code " "(%s)" % ans.errorcode) model = ans.retval problem_filename = model.filename symbol_map = model.symbol_map return (problem_filename, ), symbol_map elif args[1] in [ProblemFormat.mps, ProblemFormat.nl]: if args[1] == ProblemFormat.nl: problem_filename = TempfileManager.\ create_tempfile(suffix = '.pyomo.nl') if io_options.get("symbolic_solver_labels", False): TempfileManager.add_tempfile(problem_filename[:-3] + ".row", exists=False) TempfileManager.add_tempfile(problem_filename[:-3] + ".col", exists=False) else: assert args[1] == ProblemFormat.mps problem_filename = TempfileManager.\ create_tempfile(suffix = '.pyomo.mps') if instance is not None: if isinstance(instance, IBlock): symbol_map_id = instance.write( problem_filename, format=args[1], _solver_capability=capabilities, _called_by_solver=True, **io_options) else: (problem_filename, symbol_map_id) = \ instance.write( filename=problem_filename, format=args[1], solver_capability=capabilities, io_options=io_options) return (problem_filename, ), symbol_map_id else: # # I'm simply exposing a fatal issue with # this code path. How would we convert the # collected keywords into command-line # arguments that can be sent to the writer? # if len(io_options): raise ValueError( "The following io_options will be ignored " "(please create a bug report):\n\t" + "\n\t".join("%s = %s" % (k, v) for k, v in iteritems(io_options))) ans = pyomo.scripting.convert.\ pyomo2nl(['--output',problem_filename,args[2]]) if ans.errorcode: raise RuntimeError("pyomo2nl conversion " "returned nonzero error " "code (%s)" % ans.errorcode) model = ans.retval problem_filename = model.filename symbol_map = model.symbol_map if args[1] == ProblemFormat.nl: return (problem_filename, ), symbol_map # # Convert from NL to MPS # # TBD: We don't support a variable map file when going # from NL to MPS within the PICO converter. # NOTE: this is a problem with the MPS writer that is # provided by COIN-OR # NOTE: we should generalize this so it doesn't strictly # depend on the PICO converter utility. # ans = self.pico_converter.apply(ProblemFormat.nl, ProblemFormat.mps, problem_filename) os.remove(problem_filename) return ans elif args[1] == ProblemFormat.osil: if False: problem_filename = TempfileManager.\ create_tempfile(suffix='pyomo.osil') if instance: if isinstance(instance, IBlock): symbol_map_id = instance.write( problem_filename, format=ProblemFormat.osil, _solver_capability=capabilities, _called_by_solver=True, **io_options) else: (problem_filename, symbol_map_id) = \ instance.write( filename=problem_filename, format=ProblemFormat.osil, solver_capability=capabilities, io_options=io_options) return (problem_filename, ), None else: raise NotImplementedError("There is currently no " "script conversion available from " "Pyomo to OSiL format.")
def test_add3_dir(self): """Test explicit adding of a directory that already exists""" os.mkdir(tempdir + 'add3') TempfileManager.add_tempfile(tempdir + 'add3')
def test_add3(self): """Test explicit adding of a file that already exists""" OUTPUT = open(tempdir + 'add3', 'w') OUTPUT.write('tempfile\n') OUTPUT.close() TempfileManager.add_tempfile(tempdir + 'add3')
def test_add2_dir(self): """Test explicit adding of a directory that is missing""" TempfileManager.add_tempfile(tempdir + 'add2', False)
def _add_tempfile(self, label, filename): TempfileManager.add_tempfile(filename, exists=False) self._files[label] = filename
def _presolve(self, *args, **kwds): super()._presolve(*args, **kwds) self._gjh_file = self._soln_file[:-3] + 'gjh' TempfileManager.add_tempfile(self._gjh_file, exists=False)