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 | |
---|
12 | import sys |
---|
13 | import os |
---|
14 | from optparse import OptionParser |
---|
15 | |
---|
16 | import pyutilib.services |
---|
17 | import textwrap |
---|
18 | import traceback |
---|
19 | import cProfile |
---|
20 | import pstats |
---|
21 | import gc |
---|
22 | |
---|
23 | from coopr.pysp.ef import * |
---|
24 | |
---|
25 | # |
---|
26 | # Setup command-line options |
---|
27 | # |
---|
28 | |
---|
29 | parser = OptionParser() |
---|
30 | parser.add_option("--model-directory", |
---|
31 | help="The directory in which all model (reference and scenario) definitions are stored. Default is \".\".", |
---|
32 | action="store", |
---|
33 | dest="model_directory", |
---|
34 | type="string", |
---|
35 | default=".") |
---|
36 | parser.add_option("--instance-directory", |
---|
37 | help="The directory in which all instance (reference and scenario) definitions are stored. Default is \".\".", |
---|
38 | action="store", |
---|
39 | dest="instance_directory", |
---|
40 | type="string", |
---|
41 | default=".") |
---|
42 | parser.add_option("--generate-weighted-cvar", |
---|
43 | help="Add a weighted CVaR term to the primary objective", |
---|
44 | action="store_true", |
---|
45 | dest="generate_weighted_cvar", |
---|
46 | default=False) |
---|
47 | parser.add_option("--cvar-weight", |
---|
48 | help="The weight associated with the CVaR term in the risk-weighted objective formulation.", |
---|
49 | action="store", |
---|
50 | dest="cvar_weight", |
---|
51 | type="float", |
---|
52 | default=1.0) |
---|
53 | parser.add_option("--risk-alpha", |
---|
54 | help="The probability threshold associated with cvar (or any future) risk-oriented performance metrics.", |
---|
55 | action="store", |
---|
56 | dest="risk_alpha", |
---|
57 | type="float", |
---|
58 | default=0.95) |
---|
59 | parser.add_option("--output-file", |
---|
60 | help="Specify the name of the extensive form output file", |
---|
61 | action="store", |
---|
62 | dest="output_file", |
---|
63 | type="string", |
---|
64 | default="efout.lp") |
---|
65 | parser.add_option("--profile", |
---|
66 | help="Enable profiling of Python code. The value of this option is the number of functions that are summarized.", |
---|
67 | action="store", |
---|
68 | dest="profile", |
---|
69 | default=0) |
---|
70 | parser.usage="efwriter [options]" |
---|
71 | |
---|
72 | def run_ef_writer(options, args): |
---|
73 | |
---|
74 | # if the user enabled the addition of the weighted cvar term to the objective, |
---|
75 | # then validate the associated parameters. |
---|
76 | generate_weighted_cvar = False |
---|
77 | cvar_weight = None |
---|
78 | risk_alpha = None |
---|
79 | |
---|
80 | if options.generate_weighted_cvar is True: |
---|
81 | |
---|
82 | generate_weighted_cvar = True |
---|
83 | cvar_weight = options.cvar_weight |
---|
84 | risk_alpha = options.risk_alpha |
---|
85 | |
---|
86 | write_ef_from_scratch(os.path.expanduser(options.model_directory), os.path.expanduser(options.instance_directory), os.path.expanduser(options.output_file), \ |
---|
87 | generate_weighted_cvar, cvar_weight, risk_alpha) |
---|
88 | |
---|
89 | return |
---|
90 | |
---|
91 | def run(args=None): |
---|
92 | |
---|
93 | # |
---|
94 | # Top-level command that executes the extensive form writer. |
---|
95 | # This is segregated from run_ef_writer to enable profiling. |
---|
96 | # |
---|
97 | |
---|
98 | # for a one-pass execution, garbage collection doesn't make |
---|
99 | # much sense - so I'm disabling it. Because: It drops the run-time |
---|
100 | # by a factor of 3-4 on bigger instances. |
---|
101 | gc.disable() |
---|
102 | |
---|
103 | # |
---|
104 | # Parse command-line options. |
---|
105 | # |
---|
106 | try: |
---|
107 | (options, args) = parser.parse_args(args=args) |
---|
108 | except SystemExit: |
---|
109 | # the parser throws a system exit if "-h" is specified - catch |
---|
110 | # it to exit gracefully. |
---|
111 | return |
---|
112 | |
---|
113 | if options.profile > 0: |
---|
114 | # |
---|
115 | # Call the main ef writer with profiling. |
---|
116 | # |
---|
117 | tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile") |
---|
118 | tmp = cProfile.runctx('run_ef_writer(options,args)',globals(),locals(),tfile) |
---|
119 | p = pstats.Stats(tfile).strip_dirs() |
---|
120 | p.sort_stats('time', 'cum') |
---|
121 | options.profile = eval(options.profile) |
---|
122 | p = p.print_stats(options.profile) |
---|
123 | p.print_callers(options.profile) |
---|
124 | p.print_callees(options.profile) |
---|
125 | p = p.sort_stats('cum','calls') |
---|
126 | p.print_stats(options.profile) |
---|
127 | p.print_callers(options.profile) |
---|
128 | p.print_callees(options.profile) |
---|
129 | p = p.sort_stats('calls') |
---|
130 | p.print_stats(options.profile) |
---|
131 | p.print_callers(options.profile) |
---|
132 | p.print_callees(options.profile) |
---|
133 | pyutilib.services.TempfileManager.clear_tempfiles() |
---|
134 | ans = [tmp, None] |
---|
135 | else: |
---|
136 | # |
---|
137 | # Call the main EF writer without profiling. |
---|
138 | # |
---|
139 | ans = run_ef_writer(options, args) |
---|
140 | |
---|
141 | gc.enable() |
---|
142 | return ans |
---|
143 | |
---|