Skip to content

Commit dea5966

Browse files
committed
Reformat all with Black
1 parent c31b9b6 commit dea5966

File tree

13 files changed

+204
-597
lines changed

13 files changed

+204
-597
lines changed

examples/3zone_toy_stochastic_PySP/PySPInputGenerator.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Generate .dat files required by the PySP pyomo module, either for use with the
77
runef or runph commands. This script serves only as a specific example in
88
order to learn how the runef and runph commands work and does not pretend to
9-
be able to generate any scenario structure in a most flexible way. More
9+
be able to generate any scenario structure in a most flexible way. More
1010
versatility will be coded in the future.
1111
1212
This generator considers a two stage optimization problem, where all scenarios
@@ -31,13 +31,13 @@
3131
again in the other nodes' .dat files. I.e., if only fuel costs vary from
3232
node to node, then only the fuel_cost parameter must be specified in the
3333
.dat files. If a non-root node has an empty .dat file, then PySP will
34-
asume that such node has the same parameter values as its parent node.
34+
asume that such node has the same parameter values as its parent node.
3535
3636
ScenarioStructure.dat
3737
This file specifies the structure of the scenario tree. Refer to the PySP
3838
documentation for detailed definitions on each of the parameters. In this
3939
two stage example leaf nodes are named after the scenarios they belong to.
40-
The Expressions or Variables that define the stage costs are named after
40+
The Expressions or Variables that define the stage costs are named after
4141
the stage they belong to. These names must match the actual names of the
4242
Expressions and Variables in the Reference Model.
4343
@@ -47,21 +47,22 @@
4747
scenario_list should be modified to reflect those changes.
4848
4949
"""
50+
from __future__ import print_function
51+
5052
# Inputs directory relative to the location of this script.
5153
inputs_dir = "inputs"
5254
# ScenarioStructure.dat and RootNode.dat will be saved to a
5355
# subdirectory in the inputs folder.
5456
pysp_subdir = "pysp_inputs"
5557

5658
# Stage names. Can be any string and must be specified in order.
57-
stage_list = ["Investment", "Operation"]
59+
stage_list = ["Investment", "Operation"]
5860
stage_vars = {
5961
"Investment": ["BuildGen", "BuildLocalTD", "BuildTx"],
60-
"Operation": ["DispatchGen", "GenFuelUseRate"]
62+
"Operation": ["DispatchGen", "GenFuelUseRate"],
6163
}
6264
# List of scenario names
63-
scenario_list = [
64-
"LowFuelCosts", "MediumFuelCosts", "HighFuelCosts"]
65+
scenario_list = ["LowFuelCosts", "MediumFuelCosts", "HighFuelCosts"]
6566

6667
###########################################################
6768

@@ -70,17 +71,18 @@
7071
import sys, os
7172
from pyomo.environ import *
7273

73-
print "creating model for scenario input generation..."
74+
print("creating model for scenario input generation...")
7475

7576
module_list = utilities.get_module_list(args=None)
7677
model = utilities.create_model(module_list)
7778

78-
print "model successfully created..."
79+
print("model successfully created...")
7980

80-
print "loading inputs into model..."
81+
print("loading inputs into model...")
8182
instance = model.load_inputs(inputs_dir=inputs_dir)
82-
print "inputs successfully loaded..."
83-
83+
print("inputs successfully loaded...")
84+
85+
8486
def save_dat_files():
8587

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

9294
dat_file = os.path.join(inputs_dir, pysp_subdir, "RootNode.dat")
93-
print "creating and saving {}...".format(dat_file)
94-
utilities.save_inputs_as_dat(model, instance, save_path=dat_file,
95-
sorted_output=model.options.sorted_output)
96-
95+
print("creating and saving {}...".format(dat_file))
96+
utilities.save_inputs_as_dat(
97+
model, instance, save_path=dat_file, sorted_output=model.options.sorted_output
98+
)
99+
97100
#######################
98101
# ScenarioStructure.dat
99102

100103
scen_file = os.path.join(inputs_dir, pysp_subdir, "ScenarioStructure.dat")
101-
print "creating and saving {}...".format(scen_file)
104+
print("creating and saving {}...".format(scen_file))
102105

103106
with open(scen_file, "w") as f:
104107
# Data will be defined in a Node basis to avoid redundancies
105108
f.write("param ScenarioBasedData := False ;\n\n")
106-
109+
107110
f.write("set Stages :=")
108111
for st in stage_list:
109112
f.write(" {}".format(st))
@@ -116,17 +119,17 @@ def save_dat_files():
116119

117120
f.write("param NodeStage := RootNode {}\n".format(stage_list[0]))
118121
for s in scenario_list:
119-
f.write(" {scen} {st}\n".format(scen=s,st=stage_list[1]))
122+
f.write(" {scen} {st}\n".format(scen=s, st=stage_list[1]))
120123
f.write(";\n\n")
121-
124+
122125
f.write("set Children[RootNode] := ")
123126
for s in scenario_list:
124127
f.write("\n {}".format(s))
125128
f.write(";\n\n")
126-
129+
127130
f.write("param ConditionalProbability := RootNode 1.0")
128131
# All scenarios have the same probability in this example
129-
probs = [1.0/len(scenario_list)] * (len(scenario_list) - 1)
132+
probs = [1.0 / len(scenario_list)] * (len(scenario_list) - 1)
130133
# The remaining probability is lumped in the last scenario to avoid rounding issues
131134
probs.append(1.0 - sum(probs))
132135
for (s, p) in zip(scenario_list, probs):
@@ -149,14 +152,16 @@ def write_var_name(f, cname):
149152
if hasattr(instance, cname):
150153
dimen = getattr(instance, cname).index_set().dimen
151154
if dimen == 0:
152-
f.write(" {cn}\n".format(cn=cname))
155+
f.write(" {cn}\n".format(cn=cname))
153156
else:
154-
indexing = (",".join(["*"]*dimen))
157+
indexing = ",".join(["*"] * dimen)
155158
f.write(" {cn}[{dim}]\n".format(cn=cname, dim=indexing))
156159
else:
157160
raise ValueError(
158-
"Variable '{}' is not a component of the model. Did you make a typo?".
159-
format(cname))
161+
"Variable '{}' is not a component of the model. Did you make a typo?".format(
162+
cname
163+
)
164+
)
160165

161166
for st in stage_list:
162167
f.write("set StageVariables[{}] := \n".format(st))
@@ -170,8 +175,9 @@ def write_var_name(f, cname):
170175
f.write(" Operation OperationCost\n")
171176
f.write(";")
172177

178+
173179
####################
174-
175-
if __name__ == '__main__':
180+
181+
if __name__ == "__main__":
176182
# If the script is executed on the command line, then the .dat files are created.
177183
save_dat_files()

examples/3zone_toy_stochastic_PySP/ReferenceModel.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
per scenario, which is incongruent.
2222
2323
"""
24+
from __future__ import print_function
2425

