コード例 #1
0
    def apply(self, *args, **kwargs):
        """
        Run the external pico_convert utility
        """
        if len(args) != 3:
            raise ConverterError("Cannot apply pico_convert with more than one filename or model")
        _exe = pyomo.common.Executable("pico_convert")
        if not _exe:
            raise ConverterError("The 'pico_convert' application cannot be found")

        pico_convert_cmd = _exe.path()
        target=str(args[1])
        if target=="cpxlp":
            target="lp"
        # NOTE: if you have an extra "." in the suffix, the pico_convert program fails to output to the correct filename.
        output_filename = TempfileManager.create_tempfile(suffix = 'pico_convert.' + target)
        if not isinstance(args[2], str):
            fname= TempfileManager.create_tempfile(suffix= 'pico_convert.' +str(args[0]))
            args[2].write(filename=fname, format=args[1])
            cmd = pico_convert_cmd +" --output="+output_filename+" "+target+" "+fname
        else:
            cmd = pico_convert_cmd +" --output="+output_filename+" "+target
            for item in args[2:]:
                if not os.path.exists(item):
                    raise ConverterError("File "+item+" does not exist!")
                cmd = cmd + " "+item
        print("Running command: "+cmd)
        subprocess.run(cmd, stdout=subprocess.DEVNULL,
                       stderr=subprocess.DEVNULL)
        if not os.path.exists(output_filename):       #pragma:nocover
            raise ApplicationError(\
                    "Problem launching 'pico_convert' to create "+output_filename)
        return (output_filename,),None # no variable map at the moment
コード例 #2
0
ファイル: test_pyomo.py プロジェクト: jialuw96/pyomo
 def pyomo(self, cmd, **kwds):
     if 'root' in kwds:
         OUTPUT = kwds['root'] + '.out'
         results = kwds['root'] + '.jsn'
         TempfileManager.add_tempfile(OUTPUT, exists=False)
         TempfileManager.add_tempfile(results, exists=False)
     else:
         OUTPUT = StringIO()
         results = 'results.jsn'
         TempfileManager.create_tempfile(suffix='results.jsn')
     with capture_output(OUTPUT):
         try:
             _dir = os.getcwd()
             os.chdir(currdir)
             args = ['solve', '--solver=glpk', '--results-format=json',
                     '--save-results=%s' % results]
             if type(cmd) is list:
                 args.extend(cmd)
             elif cmd.endswith('json') or cmd.endswith('yaml'):
                 args.append(cmd)
             else:
                 args.extend(re.split('[ ]+', cmd))
             output = main.main(args)
         finally:
             os.chdir(_dir)
     if not 'root' in kwds:
         return OUTPUT.getvalue()
     return output
コード例 #3
0
    def test_open_tempfile_windows(self):
        TempfileManager.push()
        fname = TempfileManager.create_tempfile()
        f = open(fname)
        try:
            _orig = tempfiles.deletion_errors_are_fatal
            tempfiles.deletion_errors_are_fatal = True
            with self.assertRaisesRegex(WindowsError,
                                        ".*process cannot access the file"):
                TempfileManager.pop()
        finally:
            tempfiles.deletion_errors_are_fatal = _orig
            f.close()
            os.remove(fname)

        TempfileManager.push()
        fname = TempfileManager.create_tempfile()
        f = open(fname)
        log = StringIO()
        try:
            _orig = tempfiles.deletion_errors_are_fatal
            tempfiles.deletion_errors_are_fatal = False
            with LoggingIntercept(log, 'pyomo.common'):
                TempfileManager.pop()
            self.assertIn("Unable to delete temporary file", log.getvalue())
        finally:
            tempfiles.deletion_errors_are_fatal = _orig
            f.close()
            os.remove(fname)
