Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow maingo_solvermodel to be imported without maingopy #3330

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading