switch_model/solve.py
def define_arguments(argparser):
    """callback function to define model configuration arguments while the model is built"""

    # These flags were already processed, we only re-add them here
    # so that they appear in the help text (switch solve --help)
    add_pre_module_args(argparser)
    add_module_args(argparser)
    add_recommended_args(argparser)

    # iteration options
    argparser.add_argument(
        "--iterate-list", default=None,
        help="Text file with a list of modules to iterate until converged (default is iterate.txt); "
             "each row is one level of iteration, and there can be multiple modules on each row"
    )
    argparser.add_argument(
        "--max-iter", type=int, default=None,
        help="Maximum number of iterations to complete at each level for iterated models"
    )

    # scenario information
    argparser.add_argument(
        "--scenario-name", default="", help="Name of research scenario represented by this model"
    )

    # note: pyomo has a --solver-suffix option but it is not clear
    # whether that does the same thing as --suffix defined here,
    # so we don't reuse the same name.
    argparser.add_argument("--suffixes", "--suffix", nargs="+", action='extend', default=['rc','dual','slack'],
        help="Extra suffixes to add to the model and exchange with the solver (e.g., iis, rc, dual, or slack)")

    # Define solver-related arguments
    # These are a subset of the arguments offered by "pyomo solve --solver=cplex --help"
    argparser.add_argument("--solver-manager", default="serial",
        help='Name of Pyomo solver manager to use for the model ("neos" to use remote NEOS server)')
    argparser.add_argument("--solver-io", default=None, help="Method for Pyomo to use to communicate with solver")
    # note: pyomo has a --solver-options option but it is not clear
    # whether that does the same thing as --solver-options-string so we don't reuse the same name.
    argparser.add_argument("--solver-options-string", default="",
        help='A quoted string of options to pass to the model solver. Each option must be of the form option=value. '
            '(e.g., --solver-options-string "mipgap=0.001 primalopt=\'\' advance=2 threads=1")')
    argparser.add_argument("--solver-method", default=None, type=str,
                           help="Specify the solver method to use.")
    argparser.add_argument("--keepfiles", action='store_true', default=None,
        help="Keep temporary files produced by the solver (may be useful with --symbolic-solver-labels)")
    argparser.add_argument(
        "--stream-output", "--stream-solver", action='store_true', dest="tee", default=None,
        help="Display information from the solver about its progress (usually combined with a suitable --solver-options-string)")
    argparser.add_argument(
        "--no-stream-output", "--no-stream-solver", action='store_false', dest="tee", default=None,
        help="Don't display information from the solver about its progress")
    argparser.add_argument(
        "--symbolic-solver-labels", action='store_true', default=None,
        help='Use symbol names derived from the model when interfacing with the solver. '
            'See "pyomo solve --solver=x --help" for more details.')
    argparser.add_argument("--tempdir", default=None,
        help='The name of a directory to hold temporary files produced by the solver. '
             'This is usually paired with --keepfiles and --symbolic-solver-labels.')
    argparser.add_argument(
        '--retrieve-cplex-mip-duals', default=False, action='store_true',
        help=(
            "Patch Pyomo's solver script for cplex to re-solve and retrieve "
            "dual values for mixed-integer programs."
        )
    )
    argparser.add_argument(
        '--gurobi-find-iis', default=False, action='store_true',
        help='Make Gurobi compute an irreducible inconsistent subsystem (IIS) if the model is found to be infeasible. '
             'The IIS will be writen to outputs\\iis.ilp. Note this flag enables --symbolic-solver-labels since '
             'otherwise debugging would be impossible. To learn more about IIS read: '
             'https://www.gurobi.com/documentation/9.1/refman/py_model_computeiis.html.'
    )

    # NOTE: the following could potentially be made into standard arguments for all models,
    # e.g. by defining them in a define_standard_arguments() function in switch.utilities.py

    # Define input/output options
    # note: --inputs-dir is defined in add_module_args, because it may specify the
    # location of the module list (deprecated)
    # argparser.add_argument("--inputs-dir", default="inputs",
    #     help='Directory containing input files (default is "inputs")')
    argparser.add_argument(
        "--input-alias", "--input-aliases", dest="input_aliases", nargs='+', default=[],
        help='List of input file substitutions, in form of standard_file.csv=alternative_file.csv, '
        'useful for sensitivity studies with different inputs.')
    argparser.add_argument("--outputs-dir", default="outputs",
        help='Directory to write output files (default is "outputs")')

    # General purpose arguments
    argparser.add_argument(
        '--verbose', '-v', dest='verbose', default=False, action='store_true',
        help='Show information about model preparation and solution')
    argparser.add_argument(
        '--quiet', '-q', dest='verbose', action='store_false',
        help="Don't show information about model preparation and solution (cancels --verbose setting)")
    argparser.add_argument(
        '--no-post-solve', default=False, action='store_true',
        help="Don't run post-solve code on the completed model (i.e., reporting functions).")
    argparser.add_argument(
        '--reload-prior-solution', default=False, action='store_true',
        help='Load a previously saved solution; useful for re-running post-solve code or interactively exploring the model (via --interact).')
    argparser.add_argument(
        '--save-solution', default=False, action='store_true',
        help="Save the solution to a pickle file after model is solved to allow for later inspection via --reload-prior-solution.")
    argparser.add_argument(
        '--save-warm-start', default=False, action='store_true',
        help="Save warm_start.pickle to the outputs which allows future runs to warm start from this one."
    )
    argparser.add_argument(
        '--interact', default=False, action='store_true',
        help='Enter interactive shell after solving the instance to enable inspection of the solved model.')
    argparser.add_argument(
        '--enable-breakpoints', default=False, action='store_true',
        help='Break and enter the Python Debugger at key points during the solving process.'
    )
    argparser.add_argument(
        "--sig-figs-output", default=5, type=int,
        help='The number of significant digits to include in the output by default'
    )
    argparser.add_argument(
        "--zero-cutoff-output", default=1e-5, type=float,
        help="If the magnitude of an output value is less than this value, it is rounded to 0."
    )

    argparser.add_argument(
        "--sorted-output", default=False, action='store_true',
        dest='sorted_output',
        help='Write generic variable result values in sorted order')
    argparser.add_argument(
        "--graph", default=False, action='store_true',
        help="Automatically run switch graph after post solve"
    )
    argparser.add_argument(
        "--no-crossover", default=False, action='store_true',
        help="Disables crosssover when using the barrier algorithm. This reduces"
             ' the solve time greatly however may result in less accurate values and may fail to find an optimal'
             " solution. If you find that the solver returns a suboptimal solution remove this flag."
    )

    argparser.add_argument(
        "--threads", type=int, default=None,
        help="Number of threads to be used while solving. Currently only supported for Gurobi"
    )

    argparser.add_argument(
        "--warm-start-mip", default=None,
        help="Enables warm start for a Mixed Integer problem by specifying the "
             "path to a previous scenario. Warm starting only works if the solution to the previous solution"
             "is also a feasible (but not necessarily optimal) solution to the current scenario."
    )

    argparser.add_argument(
        "--warm-start", default=None,
        help="Enables warm start for a LP Problem by specifying the path to the previous scenario. Note"
             " that all variables must be the same between the previous and current scenario."
    )

    argparser.add_argument(
        "--gurobi-make-mps", default=False, action="store_true",
        help="Instead of solving just output a Gurobi .mps file that can be used for debugging numerical properties."
             " See https://github.com/staadecker/lp-analyzer/ for details."
    )
