|
4 | 4 | from __future__ import print_function |
5 | 5 |
|
6 | 6 | from pyomo.environ import * |
7 | | -from pyomo.opt import SolverFactory, SolverStatus, TerminationCondition |
| 7 | +from pyomo.opt import SolverFactory, SolverStatus, TerminationCondition, SolutionStatus |
8 | 8 | import pyomo.version |
9 | 9 | import pandas as pd |
10 | 10 |
|
@@ -1017,55 +1017,40 @@ def solve(model): |
1017 | 1017 | ) |
1018 | 1018 | breakpoint() |
1019 | 1019 |
|
1020 | | - # Treat infeasibility as an error, rather than trying to load and save the results |
1021 | | - # (note: in this case, results.solver.status may be SolverStatus.warning instead of |
1022 | | - # SolverStatus.error) |
1023 | | - if results.solver.termination_condition == TerminationCondition.infeasible: |
1024 | | - if hasattr(model, "iis"): |
1025 | | - print( |
1026 | | - "Model was infeasible; irreducibly inconsistent set (IIS) returned by solver:" |
1027 | | - ) |
1028 | | - print("\n".join(sorted(c.name for c in model.iis))) |
1029 | | - else: |
1030 | | - print( |
1031 | | - "Model was infeasible; if the solver can generate an irreducibly inconsistent set (IIS)," |
1032 | | - ) |
1033 | | - print( |
1034 | | - "more information may be available by setting the appropriate flags in the " |
1035 | | - ) |
1036 | | - print( |
1037 | | - 'solver_options_string and calling this script with "--suffixes iis" or "--gurobi-find-iis".' |
1038 | | - ) |
1039 | | - raise RuntimeError("Infeasible model") |
1040 | | - |
1041 | | - # Report any warnings; these are written to stderr so users can find them in |
1042 | | - # error logs (e.g. on HPC systems). These can occur, e.g., if solver reaches |
1043 | | - # time limit or iteration limit but still returns a valid solution |
1044 | | - if results.solver.status == SolverStatus.warning: |
1045 | | - warn( |
1046 | | - "Solver terminated with warning.\n" |
1047 | | - + " Solution Status: {}\n".format(model.solutions[-1].status) |
1048 | | - + " Termination Condition: {}".format(results.solver.termination_condition) |
1049 | | - ) |
| 1020 | + solver_status = results.solver.status |
| 1021 | + solver_message = results.solver.message |
| 1022 | + termination_condition = results.solver.termination_condition |
| 1023 | + solution_status = ( |
| 1024 | + results.solutions[-1].status if len(results.solutions) != 0 else None |
| 1025 | + ) |
1050 | 1026 |
|
1051 | 1027 | if ( |
1052 | | - results.solver.status != SolverStatus.ok |
1053 | | - or results.solver.termination_condition != TerminationCondition.optimal |
| 1028 | + solver_status != SolverStatus.ok |
| 1029 | + or termination_condition != TerminationCondition.optimal |
1054 | 1030 | ): |
1055 | 1031 | warn( |
1056 | | - f"Solver terminated with status '{results.solver.status}' and termination condition" |
1057 | | - f" {results.solver.termination_condition}" |
| 1032 | + f"Solver termination status is not 'ok' or not 'optimal':\n" |
| 1033 | + f"\t- Termination condition: {termination_condition}\n" |
| 1034 | + f"\t- Solver status: {solver_status}\n" |
| 1035 | + f"\t- Solver message: {solver_message}\n" |
| 1036 | + f"\t- Solution status: {solution_status}" |
1058 | 1037 | ) |
1059 | 1038 |
|
1060 | | - if model.options.verbose: |
1061 | | - print("") |
1062 | | - print( |
1063 | | - "Optimization termination condition was {}.".format( |
1064 | | - results.solver.termination_condition |
| 1039 | + if ( |
| 1040 | + solution_status == SolutionStatus.feasible |
| 1041 | + and solver_status == SolverStatus.warning |
| 1042 | + ): |
| 1043 | + print( |
| 1044 | + "If you used --recommended, you might want to try --recommended-robust." |
1065 | 1045 | ) |
1066 | | - ) |
1067 | | - if str(results.solver.message) != "<undefined>": |
1068 | | - print("Solver message: {}".format(results.solver.message)) |
| 1046 | + |
| 1047 | + if query_yes_no("Do you want to abort and exit?", default=None): |
| 1048 | + raise SystemExit() |
| 1049 | + |
| 1050 | + if model.options.verbose: |
| 1051 | + print(f"\nOptimization termination condition was {termination_condition}.") |
| 1052 | + if str(solver_message) != "<undefined>": |
| 1053 | + print(f"Solver message: {solver_message}") |
1069 | 1054 | print("") |
1070 | 1055 |
|
1071 | 1056 | if model.options.save_solution: |
|
0 commit comments