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

Last change on this file since 1949 was 1949, checked in by wehart, 11 years ago

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

........

r1865 | wehart | 2009-11-13 23:33:38 -0700 (Fri, 13 Nov 2009) | 2 lines


Updating the trunk version number.

........

r1897 | wehart | 2009-11-16 14:06:32 -0700 (Mon, 16 Nov 2009) | 6 lines


Standardized the technique used for guessing a file's format from
its name.


Standardized plugin interfaces to sort the plugin names when they
are requested by the user.

........

r1899 | wehart | 2009-11-18 13:00:34 -0700 (Wed, 18 Nov 2009) | 3 lines


Reorganization to prepare for the use of
a new data structure to manage results.

........

r1900 | wehart | 2009-11-18 13:17:57 -0700 (Wed, 18 Nov 2009) | 2 lines


Bug fixes to account for the reorganization of coopr.opt

........

r1902 | wehart | 2009-11-18 16:06:07 -0700 (Wed, 18 Nov 2009) | 2 lines


Misc updates.

........

r1903 | wehart | 2009-11-18 16:29:42 -0700 (Wed, 18 Nov 2009) | 2 lines


Misc fixes.

........

r1904 | wehart | 2009-11-18 17:00:29 -0700 (Wed, 18 Nov 2009) | 2 lines


rework to get some YAML data output working.

........

r1905 | wehart | 2009-11-18 19:59:58 -0700 (Wed, 18 Nov 2009) | 3 lines


Major rework. I've started to get tests passing, but this commit
is mostly to archive the current state of development.

........

r1907 | jwatson | 2009-11-18 20:24:21 -0700 (Wed, 18 Nov 2009) | 3 lines


Various cleanup of output involving SOL-originated format solutions.

........

r1908 | jwatson | 2009-11-18 20:59:02 -0700 (Wed, 18 Nov 2009) | 3 lines


Using symbol map in solver results output to print names that a user will actually recognize!

........

r1915 | wehart | 2009-11-20 07:03:47 -0700 (Fri, 20 Nov 2009) | 3 lines


Further updates to new solver results object. This capability is
still disabled within Coopr.

........

r1918 | wehart | 2009-11-20 15:27:31 -0700 (Fri, 20 Nov 2009) | 4 lines


Rework of the SolverResults? object, to provide for a more explicit
definition of solver results, and to support loading of
solver results from YAML-formated data.

........

r1921 | wehart | 2009-11-20 15:39:41 -0700 (Fri, 20 Nov 2009) | 2 lines


Update of the SolutionSet? api.

........

r1935 | wehart | 2009-11-22 20:09:13 -0700 (Sun, 22 Nov 2009) | 2 lines


Changes to maek the results object pickl-able.

........

r1936 | wehart | 2009-11-22 20:09:34 -0700 (Sun, 22 Nov 2009) | 2 lines


Adding a test to verify that SolverResults? objects are pickleable.

........

r1937 | wehart | 2009-11-23 09:46:06 -0700 (Mon, 23 Nov 2009) | 3 lines


Adding explicit import of PyYAML, and corresponding logic
to ignore yaml-dependent tests.

........

r1938 | wehart | 2009-11-23 11:07:50 -0700 (Mon, 23 Nov 2009) | 6 lines


Changes to clarify what solver outputs entail. Now, logfiles
are processed whenever available, and other data is processed with
an appropriate reader. The exception is 'soln' files, which
generically refers to solver-specific solution results. These are
parsed with a solver method.

........

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