source: projects/coopr.neos/trunk/coopr/plugins/neos/kestrel_plugin.py @ 56

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

Updating pyutilib.component import.

File size: 4.1 KB
Line 
1
2__all__ = []
3
4import os
5import pyutilib.component
6from coopr.opt.parallel.manager import *
7from coopr.opt.parallel.solver import *
8from coopr.opt.results import SolverResults
9import kestrel
10
11import pyutilib
12
13
14class SolverManager_NEOS(AsynchronousSolverManager):
15
16    def clear(self):
17        """
18        Clear manager state
19        """
20        AsynchronousSolverManager.clear(self)
21        self.kestrel = kestrel.kestrelAMPL()
22        self._opt = None
23        self._ah = {} # index is the NEOS job number
24
25        # to grab streamed output from NEOS, need to keep
26        # map of action handle to the to-date string of
27        # extracted output.
28        # TBD: The following entries aren't currently cleaned up, but
29        #      we're still trying to get the basics down.
30        # store pairs of NEOS message offset and NEOS message string.
31        # index into the map is the NEOS job number
32        self._neos_log = {} 
33
34    def _perform_queue(self, ah, *args, **kwds):
35        """
36        Perform the queue operation.  This method returns the ActionHandle,
37        and the ActionHandle status indicates whether the queue was successful.
38        """
39        if 'opt' in kwds:
40            self._opt = kwds['opt']
41            del kwds['opt']
42        else:
43            raise ActionManagerError, "Undefined solver"
44        self._opt._presolve(*args, **kwds)
45        #
46        # Apply kestrel
47        #
48        os.environ['kestrel_options'] = 'solver=%s' % self._opt.neos_name
49        os.environ[self._opt.neos_name.lower()+'_options'] = self._opt.solver_options
50        xml = self.kestrel.formXML(self._opt._problem_files[0])
51        (jobNumber, password) = self.kestrel.submit(xml)
52        ah.job = jobNumber
53        ah.password = password
54        #
55        # Store action handle, and return
56        #
57        self._ah[jobNumber] = ah
58        self._neos_log[jobNumber] = (0, "")
59        return ah
60
61    def _perform_wait_any(self):
62        """
63        Perform the wait_any operation.  This method returns an
64        ActionHandle with the results of waiting.  If None is returned
65        then the ActionManager assumes that it can call this method again.
66        Note that an ActionHandle can be returned with a dummy value,
67        to indicate an error.
68        """
69        for jobNumber in self._ah:
70
71            status = self.kestrel.neos.getJobStatus(jobNumber,self._ah[jobNumber].password)
72
73            if not status in ("Running", "Waiting"):
74
75                ah = self._ah[jobNumber]               
76               
77                # the job is done.
78                self._ah[jobNumber] = None
79                ah.status = ActionStatus.done
80               
81                # retrieve the final results, which are in message/log format.
82                results = self.kestrel.neos.getFinalResults(jobNumber, ah.password)
83
84                (current_offset, current_message) = self._neos_log[jobNumber]
85                OUTPUT=open(self._opt.log_file,'w')
86                print >>OUTPUT, current_message
87                OUTPUT.close()
88
89                OUTPUT=open(self._opt.soln_file,'w')
90                print >>OUTPUT, results.data
91                OUTPUT.close()
92
93                rc = None
94                solver_results = self._opt.process_output(rc)
95                solver_results.symbol_map = self._opt._symbol_map
96                self.results[ah.id] = solver_results
97                return ah
98            else:
99                # grab the partial messages from NEOS as you go, in case you want
100                # to output on-the-fly. we don't currently do this, but the infrastructure
101                # is in place.
102                (current_offset, current_message) = self._neos_log[jobNumber]
103                # TBD: blocking isn't the way to go, but non-blocking was triggering some exception in kestrel.
104                (message_fragment, new_offset) = self.kestrel.neos.getIntermediateResults(jobNumber, self._ah[jobNumber].password, current_offset)
105                print message_fragment,
106                self._neos_log[jobNumber] = (new_offset, current_message + message_fragment.data)
107
108        return None
109
110
111SolverManagerRegistration("neos", SolverManager_NEOS)
112
Note: See TracBrowser for help on using the repository browser.