2526
inputs_dir = "inputs"
2627

@@ -32,37 +33,42 @@
3233
import sys, os
3334
from pyomo.environ import *
3435

35-
print "loading model..."
36+
print("loading model...")
3637

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

4142
module_list = utilities.get_module_list(args=None)
4243
model = utilities.create_model(module_list, args=[])
4344

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

5152
# The following two functions are defined explicitely, because since they
5253
# are nested inside another function in the financials module, they can't
5354
# be called from this script.
5455

56+
5557
def calc_tp_costs_in_period(m, t):
56-
return sum(
57-
getattr(m, tp_cost)[t] * m.tp_weight_in_year[t]
58-
for tp_cost in m.Cost_Components_Per_TP)
58+
return sum(
59+
getattr(m, tp_cost)[t] * m.tp_weight_in_year[t]
60+
for tp_cost in m.Cost_Components_Per_TP
61+
)
62+
63+
5964
def calc_annual_costs_in_period(m, p):
60-
return sum(
61-
getattr(m, annual_cost)[p]
62-
for annual_cost in m.Cost_Components_Per_Period)
65+
return sum(
66+
getattr(m, annual_cost)[p] for annual_cost in m.Cost_Components_Per_Period
67+
)
6368

64-
# In the current version of Switch-Pyomo, all annual costs are defined
65-
# by First Stage decision variables, such as fixed O&M and capital
69+
70+
# In the current version of Switch, all annual costs are defined
71+
# by First Stage decision variables, such as fixed O&M and capital
6672
# costs, which are caused by the BuildProj, BuildTrans and BuildLocalTD
6773
# variables, all of which are considered as first stage decisions in this
6874
# two-stage example.
@@ -72,14 +78,19 @@ def calc_annual_costs_in_period(m, p):
7278
# decisions in this example.
7379
# Further comments on this are written in the Readme file.
7480

