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

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

Added model and instance directory options to the ef writer.

  • Property svn:executable set to *
File size: 3.6 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
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("--output-file",
43                  help="Specify the name of the extensive form output file",
44                  action="store",
45                  dest="output_file",
46                  type="string",
47                  default="efout.lp")
48parser.add_option("--profile",
49                  help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
50                  action="store",
51                  dest="profile",
52                  default=0)
53parser.usage="efwriter [options]"
54
55def run_ef_writer(options, args):
56
57   write_ef_from_scratch(options.model_directory, options.instance_directory, options.output_file)
58
59   return
60
61def run(args=None):
62
63    #
64    # Top-level command that executes the extensive form writer.
65    # This is segregated from run_ef_writer to enable profiling.
66    #
67
68    # for a one-pass execution, garbage collection doesn't make
69    # much sense - so I'm disabling it. Because: It drops the run-time
70    # by a factor of 3-4 on bigger instances.
71    gc.disable()       
72
73    #
74    # Parse command-line options.
75    #
76    try:
77       (options, args) = parser.parse_args(args=args)
78    except SystemExit:
79       # the parser throws a system exit if "-h" is specified - catch
80       # it to exit gracefully.
81       return
82
83    if options.profile > 0:
84        #
85        # Call the main ef writer with profiling.
86        #
87        tfile = pyutilib.TempfileManager.create_tempfile(suffix=".profile")
88        tmp = cProfile.runctx('run_ef_writer(options,args)',globals(),locals(),tfile)
89        p = pstats.Stats(tfile).strip_dirs()
90        p.sort_stats('time', 'cum')
91        options.profile = eval(options.profile)
92        p = p.print_stats(options.profile)
93        p.print_callers(options.profile)
94        p.print_callees(options.profile)
95        p = p.sort_stats('cum','calls')
96        p.print_stats(options.profile)
97        p.print_callers(options.profile)
98        p.print_callees(options.profile)
99        p = p.sort_stats('calls')
100        p.print_stats(options.profile)
101        p.print_callers(options.profile)
102        p.print_callees(options.profile)
103        pyutilib.TempfileManager.clear_tempfiles()
104        ans = [tmp, None]
105    else:
106        #
107        # Call the main EF writer without profiling.
108        #
109        ans = run_ef_writer(options, args)
110
111    gc.enable()
112    return ans
113
Note: See TracBrowser for help on using the repository browser.