コード例 #4
0
    def create_command_line(self, executable, problem_files):
        #
        # Not the best place to catch this, but we want to make sure
        # that we have done the version check somewhere
        #
        if executable not in self._known_versions:
            self._known_versions[executable] = self._get_version(executable)
        _ver = self._known_versions[executable]
        if not _ver or _ver < (4, 58):
            raise RuntimeError(
                "Pyomo only supports versions of GLPK since 4.58; "
                "found version %s.  Please upgrade your installation "
                "of GLPK" % ('.'.join(map(str, _ver)), ))

        #
        # 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, str) 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 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)
コード例 #5
0
 def test_ipopt_solve_from_nl(self):
     # Test ipopt solve from nl file
     _log = TempfileManager.create_tempfile(".test_ipopt.log")
     results = self.ipopt.solve(join(currdir, "sisser.pyomo.nl"),
                                logfile=_log,
                                suffixes=['.*'])
     # We don't want the test to care about which Ipopt version we are using
     results.Solution(0).Message = "Ipopt"
     results.Solver.Message = "Ipopt"
     _out = TempfileManager.create_tempfile(".test_ipopt.txt")
     results.write(filename=_out, times=False, format='json')
     self.compare_json(_out, join(currdir, "test_solve_from_nl.baseline"))
コード例 #6
0
    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)
コード例 #7
0
    def test_solve4(self):
        """ Test ASL - test4.nl """
        _log = TempfileManager.create_tempfile(".test_solve4.log")
        _out = TempfileManager.create_tempfile(".test_solve4.txt")

        results = self.asl.solve(join(currdir, "test4.nl"),
                                 logfile=_log,
                                 suffixes=['.*'])
        results.write(filename=_out, times=False, format='json')
        _baseline = join(currdir, "test4_asl.txt")
        with open(_out, 'r') as out, open(_baseline, 'r') as txt:
            self.assertStructuredAlmostEqual(json.load(txt), json.load(out),
                                             abstol=1e-4,
                                             allow_second_superset=True)
コード例 #8
0
    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)
コード例 #9
0
    def test_deprecated_tempdir(self):
        TempfileManager.push()
        try:
            tmpdir = TempfileManager.create_tempdir()
            _orig = pyutilib_mngr.tempdir
            pyutilib_mngr.tempdir = tmpdir
            TempfileManager.tempdir = None

            log = StringIO()
            with LoggingIntercept(log, 'pyomo'):
                fname = TempfileManager.create_tempfile()
            self.assertIn(
                "The use of the PyUtilib TempfileManager.tempdir "
                "to specify the default location for Pyomo "
                "temporary files",
                log.getvalue().replace("\n", " "))

            log = StringIO()
            with LoggingIntercept(log, 'pyomo'):
                dname = TempfileManager.create_tempdir()
            self.assertIn(
                "The use of the PyUtilib TempfileManager.tempdir "
                "to specify the default location for Pyomo "
                "temporary directories",
                log.getvalue().replace("\n", " "))
        finally:
            TempfileManager.pop()
            pyutilib_mngr.tempdir = _orig
コード例 #10
0
 def test3_write_lp(self):
     """ Convert from AMPL to LP """
     self.model = pyomo.opt.AmplModel(join(currdir, 'test3.mod'))
     _test = TempfileManager.create_tempfile(suffix='test3.lp')
     try:
         self.model.write(_test)
     except ApplicationError:
         err = sys.exc_info()[1]
         if pyomo.common.Executable("glpsol"):
             self.fail("Unexpected ApplicationError - glpsol is enabled "
                       "but not available: '%s'" % str(err))
         return
     except pyomo.opt.ConverterError:
         err = sys.exc_info()[1]
         if pyomo.common.Executable("glpsol"):
             self.fail("Unexpected ConverterError - glpsol is enabled "
                       "but not available: '%s'" % str(err))
         return
     _base = join(currdir, 'test3.baseline.lp')
     with open(_test, 'r') as run, open(_base, 'r') as baseline:
         for line1, line2 in zip_longest(run, baseline):
             for _pattern in ('Problem:', ):
                 if line1.find(_pattern) >= 0:
                     line1 = line1[:line1.find(_pattern) + len(_pattern)]
                     line2 = line2[:line2.find(_pattern) + len(_pattern)]
             self.assertEqual(line1,
                              line2,
                              msg="Files %s and %s differ" % (_test, _base))
