source: coopr.opt/stable/2.3/coopr/opt/base/convert.py @ 2313

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

Merged revisions 2234-2312 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.opt/trunk

........

r2234 | wehart | 2010-02-01 19:34:02 -0700 (Mon, 01 Feb 2010) | 2 lines


Updating changelog.

........

r2236 | wehart | 2010-02-01 19:38:27 -0700 (Mon, 01 Feb 2010) | 2 lines


Tagging coopr.opt 2.3 release

........

r2250 | wehart | 2010-02-03 13:42:06 -0700 (Wed, 03 Feb 2010) | 3 lines


Update to descriptions of solver status and associted termination
conditions.

........

r2251 | wehart | 2010-02-03 13:56:43 -0700 (Wed, 03 Feb 2010) | 3 lines


Changes due to refinement of termination condition management
Bug fix and update to baseline data.

........

r2257 | wehart | 2010-02-03 22:45:40 -0700 (Wed, 03 Feb 2010) | 6 lines


Augmenting the SolverResults? logic to support default values. This
capability is currently disabled, but I'm leaving it in in case we
want to use this in the future.


Misc fix to the activation logic.

........

r2258 | wehart | 2010-02-03 23:33:12 -0700 (Wed, 03 Feb 2010) | 4 lines


Adding a subpackage for optimization-specific testing tools.
Currently, this contains a customized PyUnit? testing class
that contains new testing functions.

........

r2259 | wehart | 2010-02-04 08:58:44 -0700 (Thu, 04 Feb 2010) | 4 lines


Adding new termination conditions.


Fixing problem setting up testing utilities when YAML is not installed.

........

r2263 | wehart | 2010-02-05 08:18:57 -0700 (Fri, 05 Feb 2010) | 6 lines


Misc rework of the converter utility to recognize that *.py files have
Pyomo models.


NOTE: If we start working with PuLP, then we might have an issue
distinguishing PuLP vs Pyomo Python files.

........

r2280 | jwatson | 2010-02-08 11:26:49 -0700 (Mon, 08 Feb 2010) | 1 line


Adding a suffixes attribute to the base solver class. This is a list of suffixes that the solver will - assuming it can - load into a solution. Examples are rc, dual, slack, etc. I modified the derived solver plugin classes to throw an exception if provided a suffix that it can't handle. I have little doubt that this change will cause some tests to fail - I'll look at this in a bit. The change was motivated by the expense of querying a solution, loading a solution, and shipping a lot of unnecessary information across the network.

........

File size: 3.4 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, guess_format
16from error import *
17from pyutilib.component.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    #
43    # Setup list of source problem types
44    #
45    tmp = args[0]
46    if isinstance(tmp,basestring):
47       fname = tmp.split(os.sep)[-1]
48       if os.sep in fname:   #pragma:nocover
49          fname = tmp.split(os.sep)[-1]
50       source_ptype = [guess_format(fname)]
51       if source_ptype is [None]:
52            raise ConverterError, "Unknown suffix type: "+suffix
53    else:
54       source_ptype = args[0].valid_problem_types()
55
56    #
57    # Setup list of valid problem types
58    #
59    valid_ptypes = copy.copy(valid_problem_types)
60    if target_problem_type is not None:
61       valid_ptypes.remove(target_problem_type)
62       valid_ptypes = [target_problem_type]  + valid_ptypes
63    if source_ptype[0] in valid_ptypes:
64       ##print "HERE",source_ptype, valid_ptypes
65       valid_ptypes.remove(source_ptype[0])
66       valid_ptypes = [source_ptype[0]]  + valid_ptypes
67
68    #
69    # Iterate over the valid problem types, starting with the target type
70    #
71    # Apply conversion and return for first match
72    #
73    for ptype in valid_ptypes:
74      for s_ptype in source_ptype:
75        #print "HERE",str(s_ptype),str(ptype),args[0]
76        #
77        # If the source and target types are equal, then simply the return
78        # the args (return just the first element of the tuple if it has length
79        # one.
80        #
81        if s_ptype == ptype:
82           return (args,ptype,None)
83        #
84        # Otherwise, try to convert
85        #
86        for converter in ExtensionPoint(IProblemConverter):
87            #print "HERE",converter,s_ptype,ptype
88            if converter.can_convert(s_ptype,ptype):
89                tmp = [s_ptype,ptype] + list(args)
90                tmp = tuple(tmp)
91                problem_files, symbol_map = converter.apply(*tmp)
92                return problem_files, ptype, symbol_map
93    raise ConverterError, "No conversion possible.  Source problem type: %s.  Valid target types: %s" % (str(source_ptype[0]),map(str,valid_ptypes))
94
Note: See TracBrowser for help on using the repository browser.