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

Apply black to pyomo/solvers directory py files #2732

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion pyomo/solvers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

21 changes: 11 additions & 10 deletions pyomo/solvers/mockmip.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@

from pyomo.opt.base.solvers import _extract_version


class MockMIP(object):
"""Methods used to create a mock MIP solver used for testing
"""
"""Methods used to create a mock MIP solver used for testing"""

def __init__(self, mockdir):
self.mock_subdir=mockdir
self.mock_subdir = mockdir

def create_command_line(self,executable,problem_files):
def create_command_line(self, executable, problem_files):
self._mock_problem = basename(problem_files[0]).split('.')[0]
self._mock_dir = dirname(problem_files[0])

def _default_executable(self):
return "mock"

executable = _default_executable

def version(self):
return _extract_version('')

def _execute_command(self,cmd):
def _execute_command(self, cmd):
mock_basename = join(self._mock_dir, self.mock_subdir, self._mock_problem)
if self._soln_file is not None:
# prefer .sol over .soln
Expand All @@ -48,12 +49,12 @@ def _execute_command(self,cmd):
for file in glob.glob(mock_basename + "*"):
if file.split(".")[-1] != "out":
shutil.copyfile(file, join(self._mock_dir, basename(file)))
log=""
log = ""
fname = mock_basename + ".out"
if not isfile(fname):
raise ValueError("Missing mock data file: "+fname)
INPUT=open(mock_basename + ".out")
raise ValueError("Missing mock data file: " + fname)
INPUT = open(mock_basename + ".out")
for line in INPUT:
log = log+line
log = log + line
INPUT.close()
return [0,log]
return [0, log]
1 change: 1 addition & 0 deletions pyomo/solvers/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________


def load():
import pyomo.solvers.plugins.converter
import pyomo.solvers.plugins.solvers
35 changes: 20 additions & 15 deletions pyomo/solvers/plugins/converter/ampl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

@ProblemConverterFactory.register('ampl')
class AmplMIPConverter(object):

def can_convert(self, from_type, to_type):
"""Returns true if this object supports the specified conversion"""
#
Expand All @@ -45,12 +44,12 @@ def apply(self, *args, **kwargs):
_exec = pyomo.common.Executable("ampl")
if not _exec:
raise ConverterError("The 'ampl' executable cannot be found")
script_filename = TempfileManager.create_tempfile(suffix = '.ampl')
script_filename = TempfileManager.create_tempfile(suffix='.ampl')

if args[1] == ProblemFormat.nl:
output_filename = TempfileManager.create_tempfile(suffix = '.nl')
output_filename = TempfileManager.create_tempfile(suffix='.nl')
else:
output_filename = TempfileManager.create_tempfile(suffix = '.mps')
output_filename = TempfileManager.create_tempfile(suffix='.mps')

cmd = [_exec.path(), script_filename]
#
Expand All @@ -61,22 +60,28 @@ def apply(self, *args, **kwargs):
OUTPUT.write("# AMPL script for converting the following files\n")
OUTPUT.write("#\n")
if len(args[2:]) == 1:
OUTPUT.write('model '+args[2]+";\n")
OUTPUT.write('model ' + args[2] + ";\n")
else:
OUTPUT.write('model '+args[2]+";\n")
OUTPUT.write('data '+args[3]+";\n")
OUTPUT.write('model ' + args[2] + ";\n")
OUTPUT.write('data ' + args[3] + ";\n")
abs_ofile = os.path.abspath(output_filename)
if args[1] == ProblemFormat.nl:
OUTPUT.write('write g'+abs_ofile[:-3]+";\n")
OUTPUT.write('write g' + abs_ofile[:-3] + ";\n")
else:
OUTPUT.write('write m'+abs_ofile[:-4]+";\n")
OUTPUT.write('write m' + abs_ofile[:-4] + ";\n")
OUTPUT.close()
#
# Execute command and cleanup
#
output = subprocess.run(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
if not os.path.exists(output_filename): #pragma:nocover
raise ApplicationError("Problem launching 'ampl' to create '%s': %s" % (output_filename, output.stdout))
return (output_filename,),None # empty variable map
output = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
if not os.path.exists(output_filename): # pragma:nocover
raise ApplicationError(
"Problem launching 'ampl' to create '%s': %s"
% (output_filename, output.stdout)
)
return (output_filename,), None # empty variable map
64 changes: 36 additions & 28 deletions pyomo/solvers/plugins/converter/glpsol.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

@ProblemConverterFactory.register('glpsol')
class GlpsolMIPConverter(object):

def can_convert(self, from_type, to_type):
"""Returns true if this object supports the specified conversion"""
#
Expand Down Expand Up @@ -54,60 +53,69 @@ def apply(self, *args, **kwargs):
# MPS->LP conversion is ignored in coverage because it's not being
# used; instead, we're using pico_convert for this conversion
#
modfile=''
if args[1] == ProblemFormat.mps: #pragma:nocover
ofile = TempfileManager.create_tempfile(suffix = '.glpsol.mps')
cmd.extend([
"--check",
"--name", "MPS model derived from "+os.path.basename(args[2]),
"--wfreemps", ofile
])
modfile = ''
if args[1] == ProblemFormat.mps: # pragma:nocover
ofile = TempfileManager.create_tempfile(suffix='.glpsol.mps')
cmd.extend(
[
"--check",
"--name",
"MPS model derived from " + os.path.basename(args[2]),
"--wfreemps",
ofile,
]
)
elif args[1] == ProblemFormat.cpxlp:
ofile = TempfileManager.create_tempfile(suffix = '.glpsol.lp')
cmd.extend([
"--check",
"--name","MPS model derived from "+os.path.basename(args[2]),
"--wcpxlp", ofile
])
ofile = TempfileManager.create_tempfile(suffix='.glpsol.lp')
cmd.extend(
[
"--check",
"--name",
"MPS model derived from " + os.path.basename(args[2]),
"--wcpxlp",
ofile,
]
)
if len(args[2:]) == 1:
cmd.append(args[2])
else:
#
# Create a temporary model file, since GLPSOL can only
# handle one input file
#
modfile = TempfileManager.create_tempfile(suffix = '.glpsol.mod')
OUTPUT=open(modfile,"w")
flag=False
modfile = TempfileManager.create_tempfile(suffix='.glpsol.mod')
OUTPUT = open(modfile, "w")
flag = False
#
# Read the model file
#
INPUT= open(args[2])
INPUT = open(args[2])
for line in INPUT:
line = line.strip()
if line == "data;":
raise ConverterError("Problem composing mathprog model and data files - mathprog file already has data in it!")
raise ConverterError(
"Problem composing mathprog model and data files - mathprog file already has data in it!"
)
if line != "end;":
OUTPUT.write(line+'\n')
OUTPUT.write(line + '\n')
INPUT.close()
OUTPUT.write("data;\n")
#
# Read the data files
#
for file in args[3:]:
INPUT= open(file)
INPUT = open(file)
for line in INPUT:
line = line.strip()
if line != "end;" and line != "data;":
OUTPUT.write(line+'\n')
OUTPUT.write(line + '\n')
INPUT.close()
OUTPUT.write("end;\n")
OUTPUT.close()
cmd.append(modfile)
subprocess.run(cmd, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if not os.path.exists(ofile): #pragma:nocover
raise ApplicationError("Problem launching 'glpsol' to create "+ofile)
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if not os.path.exists(ofile): # pragma:nocover
raise ApplicationError("Problem launching 'glpsol' to create " + ofile)
if os.path.exists(modfile):
os.remove(modfile)
return (ofile,),None # empty variable map
return (ofile,), None # empty variable map
Loading