コード例 #11
0
ファイル: cbc.py プロジェクト: adowling2/pyomo
 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)
コード例 #12
0
    def test_scip_solve_from_instance_options(self):

        # Creating a dummy scip.set file in the cwd
        # will cover the code that prints a warning
        _cwd = os.getcwd()
        tmpdir = TempfileManager.create_tempdir()
        try:
            os.chdir(tmpdir)
            open(join(tmpdir, 'scip.set'), "w").close()
            # Test scip solve from a pyomo instance and load the solution
            with LoggingIntercept() as LOG:
                results = self.scip.solve(self.model,
                                          suffixes=['.*'],
                                          options={"limits/softtime": 100})
            self.assertRegex(
                LOG.getvalue().replace("\n", " "),
                r"A file named (.*) exists in the current working "
                r"directory, but SCIP options are being "
                r"set using a separate options file. The "
                r"options file \1 will be ignored.")
        finally:
            os.chdir(_cwd)
        # We don't want the test to care about which Scip version we are using
        self.model.solutions.store_to(results)
        results.Solution(0).Message = "Scip"
        results.Solver.Message = "Scip"
        results.Solver.Time = 0
        _out = TempfileManager.create_tempfile(".txt")
        results.write(filename=_out, times=False, format='json')
        self.compare_json(
            _out, join(currdir, "test_scip_solve_from_instance.baseline"))
コード例 #13
0
ファイル: cplex.py プロジェクト: jsiirola/pyomo
 def solve(self, model, timer: HierarchicalTimer = None):
     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 + '.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
コード例 #14
0
 def _write_and_check_header(self, m, correct_lines):
     writer = appsi.writers.NLWriter()
     with TempfileManager:
         fname = TempfileManager.create_tempfile(suffix='.appsi.nl')
         writer.write(m, fname)
         with open(fname, 'r') as f:
             for ndx, line in enumerate(list(f.readlines())[:10]):
                 self.assertTrue(line.startswith(correct_lines[ndx]))
