コード例 #1
0
    def solve(self, name, *args, **kwargs):
        """
        Optimize the model using the named solver.

        Parameters
        ----------
        name : str
            The solver name
        \*args 
            A variable list of arguments.
        \**kwargs
            A variable list of keyword arguments.
        
        Notes
        -----
        The arguments and keyword arguments are the same as supported by Pyomo
        solver objects.
        """
        solver = SolverFactory(name)

        if len(self.model.o) == 0:
            # No objectives defined, so add a dummy objective
            self.model.o.add(1)

        return solver.solve(self.model, *args, **kwargs)
コード例 #2
0
    def test_SolverFactory_executable_isexe_nopath(self):
        for name in _test_names:
            with SolverFactory(name, executable=isexe_nopath) as opt:
                self.assertTrue(isinstance(opt, UnknownSolver))

        # now add the exedir to the PATH
        rm_PATH = False
        orig_PATH = None
        if "PATH" in os.environ:
            orig_PATH = os.environ["PATH"]
        else:
            rm_PATH = True
            os.environ["PATH"] = ""
        os.environ["PATH"] = exedir + os.pathsep + os.environ["PATH"]
        try:
            for name in _test_names:
                with SolverFactory(name, executable=isexe_nopath) as opt:
                    if isinstance(opt, UnknownSolver):
                        continue
                    self.assertEqual(opt._user_executable, isexe_abspath)
                    self.assertEqual(opt.executable(), isexe_abspath)
        finally:
            if rm_PATH:
                del os.environ["PATH"]
            else:
                os.environ["PATH"] = orig_PATH
コード例 #3
0
 def test_executable_isexe_abspath_user(self):
     for name in _test_names:
         with SolverFactory(name, executable=isexe_abspath_user) as opt:
             if isinstance(opt, UnknownSolver):
                 continue
             self.assertEqual(opt._user_executable, isexe_abspath)
             self.assertEqual(opt.executable(), isexe_abspath)
コード例 #4
0
    def __new__(cls, *args, **kwds):
        mode = kwds.pop('solver_io', 'python')
        if mode is None:
            mode = 'python'

        if mode not in {'python', 'direct', 'persistent'}:
            logger.error(
                'Pyomo currently only supports a Python interface to XPRESS. '
                'Please use one of python, direct, or persistent for solver_io.'
            )
            return
        if mode in ['python', 'direct']:
            opt = SolverFactory('xpress_direct', **kwds)
        elif mode == 'persistent':
            opt = SolverFactory('xpress_persistent', **kwds)
        if opt is None:
            logger.error('Python API for XPRESS is not installed')
        return opt
コード例 #5
0
    def solve(self, name, *args, **kwargs):
        """
        Optimize the model using the named solver.

        Parameters
        ----------
        name : str
            The solver name
        \*args 
            A variable list of arguments.
        \**kwargs
            A variable list of keyword arguments.
        
        Notes
        -----
        The arguments and keyword arguments are the same as supported by Pyomo
        solver objects.
        """
        solver = SolverFactory(name)
        return solver.solve(self.model, *args, **kwargs)
コード例 #6
0
 def test_executable_isexe_abspath_user(self):
     for name in _test_names:
         with SolverFactory(name, executable=isexe_abspath_user) as opt:
             if isinstance(opt, UnknownSolver):
                 continue
             # Note the user path could be different from our
             # computed path due to symlinks.  Check that the
             # executable is the same file.
             self.assertTrue(
                 os.path.samefile(opt._user_executable, isexe_abspath))
             self.assertTrue(
                 os.path.samefile(opt.executable(), isexe_abspath))
コード例 #7
0
    def __new__(cls, *args, **kwds):
        configure_cbc()
        try:
            mode = kwds['solver_io']
            if mode is None:
                mode = 'lp'
            del kwds['solver_io']
        except KeyError:
            mode = 'lp'
        #
        if mode == 'lp':
            opt = SolverFactory('_cbc_shell', **kwds)
            opt.set_problem_format(ProblemFormat.cpxlp)
            return opt
        # CBC's MPS parser seems too buggy to expose
        # this option


