source: coopr.colin/trunk/coopr/colin/core/dakota_text_io.py @ 2201

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

Update to Coopr to account for changes in PyUtilib? package names.

File size: 3.3 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# TODO: pass in variable/function name information into the
12# optimizer.  This will require an augmented point and request
13# specification.
14
15"""
16Define the plugin for DAKOTA TEXT IO
17"""
18
19import re
20from pyutilib.component.core import *
21from problem_io import *
22from pyutilib.math import as_number
23
24class DakotaTextIO(SingletonPlugin):
25    """The reader/writer for the DAKOTA TEXT IO Formats"""
26
27    implements(IOptProblemIO)
28
29    def __init__(self):
30        SingletonPlugin.__init__(self)
31        self.name = 'dakota'
32
33    def read(self, filename, point):
34        """
35        Read a point and request information.
36        This method returns a tuple: point, requests
37        """
38        self.varname = []
39        self.funcname = []
40        point = []
41        requests = {}
42        INPUT = open(filename,'r')
43        #
44        # Process variables
45        #
46        line = INPUT.readline()
47        nvars = as_number(re.split('[ \t]+',line.strip())[0])
48        for i in range(nvars):
49            line = INPUT.readline()
50            tokens = re.split('[ \t]+',line.strip())
51            point.append( as_number(tokens[0]) )
52            if len(tokens) > 1:
53                self.varnames.append(tokens[1])
54        #
55        # Process requests
56        #
57        line = INPUT.readline()
58        nfunctions = as_number(re.split('[ \t]+',line.strip())[0])
59        for i in range(nfunctions):
60            line = INPUT.readline()
61            tokens = re.split('[ \t]+',line.strip())
62            asv = as_number(tokens[0])
63            if len(tokens) > 1:
64                self.funcname.append(tokens[1])
65            if asv & 1:
66                requests['FunctionValues'] = ''
67                requests['NonlinearConstraintValues'] = ''
68            if asv & 2:
69                requests['Gradient'] = ''
70            if asv & 4:
71                requests['Hessian'] = ''
72        return point, requests
73
74    def write(self, filename, response):
75        """
76        Write response information to a file.
77        """
78        OUTPUT = open(filename,"w")
79        fno = 0
80        if 'FunctionValues' in response:
81            for val in response['FunctionValues']:
82                print >>OUTPUT, val, self.funcname[fno]
83                fno += 1
84        if 'NonlinearConstraintValues' in response:
85            for val in response['NonlinearConstraintValues']:
86                print >>OUTPUT, val, self.funcname[fno]
87                fno += 1
88        if 'Gradient' in response:
89            for val in response['Gradient']:
90                print >>OUTPUT, val,
91            print >>OUTPUT, ""
92        if 'Jacobian' in response:
93            for grad in response['Jacobian']:
94                for val in grad:
95                    print >>OUTPUT, val,
96                print >>OUTPUT, ""
97        if 'Hessian' in response:
98            print >>OUTPUT, "# ERROR: cannot print Hessian information"
99        OUTPUT.close()
100
Note: See TracBrowser for help on using the repository browser.