コード例 #15
0
    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 = kwds.pop('warmstart_file', None)
        user_warmstart = False
        if self._warm_start_file_name is not None:
            user_warmstart = True

        # 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], str):
            # we assume the user knows what they are doing...
            pass
        elif self._warm_start_solve and \
             (not isinstance(args[0], str)):
            # 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='.gurobi.mst')

        # 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], str)):

            if len(args) != 1:
                raise ValueError(
                    "GUROBI _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 is True:
                    print("Warm start write time=%.2f seconds" %
                          (end_time - start_time))
コード例 #16
0
def _run_ipopt_with_stats(model, solver, max_iter=500, max_cpu_time=120):
    """
    Run the solver (must be ipopt) and return the convergence statistics

    Parameters
    ----------
    model : Pyomo model
       The pyomo model to be solved

    solver : Pyomo solver
       The pyomo solver to use - it must be ipopt, but with whichever options
       are preferred

    max_iter : int
       The maximum number of iterations to allow for ipopt

    max_cpu_time : int
       The maximum cpu time to allow for ipopt (in seconds)

    Returns
    -------
       Returns a tuple with (solve status object, bool (solve successful or
       not), number of iters, solve time)
    """
    # ToDo: Check that the "solver" is, in fact, IPOPT

    TempfileManager.push()
    tempfile = TempfileManager.create_tempfile(suffix='ipopt_out', text=True)
    opts = {
        'output_file': tempfile,
        'max_iter': max_iter,
        'max_cpu_time': max_cpu_time
    }

    status_obj = solver.solve(model, options=opts, tee=True)
    solved = True
    if status_obj.solver.termination_condition != TerminationCondition.optimal:
        solved = False

    iters = 0
    time = 0
    # parse the output file to get the iteration count, solver times, etc.
    with open(tempfile, 'r') as f:
        for line in f:
            if line.startswith('Number of Iterations....:'):
                tokens = line.split()
                iters = int(tokens[3])
            elif line.startswith(
                    'Total CPU secs in IPOPT (w/o function evaluations)   ='):
                tokens = line.split()
                time += float(tokens[9])
            elif line.startswith(
                    'Total CPU secs in NLP function evaluations           ='):
                tokens = line.split()
                time += float(tokens[8])

    TempfileManager.pop(remove=True)
    return status_obj, solved, iters, time
コード例 #17
0
    def apply(self, *args, **kwargs):
        """Convert an instance of one type into another"""
        if not isinstance(args[2], str):
            raise ConverterError("Can only apply ampl to convert file data")
        _exec = pyomo.common.Executable("ampl")
        if not _exec:
            raise ConverterError("The 'ampl' executable cannot be found")
        script_filename = TempfileManager.create_tempfile(suffix='.ampl')

        if args[1] == ProblemFormat.nl:
            output_filename = TempfileManager.create_tempfile(suffix='.nl')
        else:
            output_filename = TempfileManager.create_tempfile(suffix='.mps')

        cmd = [_exec.path(), script_filename]
        #
        # Create the AMPL script
        #
        OUTPUT = open(script_filename, 'w')
        OUTPUT.write("#\n")
        OUTPUT.write("# AMPL script for converting the following files\n")
        OUTPUT.write("#\n")
        if len(args[2:]) == 1:
            OUTPUT.write('model ' + args[2] + ";\n")
        else:
            OUTPUT.write('model ' + args[2] + ";\n")
            OUTPUT.write('data ' + args[3] + ";\n")
        abs_ofile = os.path.abspath(output_filename)
        if args[1] == ProblemFormat.nl:
            OUTPUT.write('write g' + abs_ofile[:-3] + ";\n")
        else:
            OUTPUT.write('write m' + abs_ofile[:-4] + ";\n")
        OUTPUT.close()
        #
        # Execute command and cleanup
        #
        output = subprocess.run(cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT,
                                universal_newlines=True)
        if not os.path.exists(output_filename):  #pragma:nocover
            raise ApplicationError(
                "Problem launching 'ampl' to create '%s': %s" %
                (output_filename, output.stdout))
        return (output_filename, ), None  # empty variable map
コード例 #18
0
 def test_create3(self):
     """Test create logic - no options"""
     fname = TempfileManager.create_tempfile(suffix='bar')
     OUTPUT = open(fname, 'w')
     OUTPUT.write('tempfile\n')
     OUTPUT.close()
     self.assertEqual(len(list(glob.glob(tempdir + '*'))), 1)
     fname = os.path.basename(fname)
     self.assertTrue(fname.endswith('bar'))
コード例 #19
0
 def gams_writer_test_invalid(self, name, targetdir):
     with self.assertRaisesRegex(
             RuntimeError, "GAMS files cannot represent the unary function"):
         testFile = TempfileManager.create_tempfile(
             suffix=name + '.test.gms')
         cmd = ['--output=' + testFile,
                join(targetdir, name + '_testCase.py')]
         if os.path.exists(join(targetdir, name + '.dat')):
             cmd.append(join(targetdir, name + '.dat'))
         self.pyomo(cmd)
コード例 #20
0
ファイル: GLPK_old.py プロジェクト: Utkarsh-Detha/pyomo
    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._soln_file = TempfileManager.create_tempfile(suffix='.glpk.soln')

        #
        # Define command line
        #
        cmd = [executable]
        if self._timer:
            cmd.insert(0, self._timer)
        for key in self.options:
            opt = self.options[key]
            if (opt.__class__ is str) 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 self._timelimit is not None and self._timelimit > 0.0:
            cmd.extend(['--tmlim', str(self._timelimit)])

        cmd.extend(['--output', self._soln_file])

        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)
