source: coopr.pysp/stable/2.1/coopr/pysp/ef_writer_script.py @ 2068

Last change on this file since 2068 was 2068, checked in by wehart, 10 years ago

Merged revisions 1952-2067 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pysp/trunk

........

r1956 | jwatson | 2009-12-02 17:56:53 -0700 (Wed, 02 Dec 2009) | 3 lines


Added --scenario-solver-options and --ef-solver-options options to the "runph" script.

........

r1957 | dlwoodr | 2009-12-03 14:17:35 -0700 (Thu, 03 Dec 2009) | 2 lines


Documentation updates for pysp

........

r1974 | wehart | 2009-12-06 17:20:56 -0700 (Sun, 06 Dec 2009) | 2 lines


Updating PyPI categories

........

r1978 | jwatson | 2009-12-10 21:29:33 -0700 (Thu, 10 Dec 2009) | 3 lines


Eliminated exception-handling logic when loading user-defined extension modules in PH. The range of exceptions is too large, and for debugging purposes, it is more useful to see the raw trace output.

........

r1979 | jwatson | 2009-12-10 22:23:17 -0700 (Thu, 10 Dec 2009) | 5 lines


Biggest enhancement: The efwriter command-line script now has the option to output a CVaR-weighted objective term. Not extensively tested, but behaves sane on a number of small test cases.


Improved exception handling and error diagnostics in both the runph and efwriter scripts.

........

r1985 | jwatson | 2009-12-12 10:45:17 -0700 (Sat, 12 Dec 2009) | 3 lines


Modified PH to only use warm-starts if a solver has the capability!

........

r1998 | jwatson | 2009-12-13 15:17:58 -0700 (Sun, 13 Dec 2009) | 3 lines


Changed references to _component to active_component.

........

r2026 | wehart | 2009-12-21 23:27:06 -0700 (Mon, 21 Dec 2009) | 2 lines


Attempting to update PH. I'm not sure if this works, since I don't know how to test PH.

........

r2029 | jwatson | 2009-12-22 09:52:21 -0700 (Tue, 22 Dec 2009) | 3 lines


Some fixes to the ef writer based on Bill's recent changes to _initialize_constraint.

........

r2035 | jwatson | 2009-12-22 21:10:32 -0700 (Tue, 22 Dec 2009) | 3 lines


Added --scenario-mipgap option to PH script. Added _mipgap attribute to PH object. This attribute is mirrored to the solver plugin at the initiation of each iteration, after any PH extensions have the opportunity to provide a new value to the attribute. It is currently made use of by the WW PH extension.

........

r2037 | dlwoodr | 2009-12-23 14:38:43 -0700 (Wed, 23 Dec 2009) | 2 lines


add this example from Fernando

........

r2038 | dlwoodr | 2009-12-23 14:46:56 -0700 (Wed, 23 Dec 2009) | 3 lines


finish the job: we can now duplicate Fernando's example

........

r2039 | jwatson | 2009-12-23 15:13:54 -0700 (Wed, 23 Dec 2009) | 3 lines


Missed fix with new constraint initialization syntax in PH linearization.

........

r2058 | jwatson | 2009-12-29 10:57:58 -0700 (Tue, 29 Dec 2009) | 3 lines


Missed some _initialize_constraint function calls within the PySP EF writer during the recent switch to the corresponding "add" method.

........

r2059 | jwatson | 2009-12-29 10:58:34 -0700 (Tue, 29 Dec 2009) | 3 lines


Enabling garbage collection by default in PH.

........

r2060 | jwatson | 2009-12-29 10:59:05 -0700 (Tue, 29 Dec 2009) | 3 lines


Elimnating some debug output.

........

r2061 | jwatson | 2009-12-29 11:07:47 -0700 (Tue, 29 Dec 2009) | 3 lines


Fixing some option documentation in PH.

........

r2062 | jwatson | 2009-12-29 12:00:37 -0700 (Tue, 29 Dec 2009) | 3 lines


Added ef-mipgap option to PH scripts.

........

  • Property svn:executable set to *
File size: 4.9 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# Setup command-line options
27#
28
29parser = OptionParser()
30parser.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=".")
36parser.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=".")
42parser.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)
47parser.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)
53parser.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)
59parser.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")
65parser.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)
70parser.usage="efwriter [options]"
71
72def 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(options.model_directory, options.instance_directory, options.output_file, \
87                         generate_weighted_cvar, cvar_weight, risk_alpha)
88
89   return
90
91def 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
Note: See TracBrowser for help on using the repository browser.