source: coopr.misc/trunk/scripts/pyro_mip_server @ 2327

Last change on this file since 2327 was 2327, checked in by jwatson, 10 years ago

Added import of cPickle instead of pickle, if it exists.

  • Property svn:executable set to *
File size: 3.0 KB
Line 
1#! /usr/bin/env python
2#
3# pyro_mip_server: A script that sets up a Pyro server for solving MIPs in
4#           a distributed manner.
5#
6#  _________________________________________________________________________
7#
8#  Coopr: A COmmon Optimization Python Repository
9#  Copyright (c) 2008 Sandia Corporation.
10#  This software is distributed under the BSD License.
11#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
12#  the U.S. Government retains certain rights in this software.
13#  For more information, see the FAST README.txt file.
14#  _________________________________________________________________________
15
16import os
17import os.path
18import sys
19from os.path import abspath, dirname
20sys.path.insert(0, dirname(dirname(abspath(__file__))))
21sys.path.append(".")
22
23try:
24   import cPickle as pickle
25except ImportError:
26   import pickle
27
28import pyutilib.services
29import pyutilib.pyro
30
31import coopr.opt
32
33class CooprMIPWorker(pyutilib.pyro.TaskWorker):
34
35    def process(self, data):
36        # construct the solver on this end, based on the input type stored in "data.opt".
37        opt = coopr.opt.SolverFactory(data.opt)
38        if opt is None:
39            raise ValueError, "Problem constructing solver `"+data.opt+"'"
40        opt.mipgap = data.mipgap
41        opt.suffixes = data.suffixes
42
43        # here is where we should set any options required by the solver, available
44        # as specific attributes of the input data object.
45        solver_options = data.solver_options
46        del data.solver_options
47        for key,value in solver_options.items():
48#            print "RECEIVED SOLVER OPTION="+key+"WITH VALUE="+value
49            setattr(opt.options,key,value)
50
51        problem_filename_suffix = os.path.split(data.filename)[1]
52        temp_problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix="."+problem_filename_suffix)
53        OUTPUT=open(temp_problem_filename,'w')
54        print >>OUTPUT, data.file
55        OUTPUT.close()
56
57        if data.warmstart_file is not None:
58           warmstart_filename_suffix = os.path.split(data.warmstart_filename)[1]
59           temp_warmstart_filename = pyutilib.services.TempfileManager.create_tempfile(suffix="."+warmstart_filename_suffix)
60           OUTPUT=open(temp_warmstart_filename,'w')
61           print >>OUTPUT, data.warmstart_file
62           OUTPUT.close()
63           opt.warm_start_solve = True
64           opt.warm_start_file_name = temp_warmstart_filename
65       
66        print "Applying solver="+data.opt+" to solve problem="+temp_problem_filename
67        results = opt.solve(temp_problem_filename, **data.kwds)
68        pyutilib.services.TempfileManager.clear_tempfiles()
69        # disabling write of solutions for now - this was getting too verbose,
70        # especially when running Progressive Hedging.
71        print "Solve completed - number of solutions="+str(len(results.solution))
72        sys.stdout.flush()
73#        results.write()
74#        sys.stdout.flush()
75        return pickle.dumps(results)
76
77
78pyutilib.pyro.TaskWorkerServer(CooprMIPWorker, argv=sys.argv)
79
Note: See TracBrowser for help on using the repository browser.