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 c31b9b6 commit dea5966
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 597 deletions.
62 changes: 34 additions & 28 deletions examples/3zone_toy_stochastic_PySP/PySPInputGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Generate .dat files required by the PySP pyomo module, either for use with the
runef or runph commands. This script serves only as a specific example in
order to learn how the runef and runph commands work and does not pretend to
be able to generate any scenario structure in a most flexible way. More
be able to generate any scenario structure in a most flexible way. More
versatility will be coded in the future.
This generator considers a two stage optimization problem, where all scenarios
Expand All @@ -31,13 +31,13 @@
again in the other nodes' .dat files. I.e., if only fuel costs vary from
node to node, then only the fuel_cost parameter must be specified in the
.dat files. If a non-root node has an empty .dat file, then PySP will
asume that such node has the same parameter values as its parent node.
asume that such node has the same parameter values as its parent node.
ScenarioStructure.dat
This file specifies the structure of the scenario tree. Refer to the PySP
documentation for detailed definitions on each of the parameters. In this
two stage example leaf nodes are named after the scenarios they belong to.
The Expressions or Variables that define the stage costs are named after
The Expressions or Variables that define the stage costs are named after
the stage they belong to. These names must match the actual names of the
Expressions and Variables in the Reference Model.
Expand All @@ -47,21 +47,22 @@
scenario_list should be modified to reflect those changes.
"""
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
# subdirectory in the inputs folder.
pysp_subdir = "pysp_inputs"

# Stage names. Can be any string and must be specified in order.
stage_list = ["Investment", "Operation"]
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 @@ -70,17 +71,18 @@
import sys, os
from pyomo.environ import *

print "creating model for scenario input generation..."
print("creating model for scenario input generation...")

module_list = utilities.get_module_list(args=None)
model = utilities.create_model(module_list)

print "model successfully created..."
print("model successfully created...")

print "loading inputs into model..."
print("loading inputs into model...")
instance = model.load_inputs(inputs_dir=inputs_dir)
print "inputs successfully loaded..."

print("inputs successfully loaded...")


def save_dat_files():

if not os.path.exists(os.path.join(inputs_dir, pysp_subdir)):
Expand All @@ -90,20 +92,21 @@ def save_dat_files():
# RootNode.dat

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)

print("creating and saving {}...".format(dat_file))
utilities.save_inputs_as_dat(
model, instance, save_path=dat_file, sorted_output=model.options.sorted_output
)

#######################
# ScenarioStructure.dat

scen_file = os.path.join(inputs_dir, pysp_subdir, "ScenarioStructure.dat")
print "creating and saving {}...".format(scen_file)
print("creating and saving {}...".format(scen_file))

with open(scen_file, "w") as f:
# Data will be defined in a Node basis to avoid redundancies
f.write("param ScenarioBasedData := False ;\n\n")

f.write("set Stages :=")
for st in stage_list:
f.write(" {}".format(st))
Expand All @@ -116,17 +119,17 @@ 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] := ")
for s in scenario_list:
f.write("\n {}".format(s))
f.write(";\n\n")

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 @@ -149,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 @@ -170,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()
59 changes: 35 additions & 24 deletions examples/3zone_toy_stochastic_PySP/ReferenceModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
per scenario, which is incongruent.
"""
from __future__ import print_function

inputs_dir = "inputs"

Expand All @@ -32,37 +33,42 @@
import sys, os
from pyomo.environ import *

print "loading model..."
print("loading model...")

# 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=[])

# The following code augments the model object with Expressions for the
# Stage costs, which both runef and runph scripts need in order to build
# The following code augments the model object with Expressions for the
# Stage costs, which both runef and runph scripts need in order to build
# the stochastic objective function. In this particular example, only
# two stages are considered: Investment and Operation. These Expression
# names must match exactly the StageCostVariable parameter defined for
# each Stage in the ScenarioStructure.dat file.
# each Stage in the ScenarioStructure.dat file.

# The following two functions are defined explicitely, because since they
# 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-Pyomo, all annual costs are defined
# by First Stage decision variables, such as fixed O&M and capital

# In the current version of Switch, all annual costs are defined
# by First Stage decision variables, such as fixed O&M and capital
# costs, which are caused by the BuildProj, BuildTrans and BuildLocalTD
# variables, all of which are considered as first stage decisions in this
# two-stage example.
Expand All @@ -72,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))

