source: trunk/coopr/opt/reader/sol.py @ 1772

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

Updates to allow for proper handling of variable maps in cases where formats cannot handle fully qualified,
human-readable variable names. Many ripple effects, as problem writers now must return a variable map (or None,
if not applicable. CBC now properly handles and reads NL and SOL input/output combinations!

File size: 4.4 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#
12# Class for reading an AMPL *.sol file
13#
14
15import os
16import sys
17import re
18
19from coopr.opt.base import results
20from coopr.opt.base.formats import *
21from coopr.opt.base.solution import SolverResults, Solution
22
23class ResultsReader_sol(results.AbstractResultsReader):
24    """
25    Class that reads in a *.sol results file and generates a
26    SolverResults object.
27    """
28
29    def __init__(self, name=None):
30        results.AbstractResultsReader.__init__(self,ResultsFormat.sol)
31        if not name is None:
32            self.name = name
33
34    def __call__(self, filename, res=None, soln=None):
35        """
36        Parse a *.sol file
37        """
38        if res is None:
39            res = SolverResults() 
40        #
41        IN = open(filename,"r")
42        msg = ""
43        line = IN.readline()
44        while line:
45            if line[0] == '\n' or (line[0] == '\r' and line[1] == '\n'):
46                break
47            msg += line
48            line = IN.readline()
49        z = []
50        line = IN.readline()
51        if line[:7] == "Options":
52            line = IN.readline()
53            nopts = int(line)
54            need_vbtol = False
55            if nopts > 4:           # WEH - when is this true?
56                nopts -= 2
57                need_vbtol = True
58            for i in xrange(nopts + 4):
59                line = IN.readline()
60                z += [int(line)]
61            if need_vbtol:          # WEH - when is this true?
62                line = IN.readline()
63                z += [float(line)]
64        else:
65            raise ValueError,"Error reading \"" + filename + "\": no Options line found."
66        n = z[nopts + 3] # variables
67        m = z[nopts + 1] # constraints
68        x = []
69        y = []
70        for i in xrange(m):
71            line = IN.readline()
72            y += [float(line)]
73        for i in xrange(n):
74            line = IN.readline()
75            x += [float(line)]
76        objno = [0,0]
77        line = IN.readline()
78        if line:                    # WEH - when is this true?
79            if line[:5] != "objno":         #pragma:nocover
80                raise ValueError, "Error reading \"" + filename + "\": expected \"objno\", found", line
81            t = line.split()
82            if len(t) != 3:
83                raise ValueError, "Error reading \"" + filename + "\": expected two numbers in objno line, but found", line
84            objno = [int(t[1]), int(t[2])]
85        IN.close()
86        res.solver.message = msg.strip()
87        res.solver.message = res.solver.message.replace("\n",". ")
88        ##res.solver.instanceName = osrl.header.instanceName
89        ##res.solver.systime = osrl.header.time
90        res.solver.id = objno[1]
91        ##res.problem.name = osrl.header.instanceName
92        if soln is None:
93            soln = res.solution.create()
94        ##soln.status = Solution.SolutionStatus(soln.status_type)
95        ##soln.status_description = soln.status_description
96        soln.message = msg.strip()
97        for i in range(0,len(x)):
98            #soln.variable.add("_svar["+str(i)+"]", x[i])
99            soln.variable.add("V"+str(i), x[i])
100        for i in range(0,len(y)):
101            #soln.dual.add("_scon["+str(i)+"]", y[i])
102            soln.dual.add("c"+str(i), y[i])
103        #soln.value = objno[0]
104        try:
105            #
106            # This is a bit of a hack to accommodate PICO.  If
107            # the PICO parser has parsed the # of constraints, then
108            # don't try to read it in from the *.sol file.  The reason
109            # is that these may be inconsistent values!
110            #
111            res.problem.num_constraints
112        except AttributeError:
113            res.problem.num_constraints = m
114        if res.problem.num_constraints is None:
115            res.problem.num_constraints = m
116        res.problem.num_variables = n
117        #res.problem.num_objectives = len(objno)
118        res.problem.num_objectives = 1
119        return res
120
121
122results.ReaderRegistration(str(ResultsFormat.sol), ResultsReader_sol)
123
124
Note: See TracBrowser for help on using the repository browser.