コード例 #21
0
 def test_ipopt_solve_from_instance(self):
     # Test ipopt solve from a pyomo instance and load the solution
     results = self.ipopt.solve(self.sisser_instance, suffixes=['.*'])
     # We don't want the test to care about which Ipopt version we are using
     self.sisser_instance.solutions.store_to(results)
     results.Solution(0).Message = "Ipopt"
     results.Solver.Message = "Ipopt"
     _out = TempfileManager.create_tempfile(".test_ipopt.txt")
     results.write(filename=_out, times=False, format='json')
     self.compare_json(_out,
                       join(currdir, "test_solve_from_instance.baseline"))
コード例 #22
0
ファイル: test_tee.py プロジェクト: Ishanki/pyomo
 def test_capture_output_logfile_string(self):
     logfile = TempfileManager.create_tempfile()
     self.assertTrue(isinstance(logfile, str))
     try: 
         with tee.capture_output(logfile):
             print('HELLO WORLD')
         with open(logfile, 'r') as f:
             result = f.read()
         self.assertEqual('HELLO WORLD\n', result)
     finally:
         TempfileManager.clear_tempfiles()
コード例 #23
0
 def test_create4(self):
     """Test create logic - no options"""
     TempfileManager.sequential_files(2)
     fname = TempfileManager.create_tempfile()
     OUTPUT = open(fname, 'w')
     OUTPUT.write('tempfile\n')
     OUTPUT.close()
     self.assertEqual(len(list(glob.glob(tempdir + '*'))), 1)
     fname = os.path.basename(fname)
     self.assertEqual(fname, 'tmp2')
     #
     TempfileManager.unique_files()
     fname = TempfileManager.create_tempfile()
     OUTPUT = open(fname, 'w')
     OUTPUT.write('tempfile\n')
     OUTPUT.close()
     self.assertEqual(len(list(glob.glob(tempdir + '*'))), 2)
     fname = os.path.basename(fname)
     self.assertNotEqual(fname, 'tmp3')
     self.assertTrue(fname.startswith('tmp'))
コード例 #24
0
 def test_scip_solve_from_instance(self):
     # Test scip solve from a pyomo instance and load the solution
     results = self.scip.solve(self.model, suffixes=['.*'])
     # We don't want the test to care about which Scip version we are using
     self.model.solutions.store_to(results)
     results.Solution(0).Message = "Scip"
     results.Solver.Message = "Scip"
     results.Solver.Time = 0
     _out = TempfileManager.create_tempfile(".txt")
     results.write(filename=_out, times=False, format='json')
     self.compare_json(
         _out, join(currdir, "test_scip_solve_from_instance.baseline"))
コード例 #25
0
 def test_factory(self):
     with ReaderFactory("sol") as reader:
         if reader is None:
             raise IOError("Reader 'sol' is not registered")
         soln = reader(join(currdir, "test4_sol.sol"), suffixes=["dual"])
         _test = TempfileManager.create_tempfile('factory.txt')
         soln.write(filename=_test, format='json')
         with open(_test, 'r') as out, \
             open(join(currdir, "test4_sol.jsn"), 'r') as txt:
             self.assertStructuredAlmostEqual(json.load(txt),
                                              json.load(out),
                                              allow_second_superset=True)