print "model successfully loaded..."
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...")
39 changes: 6 additions & 33 deletions switch_model/balancing/demand_response/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,16 @@ def define_components(mod):
mod.TIMEPOINTS,
default=0.0,
within=NonNegativeReals,
<<<<<<< HEAD
input_file="dr_data.csv",
validate=lambda m, value, z, t: value <= m.zone_demand_mw[z, t],
)
mod.dr_shift_up_limit = Param(
mod.LOAD_ZONES, mod.TIMEPOINTS, default=float("inf"), within=NonNegativeReals
)
=======
input_file="dr_data.csv",
validate=lambda m, value, z, t: value <= m.zone_demand_mw[z, t])
mod.dr_shift_up_limit = Param(
mod.LOAD_ZONES, mod.TIMEPOINTS,
default= float('inf'),
mod.LOAD_ZONES,
mod.TIMEPOINTS,
default=float("inf"),
input_file="dr_data.csv",
within=NonNegativeReals)
>>>>>>> 941c8e2e (Further improvements)
within=NonNegativeReals,
)
mod.ShiftDemand = Var(
mod.LOAD_ZONES,
mod.TIMEPOINTS,
Expand All @@ -92,26 +87,4 @@ def define_components(mod):
try:
mod.Distributed_Power_Withdrawals.append("ShiftDemand")
except AttributeError:
<<<<<<< HEAD
mod.Zone_Power_Withdrawals.append("ShiftDemand")


def load_inputs(mod, switch_data, inputs_dir):
"""
Import demand response-specific data from an input directory.
dr_data.csv
LOAD_ZONE, TIMEPOINT, dr_shift_down_limit, dr_shift_up_limit
"""

switch_data.load_aug(
optional=True,
filename=os.path.join(inputs_dir, "dr_data.csv"),
autoselect=True,
param=(mod.dr_shift_down_limit, mod.dr_shift_up_limit),
)
=======
mod.Zone_Power_Withdrawals.append('ShiftDemand')
>>>>>>> 941c8e2e (Further improvements)
65 changes: 15 additions & 50 deletions switch_model/balancing/electric_vehicles/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,28 @@ def define_components(mod):
"""

mod.ev_charge_limit_mw = Param(
<<<<<<< HEAD
mod.LOAD_ZONES, mod.TIMEPOINTS, default=float("inf"), within=NonNegativeReals
)

mod.ev_cumulative_charge_upper_mwh = Param(
mod.LOAD_ZONES, mod.TIMEPOINTS, default=0.0, within=NonNegativeReals
)

mod.ev_cumulative_charge_lower_mwh = Param(
mod.LOAD_ZONES, mod.TIMEPOINTS, default=0.0, within=NonNegativeReals
)
=======
mod.LOAD_ZONES, mod.TIMEPOINTS,
default = float('inf'),
mod.LOAD_ZONES,
mod.TIMEPOINTS,
default=float("inf"),
input_file="ev_limits.csv",
within=NonNegativeReals)
within=NonNegativeReals,
)

mod.ev_cumulative_charge_upper_mwh = Param(
mod.LOAD_ZONES, mod.TIMEPOINTS,
default = 0.0,
mod.LOAD_ZONES,
mod.TIMEPOINTS,
default=0.0,
input_file="ev_limits.csv",
within=NonNegativeReals)
within=NonNegativeReals,
)

mod.ev_cumulative_charge_lower_mwh = Param(
mod.LOAD_ZONES, mod.TIMEPOINTS,
default = 0.0,
mod.LOAD_ZONES,
mod.TIMEPOINTS,
default=0.0,
input_file="ev_limits.csv",
within=NonNegativeReals)
>>>>>>> 941c8e2e (Further improvements)
within=NonNegativeReals,
)

mod.EVCharge = Var(
mod.LOAD_ZONES,
Expand Down Expand Up @@ -137,32 +130,4 @@ def define_components(mod):
if "Distributed_Power_Injections" in dir(mod):
mod.Distributed_Power_Withdrawals.append("EVCharge")
else:
<<<<<<< HEAD
mod.Zone_Power_Withdrawals.append("EVCharge")


def load_inputs(mod, switch_data, inputs_dir):
"""
Import virtual batteries specific location and power limits
from an input directory.
ev_limits.tab
LOAD_ZONES, TIMEPOINT, ev_cumulative_charge_upper_mwh,
ev_cumulative_charge_upper_mwh, ev_charge_limit_mw
"""

switch_data.load_aug(
optional=True,
filename=os.path.join(inputs_dir, "ev_limits.tab"),
autoselect=True,
param=(
mod.ev_cumulative_charge_lower_mwh,
mod.ev_cumulative_charge_upper_mwh,
mod.ev_charge_limit_mw,
),
)
=======
mod.Zone_Power_Withdrawals.append('EVCharge')
>>>>>>> 941c8e2e (Further improvements)
Loading

0 comments on commit dea5966

Please sign in to comment.