source: trunk/coopr/opt/base/convert.py @ 1768

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

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 3.5 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__all__ = ['IProblemConverter', 'convert_problem']
12
13import copy
14import os
15from formats import ProblemFormat
16from error import *
17from pyutilib.plugin.core import *
18
19
20class IProblemConverter(Interface):
21
22    def can_convert(self, from_type, to_type):
23        """Returns true if this object supports the specified conversion"""
24
25    def convert(self, from_type, to_type):
26        """Convert an instance of one type into another"""
27
28
29def convert_problem(args, target_problem_type, valid_problem_types):
30    """
31    Convert a problem, defined by the 'args' tuple, into another problem.
32    """
33    #print "HERE",args,target_problem_type,valid_problem_types
34    if len(valid_problem_types) == 0:
35       raise ConverterError, "No valid problem types"
36    if not (target_problem_type is None or \
37             target_problem_type in valid_problem_types):
38       raise ConverterError, "Problem type "+str(target_problem_type)+" is not valid"
39    if len(args) == 0:
40       raise ConverterError, "Empty argument list"
41    #
42    # Setup list of source problem types
43    #
44    tmp = args[0]
45    if isinstance(tmp,basestring):
46       fname = tmp.split(os.sep)[-1]
47       if os.sep in fname:   #pragma:nocover
48          fname = tmp.split(os.sep)[-1]
49       suffix = fname.split('.')[-1]
50       try:
51          if suffix == "lp":
52             source_ptype = [ProblemFormat.cpxlp]
53          else:
54             source_ptype = [getattr(ProblemFormat,suffix)]
55       except AttributeError:
56          raise ConverterError, "Unknown suffix type: "+suffix
57    else:
58       source_ptype = args[0].valid_problem_types()
59    #
60    # Setup list of valid problem types
61    #
62    valid_ptypes = copy.copy(valid_problem_types)
63    if target_problem_type is not None:
64       valid_ptypes.remove(target_problem_type)
65       valid_ptypes = [target_problem_type]  + valid_ptypes
66    if source_ptype[0] in valid_ptypes:
67       ##print "HERE",source_ptype, valid_ptypes
68       valid_ptypes.remove(source_ptype[0])
69       valid_ptypes = [source_ptype[0]]  + valid_ptypes
70    #
71    # Iterate over the valid problem types, starting with the target type
72    #
73    # Apply conversion and return for first match
74    #
75    for ptype in valid_ptypes:
76      for s_ptype in source_ptype:
77        #print "HERE",str(s_ptype),str(ptype),args[0]
78        #
79        # If the source and target types are equal, then simply the return
80        # the args (return just the first element of the tuple if it has length
81        # one.
82        #
83        if s_ptype == ptype:
84           return (args,ptype)
85        #
86        # Otherwise, try to convert
87        #
88        for converter in ExtensionPoint(IProblemConverter):
89            #print "HERE",converter,s_ptype,ptype
90            if converter.can_convert(s_ptype,ptype):
91                tmp = [s_ptype,ptype] + list(args)
92                tmp = tuple(tmp)
93                return (converter.apply(*tmp), ptype)
94    raise ConverterError, "No conversion possible.  Source problem type: %s.  Valid target types: %s" % (str(source_ptype[0]),map(str,valid_ptypes))
95
Note: See TracBrowser for help on using the repository browser.