Skip to content

Commit

Permalink
Reformat all with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
staadecker committed Jan 28, 2023
1 parent 2eae434 commit 4f98c50
Show file tree
Hide file tree
Showing 158 changed files with 11,075 additions and 6,894 deletions.
29 changes: 17 additions & 12 deletions examples/3zone_toy_stochastic_PySP/PySPInputGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"""
from __future__ import print_function

# Inputs directory relative to the location of this script.
inputs_dir = "inputs"
# ScenarioStructure.dat and RootNode.dat will be saved to a
Expand All @@ -58,11 +59,10 @@
stage_list = ["Investment", "Operation"]
stage_vars = {
"Investment": ["BuildGen", "BuildLocalTD", "BuildTx"],
"Operation": ["DispatchGen", "GenFuelUseRate"]
"Operation": ["DispatchGen", "GenFuelUseRate"],
}
# List of scenario names
scenario_list = [
"LowFuelCosts", "MediumFuelCosts", "HighFuelCosts"]
scenario_list = ["LowFuelCosts", "MediumFuelCosts", "HighFuelCosts"]

###########################################################

Expand All @@ -82,6 +82,7 @@
instance = model.load_inputs(inputs_dir=inputs_dir)
print("inputs successfully loaded...")


def save_dat_files():

if not os.path.exists(os.path.join(inputs_dir, pysp_subdir)):
Expand All @@ -92,8 +93,9 @@ def save_dat_files():

dat_file = os.path.join(inputs_dir, pysp_subdir, "RootNode.dat")
print("creating and saving {}...".format(dat_file))
utilities.save_inputs_as_dat(model, instance, save_path=dat_file,
sorted_output=model.options.sorted_output)
utilities.save_inputs_as_dat(
model, instance, save_path=dat_file, sorted_output=model.options.sorted_output
)

#######################
# ScenarioStructure.dat
Expand All @@ -117,7 +119,7 @@ def save_dat_files():

f.write("param NodeStage := RootNode {}\n".format(stage_list[0]))
for s in scenario_list:
f.write(" {scen} {st}\n".format(scen=s,st=stage_list[1]))
f.write(" {scen} {st}\n".format(scen=s, st=stage_list[1]))
f.write(";\n\n")

f.write("set Children[RootNode] := ")
Expand All @@ -127,7 +129,7 @@ def save_dat_files():

f.write("param ConditionalProbability := RootNode 1.0")
# All scenarios have the same probability in this example
probs = [1.0/len(scenario_list)] * (len(scenario_list) - 1)
probs = [1.0 / len(scenario_list)] * (len(scenario_list) - 1)
# The remaining probability is lumped in the last scenario to avoid rounding issues
probs.append(1.0 - sum(probs))
for (s, p) in zip(scenario_list, probs):
Expand All @@ -150,14 +152,16 @@ def write_var_name(f, cname):
if hasattr(instance, cname):
dimen = getattr(instance, cname).index_set().dimen
if dimen == 0:
f.write(" {cn}\n".format(cn=cname))
f.write(" {cn}\n".format(cn=cname))
else:
indexing = (",".join(["*"]*dimen))
indexing = ",".join(["*"] * dimen)
f.write(" {cn}[{dim}]\n".format(cn=cname, dim=indexing))
else:
raise ValueError(
"Variable '{}' is not a component of the model. Did you make a typo?".
format(cname))
"Variable '{}' is not a component of the model. Did you make a typo?".format(
cname
)
)

for st in stage_list:
f.write("set StageVariables[{}] := \n".format(st))
Expand All @@ -171,8 +175,9 @@ def write_var_name(f, cname):
f.write(" Operation OperationCost\n")
f.write(";")


####################

if __name__ == '__main__':
if __name__ == "__main__":
# If the script is executed on the command line, then the .dat files are created.
save_dat_files()
42 changes: 26 additions & 16 deletions examples/3zone_toy_stochastic_PySP/ReferenceModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

# Ideally, we would use the main codebase to generate the model, but the
# mandatory switch argument parser is interferring with pysp's command line tools
#model = switch_model.solve.main(return_model=True)
# model = switch_model.solve.main(return_model=True)

module_list = utilities.get_module_list(args=None)
model = utilities.create_model(module_list, args=[])
Expand All @@ -53,14 +53,19 @@
# are nested inside another function in the financials module, they can't
# be called from this script.


def calc_tp_costs_in_period(m, t):
return sum(
getattr(m, tp_cost)[t] * m.tp_weight_in_year[t]
for tp_cost in m.Cost_Components_Per_TP)
return sum(
getattr(m, tp_cost)[t] * m.tp_weight_in_year[t]
for tp_cost in m.Cost_Components_Per_TP
)


def calc_annual_costs_in_period(m, p):
return sum(
getattr(m, annual_cost)[p]
for annual_cost in m.Cost_Components_Per_Period)
return sum(
getattr(m, annual_cost)[p] for annual_cost in m.Cost_Components_Per_Period
)


# In the current version of Switch, all annual costs are defined
# by First Stage decision variables, such as fixed O&M and capital
Expand All @@ -73,14 +78,19 @@ def calc_annual_costs_in_period(m, p):
# decisions in this example.
# Further comments on this are written in the Readme file.

model.InvestmentCost = Expression(rule=lambda m: sum(
calc_annual_costs_in_period(m, p) * m.bring_annual_costs_to_base_year[p]
for p in m.PERIODS))

model.OperationCost = Expression(rule=lambda m:
sum(
sum(calc_tp_costs_in_period(m, t) for t in m.TPS_IN_PERIOD[p]
) * m.bring_annual_costs_to_base_year[p]
for p in m.PERIODS))
model.InvestmentCost = Expression(
rule=lambda m: sum(
calc_annual_costs_in_period(m, p) * m.bring_annual_costs_to_base_year[p]
for p in m.PERIODS
)
)

model.OperationCost = Expression(
rule=lambda m: sum(
sum(calc_tp_costs_in_period(m, t) for t in m.TPS_IN_PERIOD[p])
* m.bring_annual_costs_to_base_year[p]
for p in m.PERIODS
)
)

print("model successfully loaded...")
11 changes: 8 additions & 3 deletions examples/3zone_toy_stochastic_PySP/pha_bounds_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
# Use this by adding terms like the following to the runph command:
# --linearize-nonbinary-penalty-terms=5 --bounds-cfgfile=pha_bounds_cfg.py


def pysp_boundsetter_callback(self, scenario_tree, scenario):
m = scenario._instance # see pyomo/pysp/scenariotree/tree_structure.py
m = scenario._instance # see pyomo/pysp/scenariotree/tree_structure.py

# BuildLocalTD
for p in m.PERIODS:
for lz in m.LOAD_ZONES:
m.BuildLocalTD[lz, p].setub(2 * m.zone_expected_coincident_peak_demand[lz, p])
m.BuildLocalTD[lz, p].setub(
2 * m.zone_expected_coincident_peak_demand[lz, p]
)

# Estimate an upper bound of system peak demand for limiting generation unit
# & transmission line builds
system_wide_peak = {}
for p in m.PERIODS:
system_wide_peak[p] = sum(
m.zone_expected_coincident_peak_demand[lz, p] for lz in m.LOAD_ZONES)
m.zone_expected_coincident_peak_demand[lz, p] for lz in m.LOAD_ZONES
)

# BuildGen
for g, bld_yr in m.NEW_GEN_BLD_YRS:
Expand All @@ -27,6 +31,7 @@ def pysp_boundsetter_callback(self, scenario_tree, scenario):
for tx, bld_yr in m.TRANS_BLD_YRS:
m.BuildTx[tx, bld_yr].setub(5 * system_wide_peak[bld_yr])


# For some reason runph looks for pysp_boundsetter_callback when run in
# single-thread mode and ph_boundsetter_callback when called from mpirun with
# remote execution via pyro. so we map both names to the same function.
Expand Down
28 changes: 17 additions & 11 deletions examples/3zone_toy_stochastic_PySP/rhosetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
from pyomo.environ import Objective

try:
from pyomo.repn import generate_standard_repn # Pyomo >=5.6
from pyomo.repn import generate_standard_repn # Pyomo >=5.6

newPyomo = True
except ImportError:
from pyomo.repn import generate_canonical_repn # Pyomo <=5.6
from pyomo.repn import generate_canonical_repn # Pyomo <=5.6

newPyomo = False


def ph_rhosetter_callback(ph, scenario_tree, scenario):
# Derive coefficients from active objective
cost_expr = next(scenario._instance.component_data_objects(
Objective, active=True, descend_into=True
))
# Derive coefficients from active objective
cost_expr = next(
scenario._instance.component_data_objects(
Objective, active=True, descend_into=True
)
)
set_rho_values(ph, scenario_tree, scenario, cost_expr)


Expand Down Expand Up @@ -56,8 +61,7 @@ def set_rho_values(ph, scenario_tree, scenario, cost_expr):

cost_coefficients = {}
var_names = {}
for (variable, coef) in \
zip(standard_repn.linear_vars, standard_repn.linear_coefs):
for (variable, coef) in zip(standard_repn.linear_vars, standard_repn.linear_coefs):
variable_id = symbol_map.getSymbol(variable)
cost_coefficients[variable_id] = coef
var_names[variable_id] = variable.name
Expand All @@ -71,11 +75,13 @@ def set_rho_values(ph, scenario_tree, scenario, cost_expr):
tree_node,
scenario,
variable_id,
cost_coefficients[variable_id] * rho_coefficient)
cost_coefficients[variable_id] * rho_coefficient,
)
set_rho = True
break
if not set_rho:
print(
"Warning! Could not find tree node for variable {}; rho not set."
.format(var_names[variable_id])
"Warning! Could not find tree node for variable {}; rho not set.".format(
var_names[variable_id]
)
)
1 change: 1 addition & 0 deletions examples/3zone_toy_stochastic_PySP/rhosetter_FS_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# The rhosetter module should be in the same directory as this file.
from rhosetter import set_rho_values


def ph_rhosetter_callback(ph, scenario_tree, scenario):
# This component name must match the expression used for first stage
# costs defined in the ReferenceModel.
Expand Down
6 changes: 2 additions & 4 deletions examples/custom_extension/sunk_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@


def define_components(mod):
mod.administration_fees = Param(
mod.PERIODS,
initialize=lambda m, p: 1000000)
mod.Cost_Components_Per_Period.append('administration_fees')
mod.administration_fees = Param(mod.PERIODS, initialize=lambda m, p: 1000000)
mod.Cost_Components_Per_Period.append("administration_fees")
23 changes: 14 additions & 9 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class TestLoader(unittest.TestLoader):
# effects when imported.
def discover(self, start_dir, pattern, top_level_dir):
test_suite = unittest.TestSuite()
for subdir in ('switch_model', 'tests'):
for subdir in ("switch_model", "tests"):
test_suite.addTests(
super(TestLoader, self).discover(
os.path.join(top_level_dir, subdir),
pattern, top_level_dir))
os.path.join(top_level_dir, subdir), pattern, top_level_dir
)
)
return test_suite

# The unittest module does not have built-in support for finding
Expand All @@ -37,7 +38,7 @@ def loadTestsFromModule(self, module, **kwargs):
if not docstring:
# Work around a misfeature whereby doctest complains if a
# module contains no docstrings.
module.__doc__ = 'Placeholder docstring'
module.__doc__ = "Placeholder docstring"
test_suite.addTests(doctest.DocTestSuite(module))
if not docstring:
# Restore the original, in case this matters.
Expand All @@ -48,12 +49,16 @@ def loadTestsFromModule(self, module, **kwargs):
def main():
script_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
# print('old argv: {}'.format(sys.argv))
argv = [sys.argv[0],
'discover',
'--top-level-directory', script_dir,
'--pattern', '*.py'] + sys.argv[1:]
argv = [
sys.argv[0],
"discover",
"--top-level-directory",
script_dir,
"--pattern",
"*.py",
] + sys.argv[1:]
unittest.TestProgram(testLoader=TestLoader(), argv=argv, module=None)


if __name__ == '__main__':
if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def read(*rnames):
"planning",
"optimization",
],
python_requires='>=3.7',
python_requires=">=3.7",
install_requires=[
"Pyomo>=6.1,<6.4.1", # 6.1 Has all the bug fixes we need
"pint", # needed by Pyomo when we run our tests, but not included
Expand Down Expand Up @@ -96,7 +96,7 @@ def read(*rnames):
"dev": ["ipdb", "black", "psycopg2-binary"],
# On Windows at least, installing these will only work via conda.
# Run conda install -c conda-forge geopandas shapely [... all the other packages]
"maps_INSTALL_WITH_CONDA": ["geopandas", "shapely", "cartopy", "plotnine"]
"maps_INSTALL_WITH_CONDA": ["geopandas", "shapely", "cartopy", "plotnine"],
},
entry_points={
"console_scripts": [
Expand Down
14 changes: 8 additions & 6 deletions switch_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
transmission, local_td, reserves, etc.
"""
from .version import __version__

core_modules = [
'switch_model.timescales',
'switch_model.financials',
'switch_model.balancing.load_zones',
'switch_model.energy_sources.properties',
'switch_model.generators.core',
'switch_model.reporting']
"switch_model.timescales",
"switch_model.financials",
"switch_model.balancing.load_zones",
"switch_model.energy_sources.properties",
"switch_model.generators.core",
"switch_model.reporting",
]
Loading

0 comments on commit 4f98c50

Please sign in to comment.