Skip to content

Commit 62d6093

Browse files
committed
Merge remote-tracking branch 'rael/wecc' into cplex
# Conflicts: # switch_model/solve.py
2 parents faa9dec + 8362e91 commit 62d6093

File tree

6 files changed

+27
-32
lines changed

6 files changed

+27
-32
lines changed

switch_model/solve.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ def define_arguments(argparser):
564564
" that all variables must be the same between the previous and current scenario."
565565
)
566566

567+
argparser.add_argument(
568+
"--gurobi-make-mps", default=False, action="store_true",
569+
help="Instead of solving just output a Gurobi .mps file that can be used for debugging numerical properties."
570+
" See https://github.com/staadecker/lp-analyzer/ for details."
571+
)
572+
567573

568574
def add_recommended_args(argparser):
569575
"""
@@ -725,15 +731,21 @@ def solve(model):
725731
solver = SolverFactory(solver_type, solver_io=model.options.solver_io)
726732
solver_manager = SolverManagerFactory(model.options.solver_manager)
727733

728-
# If this option is enabled, gurobi will output an IIS to outputs\iis.ilp.
729-
if model.options.gurobi_find_iis:
730-
# Enable symbolic labels since otherwise we can't debug the .ilp file.
734+
if model.options.gurobi_find_iis and model.options.gurobi_make_mps:
735+
raise Exception("Can't use --gurobi-find-iis with --gurobi-make-mps.")
736+
737+
if model.options.gurobi_find_iis or model.options.gurobi_make_mps:
738+
# If we are outputting a file we want to enable symbolic labels to help debugging
731739
model.options.symbolic_solver_labels = True
732740

733-
# Add to the solver options 'ResultFile=iis.ilp'
734-
# https://stackoverflow.com/a/51994135/5864903
735-
iis_file_path = os.path.join(model.options.outputs_dir, "iis.ilp")
736-
options_string += f" ResultFile={iis_file_path}"
741+
# If this option is enabled, gurobi will output an IIS to outputs\iis.ilp.
742+
if model.options.gurobi_find_iis:
743+
# Add to the solver options 'ResultFile=iis.ilp'
744+
# https://stackoverflow.com/a/51994135/5864903
745+
model.options.solver_options_string += " ResultFile=iis.ilp"
746+
if model.options.gurobi_make_mps:
747+
# Output the input file and set time limit to zero to ensure it doesn't actually solve
748+
model.options.solver_options_string += f" ResultFile=problem.mps TimeLimit=0"
737749

738750
if model.options.no_crossover:
739751
if solver_type in gurobi_types:

switch_model/tools/graph/main.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,13 @@
2121
import plotnine
2222

2323
# Local imports
24-
from switch_model.utilities import StepTimer, get_module_list, query_yes_no
24+
from switch_model.utilities import StepTimer, get_module_list, query_yes_no, catch_exceptions
2525

2626
# When True exceptions that are thrown while graphing will be caught
2727
# and outputted to console as a warning instead of an error
2828
CATCH_EXCEPTIONS = True
2929

3030

31-
def catch_exceptions(func):
32-
"""
33-
Decorator that wraps a function such that exceptions are caught and outputted as warnings instead.
34-
"""
35-
36-
@functools.wraps(func)
37-
def wrapper(*args, **kwargs):
38-
if not CATCH_EXCEPTIONS:
39-
return func(*args, **kwargs)
40-
try:
41-
return func(*args, **kwargs)
42-
except:
43-
warnings.warn(f"The following error was caught and we are moving on."
44-
f"{traceback.format_exc()}")
45-
46-
return wrapper
47-
48-
4931
# List of graphing functions. Every time a function uses the @graph() decorator,
5032
# the function gets registered here.
5133
registered_graphs = {}
@@ -75,7 +57,7 @@ def graph(
7557

7658
def decorator(func):
7759
@functools.wraps(func)
78-
@catch_exceptions
60+
@catch_exceptions("Failed to run a graphing function.", should_catch=CATCH_EXCEPTIONS)
7961
def wrapper(tools: GraphTools):
8062
if tools.skip_long and is_long:
8163
return

switch_model/utilities/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def query_yes_no(question, default="yes"):
881881
"(or 'y' or 'n').\n")
882882

883883

884-
def catch_exceptions(warning_msg=None, should_catch=True):
884+
def catch_exceptions(warning_msg="An exception was caught and ignored.", should_catch=True):
885885
"""Decorator that catches exceptions."""
886886

887887
def decorator(func):
@@ -893,8 +893,7 @@ def wrapper(*args, **kwargs):
893893
try:
894894
return func(*args, **kwargs)
895895
except:
896-
if warning_msg is not None:
897-
warnings.warn(warning_msg)
896+
warnings.warn(warning_msg + "\nDetailed error log: " + traceback.format_exc())
898897

899898
return wrapper
900899

switch_model/utilities/gurobi_aug.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from pyomo.solvers.plugins.solvers.gurobi_direct import GurobiDirect
2323
from pyomo.environ import *
2424

25-
from switch_model.utilities import StepTimer
25+
from switch_model.utilities import StepTimer, catch_exceptions
2626

2727

2828
class PicklableData:
@@ -206,6 +206,7 @@ def _postsolve(self):
206206
print(f"Created warm start pickle file in {timer.step_time_as_str()}")
207207
return results
208208

209+
@catch_exceptions(warning_msg="Failed to save warm start information.")
209210
def _save_warm_start(self, save_c_v_basis):
210211
"""Create a pickle file containing the CBasis/VBasis information."""
211212
# Setup our data objects

switch_model/wecc/get_inputs/get_inputs.py

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def write_csv(data: Iterable[List], fname, headers: List[str], log=True):
6868
"switch_model.transmission.transport.build",
6969
"switch_model.transmission.transport.dispatch",
7070
"switch_model.policies.carbon_policies",
71-
"switch_model.policies.rps_unbundled",
7271
"switch_model.policies.min_per_tech", # Always include since it provides useful outputs even when unused
7372
# "switch_model.reporting.basic_exports_wecc",
7473
# Always include since by default it does nothing except output useful data
@@ -722,6 +721,7 @@ def query_db(full_config, skip_cf):
722721
order by 1, 2;
723722
"""
724723
)
724+
modules.append("switch_model.policies.rps_unbundled")
725725

726726
########################################################
727727
# BIO_SOLID SUPPLY CURVE

switch_model/wecc/get_inputs/post_process_steps/add_storage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def main(config):
123123

124124
# Get the plant costs from GSheets and append to costs
125125
storage_costs = fetch_df("costs", "costs_scenario", config)
126+
storage_costs = storage_costs[storage_costs["GENERATION_PROJECT"].isin(gen_projects["GENERATION_PROJECT"])]
126127
add_to_csv("gen_build_costs.csv", storage_costs, primary_key=["GENERATION_PROJECT", "build_year"])
127128

128129
# Create add_storage_info.csv

0 commit comments

Comments
 (0)