Skip to content

Commit effc5b0

Browse files
committed
Add --gurobi-make-mps flag
1 parent ddde45e commit effc5b0

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

switch_model/solve.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def define_arguments(argparser):
597597
# whether that does the same thing as --solver-options-string so we don't reuse the same name.
598598
argparser.add_argument(
599599
"--solver-options-string",
600-
default=None,
600+
default="",
601601
help="A quoted string of options to pass to the model solver. Each option must be of the form option=value. "
602602
"(e.g., --solver-options-string \"mipgap=0.001 primalopt='' advance=2 threads=1\")",
603603
)
@@ -764,6 +764,14 @@ def define_arguments(argparser):
764764
help="Path to folder of directory to use for warm start",
765765
)
766766

767+
argparser.add_argument(
768+
"--gurobi-make-mps",
769+
default=False,
770+
action="store_true",
771+
help="Instead of solving just output a Gurobi .mps file that can be used for debugging numerical properties."
772+
" See https://github.com/staadecker/lp-analyzer/ for details.",
773+
)
774+
767775

768776
def add_recommended_args(argparser):
769777
"""
@@ -933,27 +941,25 @@ def solve(model):
933941
# Note previously solver was saved in model however this is very memory inefficient.
934942
solver = SolverFactory(model.options.solver, solver_io=model.options.solver_io)
935943

936-
# If this option is enabled, gurobi will output an IIS to outputs\iis.ilp.
937-
if model.options.gurobi_find_iis:
938-
# Enable symbolic labels since otherwise we can't debug the .ilp file.
939-
model.options.symbolic_solver_labels = True
944+
if model.options.gurobi_find_iis and model.options.gurobi_make_mps:
945+
raise Exception("Can't use --gurobi-find-iis with --gurobi-make-mps.")
940946

941-
# If no string is passed make the string empty so we can add to it
942-
if model.options.solver_options_string is None:
943-
model.options.solver_options_string = ""
947+
if model.options.gurobi_find_iis or model.options.gurobi_make_mps:
948+
# If we are outputting a file we want to enable symbolic labels to help debugging
949+
model.options.symbolic_solver_labels = True
944950

951+
# If this option is enabled, gurobi will output an IIS to outputs\iis.ilp.
952+
if model.options.gurobi_find_iis:
945953
# Add to the solver options 'ResultFile=iis.ilp'
946954
# https://stackoverflow.com/a/51994135/5864903
947-
iis_file_path = os.path.join(model.options.outputs_dir, "iis.ilp")
948-
model.options.solver_options_string += " ResultFile={}".format(
949-
iis_file_path
955+
model.options.solver_options_string += " ResultFile=iis.ilp"
956+
if model.options.gurobi_make_mps:
957+
# Output the input file and set time limit to zero to ensure it doesn't actually solve
958+
model.options.solver_options_string += (
959+
f" ResultFile=problem.mps TimeLimit=0"
950960
)
951961

952962
if model.options.threads:
953-
# If no string is passed make the string empty so we can add to it
954-
if model.options.solver_options_string is None:
955-
model.options.solver_options_string = ""
956-
957963
model.options.solver_options_string += f" Threads={model.options.threads}"
958964

959965
solver_manager = SolverManagerFactory(model.options.solver_manager)

0 commit comments

Comments
 (0)