source: coopr.opt/stable/2.1/coopr/opt/results/results.py @ 1973

Revision 1973, 5.7 KB checked in by wehart, 4 years ago (diff)

Merged revisions 1949-1972 via svnmerge from
 https://software.sandia.gov/svn/public/coopr/coopr.opt/trunk

........

r1955 | jwatson | 2009-12-02 17:55:38 -0700 (Wed, 02 Dec 2009) | 3 lines


Fixed missing StringIO module import.

........

r1960 | jwatson | 2009-12-04 20:43:24 -0700 (Fri, 04 Dec 2009) | 5 lines


Fixed a major memory leak in the solver manager. When get_results() was previously invoked, the action handle was referenced as a key in the map, and the corresponding results object was deleted. However, it wasn't cleaned up in the solver manager results map. Consequently, the solver manager kept a copy of every result object ever created, which reaches GB size for long PH runs.


This *might* result in a slight change in semantics, but I don't think so: once a solver result is returned from the manager, whoever got it "owns it". In other words, you can't grab the result multiple times.

........

Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 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__all__ = ['SolverResults']
12
13import sys
14from container import *
15from pyutilib.enum import Enum
16from pyutilib.misc import Bunch
17import copy
18
19import StringIO
20
21import problem
22import solver
23import solution
24
25
26try:
27    import yaml
28    yaml_available=True
29except ImportError:
30    yaml_available=False
31
32
33class SolverResults(MapContainer):
34
35    undefined = undefined
36    default_print_options = solution.default_print_options
37
38    def __init__(self):
39        MapContainer.__init__(self)
40        self._sections = []
41        self._descriptions = {}
42        self.add('problem', ListContainer(problem.ProblemInformation), False, "Problem Information")
43        self.add('solver', ListContainer(solver.SolverInformation), False, "Solver Information")
44        self.add('solution', solution.SolutionSet(), False, "Solution Information")
45
46    def add(self, name, value, active, description):
47        self.declare(name, value=value, active=active)
48        tmp = self._convert(name)
49        self._sections.append(tmp)
50        self._descriptions[tmp]=description
51
52    def _repn_(self, option):
53        if not option.schema and not self._active and not self._required:
54            return ignore
55        tmp = {}
56        for key in self._sections:
57            rep = dict.__getitem__(self, key)._repn_(option)
58            if not rep == ignore:
59                tmp[key] = rep
60        return tmp
61
62    def Xwrite(self, **kwds):
63        if 'ostream' in kwds:
64            ostream = kwds['ostream']
65            del kwds['ostream']
66        else:
67            ostream = sys.stdout
68        if 'filename' in kwds:
69            OUTPUT=open(kwds['filename'],"w")
70            del kwds['filename']
71            kwds['ostream']=OUTPUT
72            self.write(**kwds)
73            OUTPUT.close()
74            return
75
76        option = copy.copy(solution.default_print_options)
77        for key in kwds:
78            setattr(option,key,kwds[key])
79
80        repn = self._repn_(option)
81        print >>ostream, "# =========================================================="
82        print >>ostream, "# = Solver Results                                         ="
83        print >>ostream, "# =========================================================="
84        for item in repn:
85            key = item.keys()[0]
86            print >>ostream, "# ----------------------------------------------------------"
87            print >>ostream, "#   %s" % self._descriptions[key]
88            print >>ostream, "# ----------------------------------------------------------"
89            #print repr(item)
90            yaml.dump(item, ostream, default_flow_style=False)
91
92    def write(self, **kwds):
93        if 'ostream' in kwds:
94            ostream = kwds['ostream']
95            del kwds['ostream']
96        else:
97            ostream = sys.stdout
98        if 'filename' in kwds:
99            OUTPUT=open(kwds['filename'],"w")
100            del kwds['filename']
101            kwds['ostream']=OUTPUT
102            self.write(**kwds)
103            OUTPUT.close()
104            return
105
106        option = copy.copy(SolverResults.default_print_options)
107        for key in kwds:
108            setattr(option,key,kwds[key])
109
110        repn = self._repn_(option)
111        #print "HERE",repn,len(repn)
112        print >>ostream, "# =========================================================="
113        print >>ostream, "# = Solver Results                                         ="
114        print >>ostream, "# =========================================================="
115        for i in range(0,len(self._order)):
116            key = self._order[i]
117            if not key in repn:
118                continue
119            item = dict.__getitem__(self,key)
120            print >>ostream, ""
121            print >>ostream, "# ----------------------------------------------------------"
122            print >>ostream, "#   %s" % self._descriptions[key]
123            print >>ostream, "# ----------------------------------------------------------"
124            print >>ostream, key+":",
125            if isinstance(item, ListContainer):
126                item.pprint(ostream, option, prefix="", repn=repn[key])
127            else:
128                item.pprint(ostream, option, prefix="  ", repn=repn[key])
129
130    def read(self, **kwds):
131        if not yaml_available:
132            raise IOError, "Aborting SolverResults.read() because PyYAML is not installed!"
133
134        if 'istream' in kwds:
135            istream = kwds['istream']
136            del kwds['istream']
137        else:
138            ostream = sys.stdin
139        if 'filename' in kwds:
140            INPUT=open(kwds['filename'],"r")
141            del kwds['filename']
142            kwds['istream']=INPUT
143            self.read(**kwds)
144            INPUT.close()
145            return
146
147        repn = yaml.load(istream, Loader=yaml.SafeLoader)
148        for i in range(0,len(self._order)):
149            key = self._order[i]
150            if not key in repn:
151                continue
152            item = dict.__getitem__(self,key)
153            item.load(repn[key])
154       
155    def __repr__(self):
156        return str(self._repn_(default_print_options))
157
158    def __str__(self):
159        ostream = StringIO.StringIO()
160        option=default_print_options
161        self.pprint(ostream, option, repn=self._repn_(option))
162        return ostream.getvalue()
163
164 
165
166       
167
168
169if __name__ == '__main__':
170    results = SolverResults()
171    results.write(schema=True)
172    #print results
Note: See TracBrowser for help on using the repository browser.