コード例 #26
0
ファイル: test_ampl.py プロジェクト: jialuw96/pyomo
 def test3_solve(self):
     if not 'glpk' in solvers:
         self.skipTest("glpk solver is not available")
     self.model = pyomo.opt.AmplModel(join(currdir, 'test3.mod'))
     opt = pyomo.opt.SolverFactory('glpk')
     _test = TempfileManager.create_tempfile(suffix='test3.out')
     results = opt.solve(self.model, keepfiles=False)
     results.write(filename=_test, format='json')
     with open(_test, 'r') as out, \
         open(join(currdir,"test3.baseline.out"), 'r') as txt:
         self.assertStructuredAlmostEqual(json.load(txt), json.load(out),
                                          abstol=1e-6,
                                          allow_second_superset=True)
コード例 #27
0
    def _presolve(self, **kwds):
        warmstart_flag = kwds.pop('warmstart', False)
        self._keepfiles = kwds.pop('keepfiles', False)
        self._save_results = kwds.pop('save_results', True)
        self._integer_only_warmstarts = kwds.pop('integer_only_warmstarts',
                                                 False)

        # create a context in the temporary file manager for
        # this plugin - is "pop"ed in the _postsolve method.
        TempfileManager.push()

        self.results = None

        model = self._pyomo_model

        # this implies we have a custom solution "parser",
        # preventing the OptSolver _presolve method from
        # creating one
        self._results_format = ResultsFormat.soln
        # use the base class _presolve to consume the
        # important keywords
        OptSolver._presolve(self, **kwds)

        # ***********************************************************
        # The following code is only needed for backwards compatability of load_solutions=False.
        # If we ever only want to support the load_vars, load_duals, etc. methods, then this can be deleted.
        if self._save_results:
            self._smap_id = id(self._symbol_map)
            if isinstance(self._pyomo_model, IBlock):
                # BIG HACK (see pyomo.core.kernel write function)
                if not hasattr(self._pyomo_model, "._symbol_maps"):
                    setattr(self._pyomo_model, "._symbol_maps", {})
                getattr(self._pyomo_model,
                        "._symbol_maps")[self._smap_id] = self._symbol_map
            else:
                self._pyomo_model.solutions.add_symbol_map(self._symbol_map)
        # ***********************************************************

        if warmstart_flag:
            if self.warm_start_capable():
                self._warm_start()
            else:
                raise ValueError(
                    '{0} solver plugin is not capable of warmstart.'.format(
                        type(self)))

        if self._log_file is None:
            self._log_file = TempfileManager.create_tempfile(suffix='.log')
コード例 #28
0
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))
コード例 #29
0
 def test_all_vars_fixed(self):
     m = pe.ConcreteModel()
     m.x = pe.Var()
     m.y = pe.Var()
     m.obj = pe.Objective(expr=m.x**2 + m.y**2)
     m.c1 = pe.Constraint(expr=m.y >= pe.exp(m.x))
     m.c2 = pe.Constraint(expr=m.y >= (m.x - 1)**2)
     m.x.fix(1)
     m.y.fix(2)
     writer = appsi.writers.NLWriter()
     with TempfileManager:
         fname = TempfileManager.create_tempfile(suffix='.appsi.nl')
         with self.assertRaisesRegex(
                 ValueError,
                 'there are not any unfixed variables in the problem'):
             writer.write(m, fname)
コード例 #30
0
    def gams_writer_baseline_test(self, name, targetdir):
        baseline = join(currdir, name + '.pyomo.gms')
        testFile = TempfileManager.create_tempfile(suffix=name + '.test.gms')
        cmd = ['--output=' + testFile, join(targetdir, name + '_testCase.py')]
        if os.path.exists(join(targetdir, name + '.dat')):
            cmd.append(join(targetdir, name + '.dat'))
        self.pyomo(cmd)

        # Check that the pyomo nl file matches its own baseline
        try:
            self.assertTrue(cmp(testFile, baseline))
        except:
            with open(testFile, 'r') as f1, open(baseline, 'r') as f2:
                f1_contents = list(filter(None, f1.read().split()))
                f2_contents = list(filter(None, f2.read().split()))
                self.assertEqual(f1_contents, f2_contents)