Skip to content

Commit a543594

Browse files
committed
Merge remote-tracking branch 'rael/wecc' into improve_load_aug
2 parents 31b8a8f + 8362e91 commit a543594

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
@@ -441,7 +441,7 @@ def define_arguments(argparser):
441441
argparser.add_argument("--solver-io", default=None, help="Method for Pyomo to use to communicate with solver")
442442
# note: pyomo has a --solver-options option but it is not clear
443443
# whether that does the same thing as --solver-options-string so we don't reuse the same name.
444-
argparser.add_argument("--solver-options-string", default=None,
444+
argparser.add_argument("--solver-options-string", default="",
445445
help='A quoted string of options to pass to the model solver. Each option must be of the form option=value. '
446446
'(e.g., --solver-options-string "mipgap=0.001 primalopt=\'\' advance=2 threads=1")')
447447
argparser.add_argument("--solver-method", default=None, type=int,
@@ -554,6 +554,12 @@ def define_arguments(argparser):
554554
" that all variables must be the same between the previous and current scenario."
555555
)
556556

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

558564
def add_recommended_args(argparser):
559565
"""
@@ -707,25 +713,23 @@ def solve(model):
707713
# Note previously solver was saved in model however this is very memory inefficient.
708714
solver = SolverFactory(model.options.solver, solver_io=model.options.solver_io)
709715

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

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

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

724732
if model.options.threads:
725-
# If no string is passed make the string empty so we can add to it
726-
if model.options.solver_options_string is None:
727-
model.options.solver_options_string = ""
728-
729733
model.options.solver_options_string += f" Threads={model.options.threads}"
730734

731735
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
@@ -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)