source: coopr.pysp/trunk/coopr/pysp/ef_writer_script.py @ 2414

Last change on this file since 2414 was 2414, checked in by jwatson, 11 years ago

Various EF writer changes in PySP, including:
1) Added a --verbose option, and by default am disabling echo of the scenario tree.
2) Changed the default behavior, due to recent experience with lot sizing and wind farm examples, to enable garbage collection.
3) Added a --disable-gc option to disable garbage collection, set to False by default.

  • Property svn:executable set to *
File size: 5.7 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2009 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11
12import sys
13import os
14from optparse import OptionParser
15
16import pyutilib.services
17import textwrap
18import traceback
19import cProfile
20import pstats
21import gc
22
23from coopr.pysp.ef import *
24
25#
26# utility method to construct an option parser for ef writer arguments
27#
28
29def construct_ef_writer_options_parser(usage_string):
30
31   parser = OptionParser()
32   parser.add_option("--verbose",
33                     help="Generate verbose output, beyond the usual status output. Default is False.",
34                     action="store_true",
35                     dest="verbose",
36                     default=False)
37   parser.add_option("--model-directory",
38                     help="The directory in which all model (reference and scenario) definitions are stored. Default is \".\".",
39                     action="store",
40                     dest="model_directory",
41                     type="string",
42                     default=".")
43   parser.add_option("--instance-directory",
44                     help="The directory in which all instance (reference and scenario) definitions are stored. Default is \".\".",
45                     action="store",
46                     dest="instance_directory",
47                     type="string",
48                     default=".")
49   parser.add_option("--generate-weighted-cvar",
50                     help="Add a weighted CVaR term to the primary objective",
51                     action="store_true",
52                     dest="generate_weighted_cvar",
53                     default=False)
54   parser.add_option("--cvar-weight",
55                     help="The weight associated with the CVaR term in the risk-weighted objective formulation.",
56                     action="store",
57                     dest="cvar_weight",
58                     type="float",
59                     default=1.0)
60   parser.add_option("--risk-alpha",
61                     help="The probability threshold associated with cvar (or any future) risk-oriented performance metrics.",
62                     action="store",
63                     dest="risk_alpha",
64                     type="float",
65                     default=0.95)
66   parser.add_option("--output-file",
67                     help="Specify the name of the extensive form output file",
68                     action="store",
69                     dest="output_file",
70                     type="string",
71                     default="efout.lp")
72   parser.add_option("--profile",
73                     help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
74                     action="store",
75                     dest="profile",
76                     default=0)
77   parser.add_option("--disable-gc",
78                     help="Disable the python garbage collecter. Default is False.",
79                     action="store_true",
80                     dest="disable_gc",
81                     default=False)
82   parser.usage=usage_string
83
84   return parser
85   
86def run_ef_writer(options, args):
87
88   # if the user enabled the addition of the weighted cvar term to the objective,
89   # then validate the associated parameters.
90   generate_weighted_cvar = False
91   cvar_weight = None
92   risk_alpha = None
93
94   if options.generate_weighted_cvar is True:
95
96      generate_weighted_cvar = True
97      cvar_weight = options.cvar_weight
98      risk_alpha = options.risk_alpha
99
100   write_ef_from_scratch(os.path.expanduser(options.model_directory),
101                         os.path.expanduser(options.instance_directory),
102                         os.path.expanduser(options.output_file),
103                         options.verbose,
104                         generate_weighted_cvar, cvar_weight, risk_alpha)
105
106def run(args=None):
107
108    #
109    # Top-level command that executes the extensive form writer.
110    # This is segregated from run_ef_writer to enable profiling.
111    #
112
113    #
114    # Parse command-line options.
115    #
116    try:
117       options_parser = construct_ef_writer_options_parser("efwriter [options]")
118       (options, args) = options_parser.parse_args(args=args)
119    except SystemExit:
120       # the parser throws a system exit if "-h" is specified - catch
121       # it to exit gracefully.
122       return
123
124    if options.disable_gc is True:
125       gc.disable()
126    else:
127       gc.enable()
128
129    if options.profile > 0:
130        #
131        # Call the main ef writer with profiling.
132        #
133        tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
134        tmp = cProfile.runctx('run_ef_writer(options,args)',globals(),locals(),tfile)
135        p = pstats.Stats(tfile).strip_dirs()
136        p.sort_stats('time', 'cum')
137        options.profile = eval(options.profile)
138        p = p.print_stats(options.profile)
139        p.print_callers(options.profile)
140        p.print_callees(options.profile)
141        p = p.sort_stats('cum','calls')
142        p.print_stats(options.profile)
143        p.print_callers(options.profile)
144        p.print_callees(options.profile)
145        p = p.sort_stats('calls')
146        p.print_stats(options.profile)
147        p.print_callers(options.profile)
148        p.print_callees(options.profile)
149        pyutilib.services.TempfileManager.clear_tempfiles()
150        ans = [tmp, None]
151    else:
152        #
153        # Call the main EF writer without profiling.
154        #
155        ans = run_ef_writer(options, args)
156
157    gc.enable()
158   
159    return ans
160
Note: See TracBrowser for help on using the repository browser.