Skip to content

Commit 5f05840

Browse files
committed
Merge remote-tracking branch 'rael/wecc' into improv_hydro
2 parents 4614348 + 8362e91 commit 5f05840

File tree

6 files changed

+26
-39
lines changed

6 files changed

+26
-39
lines changed

switch_model/solve.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def define_arguments(argparser):
443443
argparser.add_argument("--solver-io", default=None, help="Method for Pyomo to use to communicate with solver")
444444
# note: pyomo has a --solver-options option but it is not clear
445445
# whether that does the same thing as --solver-options-string so we don't reuse the same name.
446-
argparser.add_argument("--solver-options-string", default=None,
446+
argparser.add_argument("--solver-options-string", default="",
447447
help='A quoted string of options to pass to the model solver. Each option must be of the form option=value. '
448448
'(e.g., --solver-options-string "mipgap=0.001 primalopt=\'\' advance=2 threads=1")')
449449
argparser.add_argument("--solver-method", default=None, type=int,
@@ -556,6 +556,12 @@ def define_arguments(argparser):
556556
" that all variables must be the same between the previous and current scenario."
557557
)
558558

559+
argparser.add_argument(
560+
"--gurobi-make-mps", default=False, action="store_true",
561+
help="Instead of solving just output a Gurobi .mps file that can be used for debugging numerical properties."
562+
" See https://github.com/staadecker/lp-analyzer/ for details."
563+
)
564+
559565

560566
def add_recommended_args(argparser):
561567
"""
@@ -709,25 +715,23 @@ def solve(model):
709715
# Note previously solver was saved in model however this is very memory inefficient.
710716
solver = SolverFactory(model.options.solver, solver_io=model.options.solver_io)
711717

712-
# If this option is enabled, gurobi will output an IIS to outputs\iis.ilp.
713-
if model.options.gurobi_find_iis:
714-
# Enable symbolic labels since otherwise we can't debug the .ilp file.
715-
model.options.symbolic_solver_labels = True
718+
if model.options.gurobi_find_iis and model.options.gurobi_make_mps:
719+
raise Exception("Can't use --gurobi-find-iis with --gurobi-make-mps.")
716720

717-
# If no string is passed make the string empty so we can add to it
718-
if model.options.solver_options_string is None:
719-
model.options.solver_options_string = ""
721+
if model.options.gurobi_find_iis or model.options.gurobi_make_mps:
722+
# If we are outputting a file we want to enable symbolic labels to help debugging
723+
model.options.symbolic_solver_labels = True
720724

725+
# If this option is enabled, gurobi will output an IIS to outputs\iis.ilp.
726+
if model.options.gurobi_find_iis:
721727
# Add to the solver options 'ResultFile=iis.ilp'
722728
# https://stackoverflow.com/a/51994135/5864903
723-
iis_file_path = os.path.join(model.options.outputs_dir, "iis.ilp")
724-
model.options.solver_options_string += " ResultFile={}".format(iis_file_path)
729+
model.options.solver_options_string += " ResultFile=iis.ilp"
730+
if model.options.gurobi_make_mps:
731+
# Output the input file and set time limit to zero to ensure it doesn't actually solve
732+
model.options.solver_options_string += f" ResultFile=problem.mps TimeLimit=0"
725733

726734
if model.options.threads:
727-
# If no string is passed make the string empty so we can add to it
728-
if model.options.solver_options_string is None:
729-
model.options.solver_options_string = ""
730-
731735
model.options.solver_options_string += f" Threads={model.options.threads}"
732736

733737
if model.options.solver_method is not None:

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
@@ -736,7 +736,7 @@ def query_yes_no(question, default="yes"):
736736
"(or 'y' or 'n').\n")
737737

738738

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

742742
def decorator(func):
@@ -748,8 +748,7 @@ def wrapper(*args, **kwargs):
748748
try:
749749
return func(*args, **kwargs)
750750
except:
751-
if warning_msg is not None:
752-
warnings.warn(warning_msg)
751+
warnings.warn(warning_msg + "\nDetailed error log: " + traceback.format_exc())
753752

754753
return wrapper
755754

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
@@ -747,6 +746,7 @@ def query_db(full_config, skip_cf):
747746
order by 1, 2;
748747
"""
749748
)
749+
modules.append("switch_model.policies.rps_unbundled")
750750

751751
########################################################
752752
# 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)