diff --git a/switch_model/solve.py b/switch_model/solve.py index 4af31b53..29362b4c 100755 --- a/switch_model/solve.py +++ b/switch_model/solve.py @@ -265,7 +265,7 @@ def main(args=None, return_model=False, return_instance=False): if instance.options.save_solution_file: logger.info(f"\nSaving solution file...") - save_results(instance, instance.options.outputs_dir) + save_solution_file(instance, instance.options.outputs_dir) logger.info(f"Saved solution file in {timer.step_time():.2f} s.") # report results @@ -722,6 +722,16 @@ def define_arguments(argparser): functions). """, ) + argparser.add_argument( + "--no-load-solution", + default=False, + action="store_true", + help=""" + Attempt to solve model but do not load the results from the solver. + This can be useful for reporting additional information on models + that fail to solve. + """, + ) argparser.add_argument( "--reload-prior-solution", default=False, @@ -1031,6 +1041,9 @@ def solve(model): # drop all the unspecified options solver_args = {k: v for (k, v) in solver_args.items() if v} + if model.options.no_load_solution: + solver_args["load_solutions"] = False + # Automatically send any defined suffixes to the solver # This is mostly obsolete: appsi_* solvers won't accept any suffixes but # automatically adapt to duals, slack and rc; cplex and gurobi accept @@ -1063,13 +1076,49 @@ def solve(model): try: results = model.solver_manager.solve(model, opt=model.solver, **solver_args) - except ValueError as err: - # show the solver status for obscure errors if possible - model.logger.error("\n" + "=" * 80 + "\nError during solve:\n") - try: - model.logger.error(err.__traceback__.tb_frame.f_locals["results"]) - except: - pass + except Exception as err: + # report miscellaneous errors + # TODO: convert appsi's recommendations into Switch recommendations, + # i.e., create a --no-load-results option and tell the user to set that, + # then report the actual status (without results loaded, which will get + # us down to the invalid-solution step later...) + err_str = str(err) + + # convert some errors to more useful form + if "Solver