75-
model.InvestmentCost = Expression(rule=lambda m: sum(
76-
calc_annual_costs_in_period(m, p) * m.bring_annual_costs_to_base_year[p]
77-
for p in m.PERIODS))
78-
79-
model.OperationCost = Expression(rule=lambda m:
80-
sum(
81-
sum(calc_tp_costs_in_period(m, t) for t in m.TPS_IN_PERIOD[p]
82-
) * m.bring_annual_costs_to_base_year[p]
83-
for p in m.PERIODS))
84-
85-
print "model successfully loaded..."
81+
model.InvestmentCost = Expression(
82+
rule=lambda m: sum(
83+
calc_annual_costs_in_period(m, p) * m.bring_annual_costs_to_base_year[p]
84+
for p in m.PERIODS
85+
)
86+
)
87+
88+
model.OperationCost = Expression(
89+
rule=lambda m: sum(
90+
sum(calc_tp_costs_in_period(m, t) for t in m.TPS_IN_PERIOD[p])
91+
* m.bring_annual_costs_to_base_year[p]
92+
for p in m.PERIODS
93+
)
94+
)
95+
96+
print("model successfully loaded...")

switch_model/balancing/demand_response/simple.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,16 @@ def define_components(mod):
5858
mod.TIMEPOINTS,
5959
default=0.0,
6060
within=NonNegativeReals,
61-
<<<<<<< HEAD
61+
input_file="dr_data.csv",
6262
validate=lambda m, value, z, t: value <= m.zone_demand_mw[z, t],
6363
)
6464
mod.dr_shift_up_limit = Param(
65-
mod.LOAD_ZONES, mod.TIMEPOINTS, default=float("inf"), within=NonNegativeReals
66-
)
67-
=======
68-
input_file="dr_data.csv",
69-
validate=lambda m, value, z, t: value <= m.zone_demand_mw[z, t])
70-
mod.dr_shift_up_limit = Param(
71-
mod.LOAD_ZONES, mod.TIMEPOINTS,
72-
default= float('inf'),
65+
mod.LOAD_ZONES,
66+
mod.TIMEPOINTS,
67+
default=float("inf"),
7368
input_file="dr_data.csv",
74-
within=NonNegativeReals)
75-
>>>>>>> 941c8e2e (Further improvements)
69+
within=NonNegativeReals,
70+
)
7671
mod.ShiftDemand = Var(
7772
mod.LOAD_ZONES,
7873
mod.TIMEPOINTS,
@@ -92,26 +87,4 @@ def define_components(mod):
9287
try:
9388
mod.Distributed_Power_Withdrawals.append("ShiftDemand")
9489
except AttributeError:
95-
<<<<<<< HEAD
9690
mod.Zone_Power_Withdrawals.append("ShiftDemand")
97-
98-
99-
def load_inputs(mod, switch_data, inputs_dir):
100-
"""
101-
102-
Import demand response-specific data from an input directory.
103-
104-
dr_data.csv
105-
LOAD_ZONE, TIMEPOINT, dr_shift_down_limit, dr_shift_up_limit
106-
107-
"""
108-
109-
switch_data.load_aug(
110-
optional=True,
111-
filename=os.path.join(inputs_dir, "dr_data.csv"),
112-
autoselect=True,
113-
param=(mod.dr_shift_down_limit, mod.dr_shift_up_limit),
114-
)
115-
=======
116-
mod.Zone_Power_Withdrawals.append('ShiftDemand')
117-
>>>>>>> 941c8e2e (Further improvements)

switch_model/balancing/electric_vehicles/simple.py

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,28 @@ def define_components(mod):
7373
"""
7474

7575
mod.ev_charge_limit_mw = Param(
76-
<<<<<<< HEAD
77-
mod.LOAD_ZONES, mod.TIMEPOINTS, default=float("inf"), within=NonNegativeReals
78-
)
79-
80-
mod.ev_cumulative_charge_upper_mwh = Param(
81-
mod.LOAD_ZONES, mod.TIMEPOINTS, default=0.0, within=NonNegativeReals
82-
)
83-
84-
mod.ev_cumulative_charge_lower_mwh = Param(
85-
mod.LOAD_ZONES, mod.TIMEPOINTS, default=0.0, within=NonNegativeReals
86-
)
87-
=======
88-
mod.LOAD_ZONES, mod.TIMEPOINTS,
89-
default = float('inf'),
76+
mod.LOAD_ZONES,
77+
mod.TIMEPOINTS,
78+
default=float("inf"),
9079
input_file="ev_limits.csv",
91-
within=NonNegativeReals)
80+
within=NonNegativeReals,
81+
)
9282

9383
mod.ev_cumulative_charge_upper_mwh = Param(
94-
mod.LOAD_ZONES, mod.TIMEPOINTS,
95-
default = 0.0,
84+
mod.LOAD_ZONES,
85+
mod.TIMEPOINTS,
86+
default=0.0,
9687
input_file="ev_limits.csv",
97-
within=NonNegativeReals)
88+
within=NonNegativeReals,
89+
)
9890

9991
mod.ev_cumulative_charge_lower_mwh = Param(
100-
mod.LOAD_ZONES, mod.TIMEPOINTS,
101-
default = 0.0,
92+
mod.LOAD_ZONES,
93+
mod.TIMEPOINTS,
94+
default=0.0,
10295
input_file="ev_limits.csv",
103-
within=NonNegativeReals)
104-
>>>>>>> 941c8e2e (Further improvements)
96+
within=NonNegativeReals,
97+
)
10598

10699
mod.EVCharge = Var(
107100
mod.LOAD_ZONES,
@@ -137,32 +130,4 @@ def define_components(mod):
137130
if "Distributed_Power_Injections" in dir(mod):
138131
mod.Distributed_Power_Withdrawals.append("EVCharge")
139132
else:
140-
<<<<<<< HEAD
141133
mod.Zone_Power_Withdrawals.append("EVCharge")
142-
143-
144-
def load_inputs(mod, switch_data, inputs_dir):
145-
"""
146-
147-
Import virtual batteries specific location and power limits
148-
from an input directory.
149-
150-
ev_limits.tab
151-
LOAD_ZONES, TIMEPOINT, ev_cumulative_charge_upper_mwh,
152-
ev_cumulative_charge_upper_mwh, ev_charge_limit_mw
153-
154-
"""
155-
156-
switch_data.load_aug(
157-
optional=True,
158-
filename=os.path.join(inputs_dir, "ev_limits.tab"),
159-
autoselect=True,
160-
param=(
161-
mod.ev_cumulative_charge_lower_mwh,
162-
mod.ev_cumulative_charge_upper_mwh,
163-
mod.ev_charge_limit_mw,
164-
),
165-
)
166-
=======
167-
mod.Zone_Power_Withdrawals.append('EVCharge')
168-
>>>>>>> 941c8e2e (Further improvements)

0 commit comments

Comments
 (0)