Skip to content

Commit

Permalink
Merge pull request Pyomo#3330 from jsiirola/maingopy-import-rework
Browse files Browse the repository at this point in the history
Allow maingo_solvermodel to be imported without maingopy
  • Loading branch information
mrmundt authored Jul 25, 2024
2 parents f86ac9d + caa8cb5 commit 1ada528
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
40 changes: 11 additions & 29 deletions pyomo/contrib/appsi/solvers/maingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,13 @@
from pyomo.repn.util import valid_expr_ctypes_minlp


def _import_SolverModel():
try:
from . import maingo_solvermodel
except ImportError:
raise
return maingo_solvermodel


maingo_solvermodel, solvermodel_available = attempt_import(
"maingo_solvermodel", importer=_import_SolverModel
)

MaingoVar = namedtuple("MaingoVar", "type name lb ub init")

logger = logging.getLogger(__name__)


def _import_maingopy():
try:
import maingopy
except ImportError:
MAiNGO._available = MAiNGO.Availability.NotFound
raise
return maingopy


maingopy, maingopy_available = attempt_import("maingopy", importer=_import_maingopy)
MaingoVar = namedtuple("MaingoVar", "type name lb ub init")
maingopy, maingopy_available = attempt_import("maingopy")
# Note that importing maingo_solvermodel will trigger the import of
# maingopy, so we defer that import using attempt_import (which will
# always succeed, even if maingopy is not available)
maingo_solvermodel = attempt_import("pyomo.contrib.appsi.solvers.maingo_solvermodel")[0]


class MAiNGOConfig(MIPSolverConfig):
Expand Down Expand Up @@ -185,9 +165,11 @@ def __init__(self, only_child_vars=False):
self._last_results_object: Optional[MAiNGOResults] = None

def available(self):
if not maingopy_available:
return self.Availability.NotFound
self._available = True
if self._available is None:
if maingopy_available:
MAiNGO._available = True
else:
MAiNGO._available = MAiNGO.Availability.NotFound
return self._available

def version(self):
Expand Down
12 changes: 2 additions & 10 deletions pyomo/contrib/appsi/solvers/maingo_solvermodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@
from pyomo.repn.util import valid_expr_ctypes_minlp


def _import_maingopy():
try:
import maingopy
except ImportError:
raise
return maingopy


maingopy, maingopy_available = attempt_import("maingopy", importer=_import_maingopy)
maingopy, maingopy_available = attempt_import("maingopy")

_plusMinusOne = {1, -1}

Expand Down Expand Up @@ -219,7 +211,7 @@ def _linear_to_maingo(self, node):
return sum(values)


class SolverModel(maingopy.MAiNGOmodel):
class SolverModel(maingopy.MAiNGOmodel if maingopy_available else object):
def __init__(self, var_list, objective, con_list, idmap, logger):
maingopy.MAiNGOmodel.__init__(self)
self._var_list = var_list
Expand Down

0 comments on commit 1ada528

Please sign in to comment.