Changeset 2265


Ignore:
Timestamp:
Feb 5, 2010 10:23:35 AM (10 years ago)
Author:
wehart
Message:

Extending the Pyomo converter to allow the conversion of both Pyomo Model objects
as well as Python modules that contain a Model definition.

Location:
coopr.plugins/trunk/coopr/plugins
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • coopr.plugins/trunk/coopr/plugins/converter/pico.py

    r2201 r2265  
    4949        return False
    5050
     51    def available(self):
     52        cmd = self.executable.service("pico_convert")
     53        return not cmd is None
     54
    5155    def apply(self, *args):
    5256        """
  • coopr.plugins/trunk/coopr/plugins/converter/pyomo.py

    r2201 r2265  
    1212__all__ = ['PyomoMIPConverter']
    1313
     14import coopr.pyomo
    1415from coopr.opt.base import *
    1516from pico import PicoMIPConverter
     
    3940        if from_type == ProblemFormat.pyomo and to_type == ProblemFormat.cpxlp:
    4041            return True
    41         if from_type == ProblemFormat.pyomo and to_type == ProblemFormat.mps:
     42        if self.pico_converter.available() and from_type == ProblemFormat.pyomo and to_type == ProblemFormat.mps:
    4243            return True
    4344        return False
     
    4950        """
    5051
     52        if isinstance(args[2], basestring):
     53            instance = None
     54        else:
     55            instance = args[2]
     56        #elif isinstance(args[2], coopr.pyomo.Model):
     57            #instance = args[2]
     58        #else:
     59            #raise IOError, "Bad data type for conversion: %s" % str(type(args[2]))
     60
    5161        if args[1] is ProblemFormat.cpxlp:
    5262            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.lp')
    53             (problem_filename, varmap) = args[2].write(filename=problem_filename,format=ProblemFormat.cpxlp)
    54             return (problem_filename,),None # no map file is necessary
    55         elif args[1] is ProblemFormat.mps:
     63            if instance:
     64                (problem_filename, varmap) = instance.write(filename=problem_filename,format=ProblemFormat.cpxlp)
     65                return (problem_filename,),None # no map file is necessary
     66            else:
     67                model = coopr.pyomo.scripting.pyomo2lp(['--save-model',problem_filename,args[2]])
     68                return (model.filename,),model.symbol_map
     69
     70        elif args[1] in [ProblemFormat.mps, ProblemFormat.nl]:
     71            problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
     72            if instance:
     73                (problem_filename, varmap) = instance.write(filename=problem_filename,format=ProblemFormat.nl)
     74            else:
     75                model = coopr.pyomo.scripting.pyomo2nl(['--save-model',problem_filename,args[2]])
     76                problem_filename = model.filename
     77                varmap = model.symbol_map
     78            if args[1] is ProblemFormat.nl:
     79                return (problem_filename,),varmap
     80            #
     81            # Convert from NL to MPS
     82            #
    5683            # TBD: We don't support a variable map file when going from NL to MPS within the PICO converter.
    57             problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
    58             (problem_filename, varmap) = args[2].write(filename=problem_filename,format=ProblemFormat.nl)
     84            # NOTE: this is a problem with the MPS writer that is provided by COIN-OR
     85            # NOTE: we should generalize this so it doesn't strictly depend on the PICO converter utility.
     86            #
    5987            ans = self.pico_converter.apply(ProblemFormat.nl,ProblemFormat.mps,problem_filename)
    6088            os.remove(problem_filename)
    6189            return ans
    62         elif args[1] is ProblemFormat.nl:
    63             problem_filename = pyutilib.services.TempfileManager.create_tempfile(suffix = '.pyomo.nl')
    64             (problem_filename, symbol_map) = args[2].write(filename=problem_filename,format=ProblemFormat.nl)
    65             return (problem_filename,),symbol_map # map file is necessary
    6690
  • coopr.plugins/trunk/coopr/plugins/tests/mip/test_convert.py

    r2201 r2265  
    145145        self.failIfEqual(re.match(".*tmp.*pyomo.lp$",ans[0][0]), None)
    146146
     147    def test_pyomo_lp1(self):
     148        """ Convert from Pyomo to LP with file"""
     149        ans = coopr.opt.convert_problem( (currdir+'model.py',ProblemFormat.cpxlp,), None, [ProblemFormat.cpxlp])
     150        self.failIfEqual(re.match(".*tmp.*pyomo.lp$",ans[0][0]), None)
     151
    147152    def test_mock_lp2(self):
    148153        """ Convert from NL to LP """
     
    171176        os.remove(ans[0][0])
    172177
     178    def test_pyomo_mps1(self):
     179        """ Convert from Pyomo to MPS with file"""
     180        try:
     181           ans = coopr.opt.convert_problem( (currdir+'model.py',ProblemFormat.mps,), None, [ProblemFormat.mps])
     182        except ConverterError, err:
     183           if pyutilib.services.registered_executable("pico_convert") is None:
     184                return
     185           else:
     186                self.fail("Expected ApplicationError because pico_convert is not available: '%s'" % str(err))
     187        self.failUnlessEqual(ans[0][0][-16:],"pico_convert.mps")
     188        os.remove(ans[0][0])
     189
    173190    def test_mock_nl1(self):
    174191        """ Convert from Pyomo to NL """
     
    178195        os.remove(ans[0][0])
    179196
     197    def test_pyomo_nl1(self):
     198        """ Convert from Pyomo to NL with file"""
     199        ans = coopr.opt.convert_problem( (currdir+'model.py',ProblemFormat.nl,), None, [ProblemFormat.nl])
     200        self.failIfEqual(re.match(".*tmp.*pyomo.nl$",ans[0][0]), None)
     201        os.remove(ans[0][0])
     202
    180203    def test_error1(self):
    181204        """ No valid problem types """
Note: See TracChangeset for help on using the changeset viewer.