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

Defer starting the SAS session until needed #3448

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 24 additions & 17 deletions pyomo/solvers/plugins/solvers/SAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def _presolve(self, *args, **kwds):

def available(self, exception_flag=False):
"""True if the solver is available"""
return self._python_api_exists
if not self._python_api_exists:
return False
return self.start_sas_session() is not None

def _has_integer_variables(self):
"""True if the problem has integer variables."""
Expand Down Expand Up @@ -285,12 +287,7 @@ def __init__(self, **kwds):

# Store other options for the SAS session
self._session_options = kwds

# Create the session
try:
self._sas_session = self._sas.SASsession(**self._session_options)
except:
self._sas_session = None
self._sas_session = None

def __del__(self):
# Close the session, if we created one
Expand All @@ -314,6 +311,15 @@ def _create_statement_str(self, statement):
def sas_version(self):
return self._sasver

def start_sas_session(self):
if self._sas_session is None:
# Create (and cache) the session
try:
self._sas_session = self._sas.SASsession(**self._session_options)
except:
pass
return self._sas_session

def _apply_solver(self):
""" "Prepare the options and run the solver. Then store the data to be returned."""
logger.debug("Running SAS")
Expand Down Expand Up @@ -374,10 +380,7 @@ def _apply_solver(self):
sas_options = "option notes nonumber nodate nosource pagesize=max;"

# Get the current SAS session, submit the code and return the results
if not self._sas_session:
sas = self._sas_session = self._sas.SASsession(**self._session_options)
else:
sas = self._sas_session
sas = self.start_sas_session()

# Find the version of 9.4 we are using
self._sasver = sas.sasver
Expand Down Expand Up @@ -585,19 +588,23 @@ def __init__(self, **kwds):
self._python_api_exists = True

self._session_options = kwds

# Create the session
try:
self._sas_session = self._sas.CAS(**self._session_options)
except:
self._sas_session = None
self._sas_session = None

def __del__(self):
# Close the session, if we created one
if self._sas_session:
self._sas_session.close()
del self._sas_session

def start_sas_session(self):
if self._sas_session is None:
# Create (and cache) the session
try:
self._sas_session = self._sas.CAS(**self._session_options)
except:
pass
return self._sas_session

def _uploadMpsFile(self, s, unique):
# Declare a unique table name for the mps table
mpsdata_table_name = "mps" + unique
Expand Down
15 changes: 9 additions & 6 deletions pyomo/solvers/tests/checks/test_SAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Suffix,
)
from pyomo.opt.results import SolverStatus, TerminationCondition, ProblemSense
from pyomo.opt import SolverFactory, check_available_solvers
from pyomo.opt import SolverFactory
import warnings

CFGFILE = os.environ.get("SAS_CFG_FILE_PATH", None)
Expand All @@ -38,7 +38,10 @@
}


sas_available = check_available_solvers("sas")
try:
sas94_available = SolverFactory('_sas94').available()
except:
sas94_available = False


class SASTestAbc:
Expand Down Expand Up @@ -292,7 +295,7 @@ def test_solver_with_milp(self):
)


@unittest.skipIf(not sas_available, "The SAS solver is not available")
@unittest.skipIf(not sas94_available, "The SAS94 solver interface is not available")
class SASTestLP94(SASTestLP, unittest.TestCase):
@mock.patch(
"pyomo.solvers.plugins.solvers.SAS.SAS94.sas_version",
Expand Down Expand Up @@ -325,7 +328,7 @@ def test_solver_error(self, submit_mock, symget_mock):
self.assertEqual(results.solver.status, SolverStatus.error)


# @unittest.skipIf(not sas_available, "The SAS solver is not available")
# @unittest.skipIf(not sascas_available, "The SAS solver is not available")
@unittest.skip("Tests not yet configured for SAS Viya interface.")
class SASTestLPCAS(SASTestLP, unittest.TestCase):
solver_io = "_sascas"
Expand Down Expand Up @@ -526,13 +529,13 @@ def test_solver_warmstart_capable(self):
self.assertTrue(self.opt_sas.warm_start_capable())


# @unittest.skipIf(not sas_available, "The SAS solver is not available")
# @unittest.skipIf(not sas94_available, "The SAS solver is not available")
@unittest.skip("MILP94 tests disabled.")
class SASTestMILP94(SASTestMILP, unittest.TestCase):
pass


# @unittest.skipIf(not sas_available, "The SAS solver is not available")
# @unittest.skipIf(not sascas_available, "The SAS solver is not available")
@unittest.skip("Tests not yet configured for SAS Viya interface.")
class SASTestMILPCAS(SASTestMILP, unittest.TestCase):
solver_io = "_sascas"
Expand Down
Loading