#        if mode == 'mps':
#            # *NOTE: CBC uses the COIN-OR MPS reader,
#            #        which ignores any objective sense
#            #        declared in the OBJSENSE section.
#            opt = SolverFactory('_cbc_shell', **kwds)
#            opt.set_problem_format(ProblemFormat.mps)
#            return opt
#
        if mode == 'nl':
            # the _cbc_compiled_with_asl and
            # _cbc_old_version flags are tristate
            # (None, False, True), so don't
            # simplify the if statements from
            # checking "is"
            if _cbc_compiled_with_asl is not False:
                if _cbc_old_version is True:
                    logger.warning("found CBC version " +
                                   _version_to_string(_cbc_version) +
                                   " < 2.7; ASL support disabled.")
                    logger.warning("Upgrade CBC to activate ASL "
                                   "support in this plugin")
            else:
                logger.warning("CBC solver is not compiled with ASL "
                               "interface.")
            # CBC doesn't not accept all asl style command line
            # options (-s in particular, which is required for
            # streaming output of all asl solvers). Therefore we need
            # to send it through the cbc_shell instead of ASL
            opt = SolverFactory('_cbc_shell', **kwds)
            opt.set_problem_format(ProblemFormat.nl)
            return opt
        elif mode == 'os':
            opt = SolverFactory('_ossolver', **kwds)
        else:
            logger.error('Unknown IO type: %s' % mode)
            return
        opt.set_options('solver=cbc')
        return opt
コード例 #8
0
 def __new__(cls, *args, **kwds):
     try:
         mode = kwds['solver_io']
         if mode is None:
             mode = 'lp'
         del kwds['solver_io']
     except KeyError:
         mode = 'lp'
     #
     if mode == 'lp':
         return SolverFactory('_cplex_shell', **kwds)
     if mode == 'mps':
         opt = SolverFactory('_cplex_shell', **kwds)
         opt.set_problem_format(ProblemFormat.mps)
         return opt
     if mode in ['python', 'direct']:
         opt = SolverFactory('cplex_direct', **kwds)
         if opt is None:
             logging.getLogger('pyomo.solvers').error(
                 'Python API for CPLEX is not installed')
             return
         return opt
     if mode == 'persistent':
         opt = SolverFactory('cplex_persistent', **kwds)
         if opt is None:
             logging.getLogger('pyomo.solvers').error(
                 'Python API for CPLEX is not installed')
             return
         return opt
     #
     if mode == 'os':
         opt = SolverFactory('_ossolver', **kwds)
     elif mode == 'nl':
         opt = SolverFactory('asl', **kwds)
     else:
         logging.getLogger('pyomo.solvers').error('Unknown IO type: %s' %
                                                  mode)
         return
     opt.set_options('solver=cplexamp')
     return opt
コード例 #9
0
 def __new__(cls, *args, **kwds):
     mode = kwds.pop('solver_io', 'lp')
     if mode is None:
         mode = 'lp'
     #
     if mode == 'lp':
         return SolverFactory('_gurobi_shell', **kwds)
     if mode == 'mps':
         opt = SolverFactory('_gurobi_shell', **kwds)
         opt.set_problem_format(ProblemFormat.mps)
         return opt
     if mode in ['python', 'direct']:
         opt = SolverFactory('gurobi_direct', **kwds)
         if opt is None:
             logger.error('Python API for GUROBI is not installed')
             return
         return opt
     if mode == 'persistent':
         opt = SolverFactory('gurobi_persistent', **kwds)
         if opt is None:
             logger.error('Python API for GUROBI is not installed')
             return
         return opt
     #
     if mode == 'os':
         opt = SolverFactory('_ossolver', **kwds)
     elif mode == 'nl':
         opt = SolverFactory('asl', **kwds)
     else:
         logger.error('Unknown IO type: %s' % mode)
         return
     opt.set_options('solver=gurobi_ampl')
     return opt
コード例 #10
0
ファイル: PICO.py プロジェクト: necaati/pyomo
    def __new__(cls, *args, **kwds):
        try:
            mode = kwds['solver_io']
            if mode is None:
                mode = 'lp'
            del kwds['solver_io']
        except KeyError:
            mode = 'lp'
        #
        if mode == 'lp':
            opt = SolverFactory('_pico_shell', **kwds)
            opt.set_problem_format(ProblemFormat.cpxlp)
            return opt
        # PICO's MPS parser seems too buggy to expose
        # this option


#        if mode  == 'mps':
#            opt = SolverFactory('_pico_shell', **kwds)
#            opt.set_problem_format(ProblemFormat.mps)
#            return opt
#
        if mode == 'nl':
            # PICO does not accept all asl style command line options
            # (-s in particular, which is required for streaming output of
            # all asl solvers). Therefore we need to send it through the cbc_shell
            # instead of ASL
            opt = SolverFactory('_pico_shell', **kwds)
            opt.set_problem_format(ProblemFormat.nl)
        elif mode == 'os':
            opt = SolverFactory('_ossolver', **kwds)
        else:
            logger.error('Unknown IO type: %s' % mode)
            return
        opt.set_options('solver=PICO')
        return opt
コード例 #11
0
ファイル: XPRESS.py プロジェクト: necaati/pyomo
    def __new__(cls, *args, **kwds):
        try:
            mode = kwds['solver_io']
            if mode is None:
                mode = 'lp'
            del kwds['solver_io']
        except KeyError:
            mode = 'lp'

        if mode == 'lp':
            return SolverFactory('_xpress_shell', **kwds)
        elif mode == 'mps':
            opt = SolverFactory('_xpress_shell', **kwds)
            opt.set_problem_format(ProblemFormat.mps)
            return opt
        elif mode == 'nl':
            opt = SolverFactory('asl', **kwds)
        elif mode in ['python', 'direct']:
            opt = SolverFactory('xpress_direct', **kwds)
            if opt is None:
                logger.error('Python API for XPRESS is not installed')
                return
            return opt
        elif mode == 'persistent':
            opt = SolverFactory('xpress_persistent', **kwds)
            if opt is None:
                logger.error('Python API for XPRESS is not installed')
                return
            return opt
        else:
            logging.getLogger('pyomo.solvers').error(
                'Unknown IO type for solver xpress: %s' % (mode))
            return
        opt.set_options('solver=amplxpress')
        return opt
コード例 #12
0
    def __new__(cls, *args, **kwds):
        mode = kwds.pop('solver_io', 'lp')

        if mode == 'lp' or mode is None:
            opt = SolverFactory('_cbc_shell', **kwds)
            opt.set_problem_format(ProblemFormat.cpxlp)
            return opt
        # CBC's MPS parser seems too buggy to expose
        # this option
        # if mode == 'mps':
        #     # *NOTE: CBC uses the COIN-OR MPS reader,
        #     #        which ignores any objective sense
        #     #        declared in the OBJSENSE section.
        #     opt = SolverFactory('_cbc_shell', **kwds)
        #     opt.set_problem_format(ProblemFormat.mps)
        #     return opt
        #
        if mode == 'nl':
            # CBC doesn't not accept all asl style command line
            # options (-s in particular, which is required for
            # streaming output of all asl solvers). Therefore we need
            # to send it through the cbc_shell instead of ASL
            opt = SolverFactory('_cbc_shell', **kwds)
            opt.set_problem_format(ProblemFormat.nl)
            return opt
        elif mode == 'os':
            opt = SolverFactory('_ossolver', **kwds)
            opt.set_options('solver=cbc')
            return opt
        else:
            logger.error('Unknown IO type: %s' % mode)
            return
コード例 #13
0
            colvar_map = self._glpk_colvar_map
            rowvar_map = self._glpk_rowvar_map

            for var_label in colvar_map:
                col = colvar_map[var_label]
                soln.variable[var_label] = {"Value": get_col_prim(lp, col)}

            for row_label in rowvar_map:
                row = rowvar_map[row_label]
                soln.constraint[row_label] = {"Value": get_row_prim(lp, row)}

        results.solution.insert(soln)

        self.results = results

        # All done with the GLPK object, so free up some memory.
        glp_free(lp)
        del self._glpk_instance, lp

        # let the base class deal with returning results.
        return OptSolver._postsolve(self)


# TODO: add MockGLPKDirect class

if not glpk_python_api_exists:
    SolverFactory().unregister('_glpk_direct')
    # SolverFactory().deactivate('_mock_glpk_direct')

# vim: set fileencoding=utf-8
コード例 #14
0
 def test_executable_isexe_abspath_user(self):
     for name in _test_names:
         with SolverFactory(name, executable=isexe_abspath_user) as opt:
             self.assertEqual(opt._user_executable, isexe_abspath)
             self.assertEqual(opt.executable(), isexe_abspath)