/
compile_scenario_tree.py
336 lines (293 loc) · 13.1 KB
/
compile_scenario_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# _________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2014 Sandia Corporation.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# This software is distributed under the BSD License.
# _________________________________________________________________________
__all__ = ('compile_scenario_tree',)
import os
import sys
import time
import argparse
from pyutilib.misc.config import (ConfigValue,
ConfigBlock)
from pyutilib.pyro import shutdown_pyro_components
from pyomo.util import pyomo_command
from pyomo.repn.beta.matrix import compile_block_linear_constraints
from pyomo.pysp.util.config import (safe_register_common_option,
safe_register_unique_option,
_domain_must_be_str)
from pyomo.pysp.util.misc import launch_command
from pyomo.pysp.scenariotree.scenariotreemanager import (ScenarioTreeManagerSerial,
ScenarioTreeManagerSPPyro)
from pyomo.pysp.scenariotree.scenariotreeserver import SPPyroScenarioTreeServer
from pyomo.pysp.scenariotree.scenariotreeworkerbasic import ScenarioTreeWorkerBasic
from pyomo.pysp.scenariotree.scenariotreeserverutils import InvocationType
#
# Compile all scenario models on this scenario tree manager. If output_directory
# is not none, the compiled scenario models will be pickled into individual
# files in that directory. If compiled_reference_model_filename is not none,
# a new reference model will be created that references the pickled scenario
# models.
#
# NOTE: It is assumed the models are "clean", with the exception of
# whatever objects are added by the ScenarioTree linking
# processes. That is, these instances have not yet been handed
# over to something like PH (e.g., annotated with PH specific
# parameters, variables, and other objects).
#
def compile_scenario_tree(scenario_tree_manager,
output_directory=None,
compiled_reference_model_filename=None,
verbose=False,
**kwds):
import pyomo.environ
import pyomo.solvers.plugins.smanager.phpyro
import pyomo.pysp.scenariotree.scenariotreeserverutils as \
scenariotreeserverutils
from pyomo.pysp.scenariotree.scenariotreemanager import \
(ScenarioTreeManagerSerial,
ScenarioTreeManagerSPPyro)
if output_directory is not None:
output_directory = os.path.abspath(output_directory)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
scenario_tree = scenario_tree_manager._scenario_tree
if isinstance(scenario_tree_manager,
ScenarioTreeManagerSPPyro):
ahs = []
for scenario in scenario_tree._scenarios:
output_filename = None
if output_directory is not None:
output_filename = os.path.join(output_directory,
scenario._name+".compiled.pickle")
function_kwds = {'output_filename': output_filename,
'verbose': verbose}
function_kwds.update(kwds)
scenario_tree_manager.complete_actions(
scenario_tree_manager.\
transmit_external_function_invocation_to_worker(
scenario_tree_manager.get_worker_for_scenario(scenario._name),
"pyomo.pysp.util.compile_scenario_tree",
"EXTERNAL_compile_scenario",
invocation_type=InvocationType.PerScenarioInvocation,
function_kwds=function_kwds,
return_action_handle=True))
else:
for scenario in scenario_tree._scenarios:
output_filename = None
if output_directory is not None:
output_filename = os.path.join(output_directory,
scenario._name+".compiled.pickle")
EXTERNAL_compile_scenario(scenario_tree_manager,
scenario_tree,
scenario,
output_filename=output_filename,
verbose=verbose,
**kwds)
if compiled_reference_model_filename is not None:
assert output_directory is not None
filename = os.path.join(output_directory,
compiled_reference_model_filename)
if verbose:
print("Saving reference model for compiled scenario tree "
"to file: "+str(filename))
with open(filename, 'wb') as f:
f.write("from six.moves import cPickle\n")
f.write("def pysp_instance_creation_callback(scenario_name, node_names):\n")
f.write(" with open(scenario_name+'.compiled.pickle', 'rb') as f:\n")
f.write(" return cPickle.load(f)\n")
#
# This function can be transmitted to scenario tree servers.
# NOTE: It is assumed the models are "clean", with the exception of
# whatever objects are added by the ScenarioTree linking
# processes. That is, these instances have not yet been handed
# over to something like PH (e.g., annotated with PH specific
# parameters, variables, and other objects).
#
def EXTERNAL_compile_scenario(scenario_tree_manager,
scenario_tree,
scenario,
output_times=False,
**kwds):
start_time = time.time()
assert scenario._instance is not None
scenario_instance = scenario._instance
#
# Temporarily PySP objects added by the scenario tree
# linking process, just in case this model is pickled.
# is pickled.
#
scenario_instance_cost_expression = \
scenario._instance_cost_expression
assert scenario_instance_cost_expression.name in \
("_PySP_UserCostExpression", "_PySP_CostExpression")
scenario_instance.del_component(scenario_instance_cost_expression)
scenario_instance_objective = \
scenario._instance_objective
assert scenario_instance_objective.name in \
("_PySP_UserCostObjective", "_PySP_CostObjective")
scenario_instance.del_component(scenario_instance_objective)
assert hasattr(scenario_instance, "_ScenarioTreeSymbolMap")
scenario_tree_symbol_map = scenario_instance._ScenarioTreeSymbolMap
del scenario_instance._ScenarioTreeSymbolMap
if scenario._instance_original_objective_object is not None:
assert scenario._instance_original_objective_object is not \
scenario_instance_objective
scenario._instance_original_objective_object.activate()
assert not hasattr(scenario_instance,
"_PySP_compiled_linear_constraints")
compile_block_linear_constraints(scenario_instance,
"_PySP_compiled_linear_constraints",
**kwds)
#
# Re-add PySP generated model components
#
scenario_instance.add_component(scenario_instance_cost_expression.name,
scenario_instance_cost_expression)
scenario_instance.add_component(scenario_instance_objective.name,
scenario_instance_objective)
scenario_instance._ScenarioTreeSymbolMap = scenario_tree_symbol_map
if scenario._instance_original_objective_object is not None:
scenario._instance_original_objective_object.deactivate()
stop_time = time.time()
if output_times:
print("Total time to compile instance for scenario %s: %.2f seconds"
% (scenario._name, stop_time-start_time))
def pysp_compile_scenario_tree_register_options(options):
from pyomo.pysp.scenariotree.scenariotreemanager import \
(ScenarioTreeManagerSerial,
ScenarioTreeManagerSPPyro)
safe_register_common_option(options, "disable_gc")
safe_register_common_option(options, "profile")
safe_register_common_option(options, "traceback")
safe_register_common_option(options, "verbose")
safe_register_common_option(options, "output_times")
safe_register_unique_option(
options,
"output_directory",
ConfigValue(
".",
domain=_domain_must_be_str,
description=(
"The directory in which to store all output files. "
"Default is '.'."
),
doc=None,
visibility=0))
safe_register_unique_option(
options,
"compiled_reference_model_filename",
ConfigValue(
"PySP_CompiledReferenceModel.py",
domain=_domain_must_be_str,
description=(
"The filename to use for the new reference model that uses "
"the compiled scenarios. This will be prefixed by the "
"output directory name where compiled scenarios are stored. "
"Default is 'PySP_CompiledReferenceModel.py'."
),
doc=None,
visibility=0))
safe_register_unique_option(
options,
"single_precision_storage",
ConfigValue(
False,
domain=bool,
description=(
"Use single precision memory for storing the compiled constraint "
"coefficients. By default double precision floating point storage "
"will be used. For more information, refer to the 'f' and 'd' type "
"codes in the Python built-in 'array' module."
),
doc=None,
visibility=0))
safe_register_common_option(options, "scenario_tree_manager")
ScenarioTreeManagerSerial.register_options(options)
ScenarioTreeManagerSPPyro.register_options(options)
#
# Convert a PySP scenario tree formulation to SMPS input files
#
def run_pysp_compile_scenario_tree(options):
from pyomo.pysp.scenariotree.scenariotreemanager import \
(ScenarioTreeManagerSerial,
ScenarioTreeManagerSPPyro)
ScenarioTreeManager_class = None
if options.scenario_tree_manager == 'serial':
ScenarioTreeManager_class = ScenarioTreeManagerSerial
elif options.scenario_tree_manager == 'sppyro':
ScenarioTreeManager_class = ScenarioTreeManagerSPPyro
with ScenarioTreeManager_class(options) \
as scenario_tree_manager:
scenario_tree_manager.initialize()
compile_scenario_tree(
scenario_tree_manager,
output_directory=options.output_directory,
compiled_reference_model_filename=\
options.compiled_reference_model_filename,
verbose=options.verbose,
output_times=options.output_times,
single_precision_storage=options.single_precision_storage)
#
# The main initialization / runner routine.
#
def exec_pysp_compile_scenario_tree(options):
import pyomo.environ
start_time = time.time()
try:
run_pysp_compile_scenario_tree(options)
finally:
# if an exception is triggered, and we're running with pyro,
# shut down everything - not doing so is annoying, and leads
# to a lot of wasted compute time. but don't do this if the
# shutdown-pyro option is disabled => the user wanted
if options.scenario_tree_manager == "sppyro":
if options.shutdown_pyro:
print("\n")
print("Shutting down Pyro solver components.")
shutdown_pyro_components(num_retries=0)
print("")
print("Total execution time=%.2f seconds"
% (time.time() - start_time))
return 0
#
# the main driver routine for the pysp_compile_scenario_tree script.
#
def main(args=None):
#
# Top-level command that executes the extensive form writer.
# This is segregated from run_ef_writer to enable profiling.
#
#
# Import plugins
#
import pyomo.environ
#
# Parse command-line options.
#
options = ConfigBlock()
pysp_compile_scenario_tree_register_options(options)
try:
ap = argparse.ArgumentParser(prog='pysp_compile_scenario_tree')
options.initialize_argparse(ap)
options.import_argparse(ap.parse_args(args=args))
except SystemExit as _exc:
# the parser throws a system exit if "-h" is specified
# - catch it to exit gracefully.
return _exc.code
return launch_command(exec_pysp_compile_scenario_tree,
options,
error_label="pysp_compile_scenario_tree: ",
disable_gc=options.disable_gc,
profile_count=options.profile,
traceback=options.traceback)
@pyomo_command('pysp_compile_scenario_tree',
"Compile all scenarios on a PySP scenario tree into a more "
"memory efficient form")
def pysp_compile_scenario_tree_main(args=None):
return main(args=args)
if __name__ == "__main__":
main(args=sys.argv[1:])