callback function to define model configuration arguments while the model is built
callback function to define model configuration arguments while the model is built
callback function to define model configuration arguments while the model is built
def define_arguments(argparser):
    add_pre_module_args(argparser)
    add_module_args(argparser)
    add_recommended_args(argparser)
    argparser.add_argument(
        "--iterate-list", default=None,
        help="Text file with a list of modules to iterate until converged (default is iterate.txt); "
             "each row is one level of iteration, and there can be multiple modules on each row"
    )
    argparser.add_argument(
        "--max-iter", type=int, default=None,
        help="Maximum number of iterations to complete at each level for iterated models"
    )
    argparser.add_argument(
        "--scenario-name", default="", help="Name of research scenario represented by this model"
    )
    argparser.add_argument("--suffixes", "--suffix", nargs="+", action='extend', default=['rc','dual','slack'],
        help="Extra suffixes to add to the model and exchange with the solver (e.g., iis, rc, dual, or slack)")
    argparser.add_argument("--solver-manager", default="serial",
        help='Name of Pyomo solver manager to use for the model ("neos" to use remote NEOS server)')
    argparser.add_argument("--solver-io", default=None, help="Method for Pyomo to use to communicate with solver")
    argparser.add_argument("--solver-options-string", default="",
        help='A quoted string of options to pass to the model solver. Each option must be of the form option=value. '
            '(e.g., --solver-options-string "mipgap=0.001 primalopt=\'\' advance=2 threads=1")')
    argparser.add_argument("--solver-method", default=None, type=str,
                           help="Specify the solver method to use.")
    argparser.add_argument("--keepfiles", action='store_true', default=None,
        help="Keep temporary files produced by the solver (may be useful with --symbolic-solver-labels)")
    argparser.add_argument(
        "--stream-output", "--stream-solver", action='store_true', dest="tee", default=None,
        help="Display information from the solver about its progress (usually combined with a suitable --solver-options-string)")
    argparser.add_argument(
        "--no-stream-output", "--no-stream-solver", action='store_false', dest="tee", default=None,
        help="Don't display information from the solver about its progress")
    argparser.add_argument(
        "--symbolic-solver-labels", action='store_true', default=None,
        help='Use symbol names derived from the model when interfacing with the solver. '
            'See "pyomo solve --solver=x --help" for more details.')
    argparser.add_argument("--tempdir", default=None,
        help='The name of a directory to hold temporary files produced by the solver. '
             'This is usually paired with --keepfiles and --symbolic-solver-labels.')
    argparser.add_argument(
        '--retrieve-cplex-mip-duals', default=False, action='store_true',
        help=(
            "Patch Pyomo's solver script for cplex to re-solve and retrieve "
            "dual values for mixed-integer programs."
        )
    )
    argparser.add_argument(
        '--gurobi-find-iis', default=False, action='store_true',
        help='Make Gurobi compute an irreducible inconsistent subsystem (IIS) if the model is found to be infeasible. '
             'The IIS will be writen to outputs\\iis.ilp. Note this flag enables --symbolic-solver-labels since '
             'otherwise debugging would be impossible. To learn more about IIS read: '
             'https://www.gurobi.com/documentation/9.1/refman/py_model_computeiis.html.'
    )
    argparser.add_argument(
        "--input-alias", "--input-aliases", dest="input_aliases", nargs='+', default=[],
        help='List of input file substitutions, in form of standard_file.csv=alternative_file.csv, '
        'useful for sensitivity studies with different inputs.')
    argparser.add_argument("--outputs-dir", default="outputs",
        help='Directory to write output files (default is "outputs")')
    argparser.add_argument(
        '--verbose', '-v', dest='verbose', default=False, action='store_true',
        help='Show information about model preparation and solution')
    argparser.add_argument(
        '--quiet', '-q', dest='verbose', action='store_false',
        help="Don't show information about model preparation and solution (cancels --verbose setting)")
    argparser.add_argument(
        '--no-post-solve', default=False, action='store_true',
        help="Don't run post-solve code on the completed model (i.e., reporting functions).")
    argparser.add_argument(
        '--reload-prior-solution', default=False, action='store_true',
        help='Load a previously saved solution; useful for re-running post-solve code or interactively exploring the model (via --interact).')
    argparser.add_argument(
        '--save-solution', default=False, action='store_true',
        help="Save the solution to a pickle file after model is solved to allow for later inspection via --reload-prior-solution.")
    argparser.add_argument(
        '--save-warm-start', default=False, action='store_true',
        help="Save warm_start.pickle to the outputs which allows future runs to warm start from this one."
    )
    argparser.add_argument(
        '--interact', default=False, action='store_true',
        help='Enter interactive shell after solving the instance to enable inspection of the solved model.')
    argparser.add_argument(
        '--enable-breakpoints', default=False, action='store_true',
        help='Break and enter the Python Debugger at key points during the solving process.'
    )
    argparser.add_argument(
        "--sig-figs-output", default=5, type=int,
        help='The number of significant digits to include in the output by default'
    )
    argparser.add_argument(
        "--zero-cutoff-output", default=1e-5, type=float,
        help="If the magnitude of an output value is less than this value, it is rounded to 0."
    )
    argparser.add_argument(
        "--sorted-output", default=False, action='store_true',
        dest='sorted_output',
        help='Write generic variable result values in sorted order')
    argparser.add_argument(
        "--graph", default=False, action='store_true',
        help="Automatically run switch graph after post solve"
    )
    argparser.add_argument(
        "--no-crossover", default=False, action='store_true',
        help="Disables crosssover when using the barrier algorithm. This reduces"
             ' the solve time greatly however may result in less accurate values and may fail to find an optimal'
             " solution. If you find that the solver returns a suboptimal solution remove this flag."
    )
    argparser.add_argument(
        "--threads", type=int, default=None,
        help="Number of threads to be used while solving. Currently only supported for Gurobi"
    )
    argparser.add_argument(
        "--warm-start-mip", default=None,
        help="Enables warm start for a Mixed Integer problem by specifying the "
             "path to a previous scenario. Warm starting only works if the solution to the previous solution"
             "is also a feasible (but not necessarily optimal) solution to the current scenario."
    )
    argparser.add_argument(
        "--warm-start", default=None,
        help="Enables warm start for a LP Problem by specifying the path to the previous scenario. Note"
             " that all variables must be the same between the previous and current scenario."
    )
    argparser.add_argument(
        "--gurobi-make-mps", default=False, action="store_true",
        help="Instead of solving just output a Gurobi .mps file that can be used for debugging numerical properties."
             " See https://github.com/staadecker/lp-analyzer/ for details."
    )
