66Generate .dat files required by the PySP pyomo module, either for use with the
77runef or runph commands. This script serves only as a specific example in
88order 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
1010versatility will be coded in the future.
1111
1212This generator considers a two stage optimization problem, where all scenarios
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
3636ScenarioStructure.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
4747scenario_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.
5153inputs_dir = "inputs"
5254# ScenarioStructure.dat and RootNode.dat will be saved to a
5355# subdirectory in the inputs folder.
5456pysp_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" ]
5860stage_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
7071import sys , os
7172from pyomo .environ import *
7273
73- print "creating model for scenario input generation..."
74+ print ( "creating model for scenario input generation..." )
7475
7576module_list = utilities .get_module_list (args = None )
7677model = 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..." )
8182instance = model .load_inputs (inputs_dir = inputs_dir )
82- print "inputs successfully loaded..."
83-
83+ print ("inputs successfully loaded..." )
84+
85+
8486def 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 ()
0 commit comments