Skip to content

Commit

Permalink
use attempt_import for gdxcc
Browse files Browse the repository at this point in the history
  • Loading branch information
renkekuhlmann committed May 19, 2020
1 parent 0a96ebd commit 4abffb4
Showing 1 changed file with 24 additions and 37 deletions.
61 changes: 24 additions & 37 deletions pyomo/solvers/plugins/solvers/GAMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from pyomo.opt.results import (SolverResults, SolverStatus, Solution,
SolutionStatus, TerminationCondition, ProblemSense)

from pyomo.common.dependencies import attempt_import
gdxcc, gdxcc_available = attempt_import('gdxcc', defer_check=True)

logger = logging.getLogger('pyomo.solvers')

Expand Down Expand Up @@ -584,25 +586,14 @@ def available(self, exception_flag=True):
"No 'gams' command found on system PATH - GAMS shell "
"solver functionality is not available.")

try:
from gdxcc import new_gdxHandle_tp, gdxCreateD, gdxClose, gdxFree
from gdxcc import gdxOpenRead, gdxDataReadRawStart, gdxDataReadRaw
from gdxcc import gdxDataReadDone, gdxSymbolInfo
if gdxcc_available:
return True
except ImportError as e:
if not exception_flag:
return False
else:
raise ImportError("Import of gams failed - GAMS direct "
"solver functionality is not available.\n"
"GAMS message: %s" % (e,))
except:
logger.warning(
"Attempting to import gams generated unexpected exception:\n"
"\t%s: %s" % (sys.exc_info()[0].__name__, sys.exc_info()[1]))
if not exception_flag:
return False
raise
elif exception_flag:
raise ImportError("Import of gams failed - GAMS direct "
"solver functionality is not available.\n"
"GAMS message: %s" % (e,))
else:
return False

def _default_executable(self):
executable = pyomo.common.Executable("gams")
Expand Down Expand Up @@ -673,10 +664,6 @@ def solve(self, *args, **kwds):
# Make sure available() doesn't crash
self.available()

from gdxcc import new_gdxHandle_tp, gdxCreateD, gdxClose, gdxFree
from gdxcc import gdxOpenRead, gdxDataReadRawStart, gdxDataReadRaw
from gdxcc import gdxDataReadDone, gdxSymbolInfo

if len(args) != 1:
raise ValueError('Exactly one model must be passed '
'to solve method of GAMSSolver.')
Expand Down Expand Up @@ -799,24 +786,24 @@ def solve(self, *args, **kwds):
'OBJVAL', 'NUMVAR', 'NUMEQU', 'NUMDVAR',
'NUMNZ', 'ETSOLVE'])

pgdx = new_gdxHandle_tp()
ret = gdxCreateD(pgdx, os.path.dirname(self.executable()), 128)
pgdx = gdxcc.new_gdxHandle_tp()
ret = gdxcc.gdxCreateD(pgdx, os.path.dirname(self.executable()), 128)
if not ret[0]:
raise RuntimeError("GAMS GDX failure (gdxCreate): %s." % ret[1])

if os.path.exists(statresults_filename):
ret = gdxOpenRead(pgdx, statresults_filename)
ret = gdxcc.gdxOpenRead(pgdx, statresults_filename)
if not ret[0]:
raise RuntimeError("GAMS GDX failure (gdxOpenRead): %d." % ret[1])

i = 0
while True:
i += 1
ret = gdxDataReadRawStart(pgdx, i)
ret = gdxcc.gdxDataReadRawStart(pgdx, i)
if not ret[0]:
break

ret = gdxSymbolInfo(pgdx, i)
ret = gdxcc.gdxSymbolInfo(pgdx, i)
if not ret[0]:
break
if len(ret) < 2:
Expand All @@ -825,7 +812,7 @@ def solve(self, *args, **kwds):
if not stat in stat_vars:
continue

ret = gdxDataReadRaw(pgdx)
ret = gdxcc.gdxDataReadRaw(pgdx)
if not ret[0] or len(ret[2]) == 0:
raise RuntimeError("GAMS GDX failure (gdxDataReadRaw).")

Expand All @@ -834,38 +821,38 @@ def solve(self, *args, **kwds):
else:
stat_vars[stat] = int(ret[2][0])

gdxDataReadDone(pgdx)
gdxClose(pgdx)
gdxcc.gdxDataReadDone(pgdx)
gdxcc.gdxClose(pgdx)

if os.path.exists(results_filename):
ret = gdxOpenRead(pgdx, results_filename)
ret = gdxcc.gdxOpenRead(pgdx, results_filename)
if not ret[0]:
raise RuntimeError("GAMS GDX failure (gdxOpenRead): %d." % ret[1])

i = 0
while True:
i += 1
ret = gdxDataReadRawStart(pgdx, i)
ret = gdxcc.gdxDataReadRawStart(pgdx, i)
if not ret[0]:
break

ret = gdxDataReadRaw(pgdx)
ret = gdxcc.gdxDataReadRaw(pgdx)
if not ret[0] or len(ret[2]) < 2:
raise RuntimeError("GAMS GDX failure (gdxDataReadRaw).")
level = self._parse_special_values(ret[2][0])
dual = self._parse_special_values(ret[2][1])

ret = gdxSymbolInfo(pgdx, i)
ret = gdxcc.gdxSymbolInfo(pgdx, i)
if not ret[0]:
break
if len(ret) < 2:
raise RuntimeError("GAMS GDX failure (gdxSymbolInfo).")
model_soln[ret[1]] = (level, dual)

gdxDataReadDone(pgdx)
gdxClose(pgdx)
gdxcc.gdxDataReadDone(pgdx)
gdxcc.gdxClose(pgdx)

gdxFree(pgdx)
gdxcc.gdxFree(pgdx)

finally:
if not keepfiles:
Expand Down

0 comments on commit 4abffb4

Please sign in to comment.