def define_arguments ( argparser ) : add_pre_module_args ( argparser ) add_module_args ( argparser ) add_recommended_args ( argparser ) argparser . add_argument ( "--iterate-list" , default = None , help = "Text file with a list of modules to iterate until converged (default is iterate.txt); " "each row is one level of iteration, and there can be multiple modules on each row" ) argparser . add_argument ( "--max-iter" , type = int , default = None , help = "Maximum number of iterations to complete at each level for iterated models" ) argparser . add_argument ( "--scenario-name" , default = "" , help = "Name of research scenario represented by this model" ) argparser . add_argument ( "--suffixes" , "--suffix" , nargs = "+" , action = 'extend' , default = [ 'rc' , 'dual' , 'slack' ] , help = "Extra suffixes to add to the model and exchange with the solver (e.g., iis, rc, dual, or slack)" ) argparser . add_argument ( "--solver-manager" , default = "serial" , help = 'Name of Pyomo solver manager to use for the model ("neos" to use remote NEOS server)' ) argparser . add_argument ( "--solver-io" , default = None , help = "Method for Pyomo to use to communicate with solver" ) argparser . add_argument ( "--solver-options-string" , default = "" , help = 'A quoted string of options to pass to the model solver. Each option must be of the form option=value. ' '(e.g., --solver-options-string "mipgap=0.001 primalopt=\'\' advance=2 threads=1")' ) argparser . add_argument ( "--solver-method" , default = None , type = str , help = "Specify the solver method to use." ) argparser . add_argument ( "--keepfiles" , action = 'store_true' , default = None , help = "Keep temporary files produced by the solver (may be useful with --symbolic-solver-labels)" ) argparser . add_argument ( "--stream-output" , "--stream-solver" , action = 'store_true' , dest = "tee" , default = None , help = "Display information from the solver about its progress (usually combined with a suitable --solver-options-string)" ) argparser . add_argument ( "--no-stream-output" , "--no-stream-solver" , action = 'store_false' , dest = "tee" , default = None , help = "Don't display information from the solver about its progress" ) argparser . add_argument ( "--symbolic-solver-labels" , action = 'store_true' , default = None , help = 'Use symbol names derived from the model when interfacing with the solver. ' 'See "pyomo solve --solver=x --help" for more details.' ) argparser . add_argument ( "--tempdir" , default = None , help = 'The name of a directory to hold temporary files produced by the solver. ' 'This is usually paired with --keepfiles and --symbolic-solver-labels.' ) argparser . add_argument ( '--retrieve-cplex-mip-duals' , default = False , action = 'store_true' , help = ( "Patch Pyomo's solver script for cplex to re-solve and retrieve " "dual values for mixed-integer programs." ) ) argparser . add_argument ( '--gurobi-find-iis' , default = False , action = 'store_true' , help = 'Make Gurobi compute an irreducible inconsistent subsystem (IIS) if the model is found to be infeasible. ' 'The IIS will be writen to outputs\\iis.ilp. Note this flag enables --symbolic-solver-labels since ' 'otherwise debugging would be impossible. To learn more about IIS read: ' 'https://www.gurobi.com/documentation/9.1/refman/py_model_computeiis.html.' ) argparser . add_argument ( "--input-alias" , "--input-aliases" , dest = "input_aliases" , nargs = '+' , default = [ ] , help = 'List of input file substitutions, in form of standard_file.csv=alternative_file.csv, ' 'useful for sensitivity studies with different inputs.' ) argparser . add_argument ( "--outputs-dir" , default = "outputs" , help = 'Directory to write output files (default is "outputs")' ) argparser . add_argument ( '--verbose' , '-v' , dest = 'verbose' , default = False , action = 'store_true' , help = 'Show information about model preparation and solution' ) argparser . add_argument ( '--quiet' , '-q' , dest = 'verbose' , action = 'store_false' , help = "Don't show information about model preparation and solution (cancels --verbose setting)" ) argparser . add_argument ( '--no-post-solve' , default = False , action = 'store_true' , help = "Don't run post-solve code on the completed model (i.e., reporting functions)." ) argparser . add_argument ( '--reload-prior-solution' , default = False , action = 'store_true' , help = 'Load a previously saved solution; useful for re-running post-solve code or interactively exploring the model (via --interact).' ) argparser . add_argument ( '--save-solution' , default = False , action = 'store_true' , help = "Save the solution to a pickle file after model is solved to allow for later inspection via --reload-prior-solution." ) argparser . add_argument ( '--save-warm-start' , default = False , action = 'store_true' , help = "Save warm_start.pickle to the outputs which allows future runs to warm start from this one." ) argparser . add_argument ( '--interact' , default = False , action = 'store_true' , help = 'Enter interactive shell after solving the instance to enable inspection of the solved model.' ) argparser . add_argument ( '--enable-breakpoints' , default = False , action = 'store_true' , help = 'Break and enter the Python Debugger at key points during the solving process.' ) argparser . add_argument ( "--sig-figs-output" , default = 5 , type = int , help = 'The number of significant digits to include in the output by default' ) argparser . add_argument ( "--zero-cutoff-output" , default = 1e-5 , type = float , help = "If the magnitude of an output value is less than this value, it is rounded to 0." ) argparser . add_argument ( "--sorted-output" , default = False , action = 'store_true' , dest = 'sorted_output' , help = 'Write generic variable result values in sorted order' ) argparser . add_argument ( "--graph" , default = False , action = 'store_true' , help = "Automatically run switch graph after post solve" ) argparser . add_argument ( "--no-crossover" , default = False , action = 'store_true' , help = "Disables crosssover when using the barrier algorithm. This reduces" ' the solve time greatly however may result in less accurate values and may fail to find an optimal' " solution. If you find that the solver returns a suboptimal solution remove this flag." ) argparser . add_argument ( "--threads" , type = int , default = None , help = "Number of threads to be used while solving. Currently only supported for Gurobi" ) argparser . add_argument ( "--warm-start-mip" , default = None , help = "Enables warm start for a Mixed Integer problem by specifying the " "path to a previous scenario. Warm starting only works if the solution to the previous solution" "is also a feasible (but not necessarily optimal) solution to the current scenario." ) argparser . add_argument ( "--warm-start" , default = None , help = "Enables warm start for a LP Problem by specifying the path to the previous scenario. Note" " that all variables must be the same between the previous and current scenario." ) argparser . add_argument ( "--gurobi-make-mps" , default = False , action = "store_true" , help = "Instead of solving just output a Gurobi .mps file that can be used for debugging numerical properties." " See https://github.com/staadecker/lp-analyzer/ for details." )
callback function to define model configuration arguments while the model is built
callback function to define model configuration arguments while the model is built
"""callback function to define model configuration arguments while the model is built""" # These flags were already processed, we only re-add them here # so that they appear in the help text (switch solve --help) # iteration options # scenario information # note: pyomo has a --solver-suffix option but it is not clear # whether that does the same thing as --suffix defined here, # so we don't reuse the same name. # Define solver-related arguments # These are a subset of the arguments offered by "pyomo solve --solver=cplex --help" # note: pyomo has a --solver-options option but it is not clear # whether that does the same thing as --solver-options-string so we don't reuse the same name. # NOTE: the following could potentially be made into standard arguments for all models, # e.g. by defining them in a define_standard_arguments() function in switch.utilities.py # Define input/output options # note: --inputs-dir is defined in add_module_args, because it may specify the # location of the module list (deprecated) # argparser.add_argument("--inputs-dir", default="inputs", #     help='Directory containing input files (default is "inputs")') # General purpose arguments
argparser