Changeset 2359


Ignore:
Timestamp:
Feb 15, 2010 10:26:44 PM (10 years ago)
Author:
wehart
Message:
  1. Renaming the 'presolve' phase in Pyomo to 'preprocess'. We'll

eventually want an explicit presolve phase, but that will be applied
as a transformation, right before the problem is written and sent
to the solver. Currently, this preprocess phase collects data about
the model and computes the canonical representations for expressions.

  1. Created the ability to support the construction of concrete models.

The concrete_mode() method for Model objects enables this mode.
When in the concrete mode, each component added to the model has
its construct() method called immediately after the component is
added to the model. The symbolic_mode() method for Model objects
can be called to switch back to a symbolic mode (which defers the
execution of construct().

Bad things might happen if you switch back and forth between these
modes. Notably, if you ever generate components in symbolic mode,
then you need to switch back to symbolic mode before calling create().

Note that the Model.create() method is still called when the model
construction is 'done', even when done completely in concrete mode.
This basically calls the preprocessing steps, and this method returns
the current model (i.e. no cloning is done when building a concrete
model).

  1. Created an example directory for concrete models. Note that these

can still be solved with the Pyomo command line. In this
instance, the command line simply executes the script and applies
a solver.

Location:
coopr.pyomo/trunk
Files:
4 added
1 deleted
20 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/__init__.py

    r2264 r2359  
    2020import io
    2121from components import *
    22 import presolve
     22import preprocess
    2323import coopr.opt
    2424import transform
  • coopr.pyomo/trunk/coopr/pyomo/base/PyomoModel.py

    r2226 r2359  
    3939    """
    4040
    41     presolver_ep = pyutilib.component.core.ExtensionPoint(IPyomoPresolver)
     41    preprocessor_ep = pyutilib.component.core.ExtensionPoint(IPyomoPresolver)
    4242
    4343
     
    4545        """Constructor"""
    4646        Component.__init__(self, ctype=Model)
     47        self._defer_construction=True
    4748        #
    4849        # Define component dictionary: component type -> instance
     
    6667        self.tmpctr=0
    6768        self._varctr=0
    68         self._presolvers = ["simple_presolver"]
     69        self._preprocessors = ["simple_preprocessor"]
    6970        #
    7071        # Dictionaries that map from id to Variable/Constraints
     
    7778        #
    7879        self.statistics = Container()
     80
     81    def concrete_mode(self):
     82        self._defer_construction=False
     83
     84    def symbolic_mode(self):
     85        self._defer_construction=True
    7986
    8087    def components(self, ctype=None):
     
    203210        if not val.type() is Model:
    204211            val.model=self
     212        if not self._defer_construction:
     213            val.construct(None)
    205214
    206215    def __setattr__(self,name,val):
     
    235244        read in from a file.
    236245        """
    237         instance = self.clone()
    238         instance.load(filename)
    239         instance.presolve()
     246        if self._defer_construction:
     247            instance = self.clone()
     248            instance.load(filename)
     249        else:
     250            instance = self
     251        instance.preprocess()
    240252        if not name is None:
    241253            instance.name=name
     
    260272
    261273
    262     def presolve(self):
    263         """Apply the presolvers defined by the user"""
    264         for item in self._presolvers:
    265             self = Model.presolver_ep.service(item).presolve(self)
     274    def preprocess(self):
     275        """Apply the preprocess plugins defined by the user"""
     276        for item in self._preprocessors:
     277            self = Model.preprocessor_ep.service(item).preprocess(self)
    266278       
    267279
  • coopr.pyomo/trunk/coopr/pyomo/base/component.py

    r2025 r2359  
    55        self.active=True
    66        self._type=ctype
     7        self._constructed=False
    78        self.model=None
    89
  • coopr.pyomo/trunk/coopr/pyomo/base/constraint.py

    r2170 r2359  
    7272          elif key == "sense":
    7373             tmpsense = kwd[key]
     74          elif key == "expr":
     75             tmprule = kwd[key]
    7476          else:
    7577             raise ValueError, "Objective constructor: unknown keyword - "+key
     
    130132
    131133    def construct(self, data=None):
    132         if self.rule is not None:
     134        if self._constructed:
     135            return
     136        self._constructed=True
     137        #
     138        if isinstance(self.rule,Expression):
     139            if None in self._index or len(self._index) == 0:
     140                self._data[None].expr = self.rule
     141            else:
     142                raise IndexError, "Cannot define multiple indices in an objective with a single expression"
     143        #
     144        elif self.rule is not None:
    133145           if self._ndim==0:
    134146              tmp = self._apply_rule((self.model,),None)
     
    232244          elif key == "doc":
    233245             self.doc=kwd[key]
    234           elif key == "rule":
     246          elif key == "rule" or key == 'expr':
    235247             tmprule = kwd[key]
    236248          else:
     
    282294
    283295    def construct(self, data=None):
    284 
    285296        if pyomo.debug("verbose") or pyomo.debug("normal"):     #pragma:nocover
    286297           print "Construcing constraint "+self.name
    287298        if self.rule is None:
    288299           return
     300        if self._constructed:
     301            return
     302        self._constructed=True
    289303        #
    290304        # Local variables for code optimization
     
    292306        _self_rule = self.rule
    293307        #
    294         if not None in self._index and len(self._index) > 0 and _self_rule.func_code.co_argcount == 1:
     308        if isinstance(_self_rule,Expression):
     309            if not None in self._index:
     310                raise IndexError, "Cannot define multiple indices in a constraint with a single expression"
     311            self.add(None, _self_rule)
     312        elif not None in self._index and len(self._index) > 0 and _self_rule.func_code.co_argcount == 1:
    295313            if pyomo.debug("verbose"):                            #pragma:nocover
    296314                print "   Constructing constraint indices with rule that returns a dictionary"
  • coopr.pyomo/trunk/coopr/pyomo/base/indexed_component.py

    r2128 r2359  
    22from component import Component
    33from sets import _BaseSet, Set, set_options
    4 
    5 def set_options(**kwds):
    6     def decorator(func):
    7         func.set_options = kwds
    8         return func
    9     return decorator
    104
    115
  • coopr.pyomo/trunk/coopr/pyomo/base/param.py

    r2279 r2359  
    174174        if pyomo.debug("verbose"):      #pragma:nocover
    175175           print "Constructing Param, name="+self.name+", from data="+`data`
     176        if self._constructed:
     177            return
     178        self._constructed=True
    176179        #
    177180        # Code optimization with local variables
  • coopr.pyomo/trunk/coopr/pyomo/base/rangeset.py

    r2025 r2359  
    5656
    5757    def construct(self, values=None):
     58        if self._constructed:
     59            return
     60        self._constructed=True
    5861        if isinstance(self._start,expr.Expression):
    5962           self._start_val = self._start()
  • coopr.pyomo/trunk/coopr/pyomo/base/sets.py

    r2201 r2359  
    188188        """ Apply the rule to construct values in this set """
    189189        log.debug("Constructing _SetContainer, name="+self.name+", from data="+repr(values))
     190        if self._constructed:
     191            return
     192        self._constructed=True
    190193        #
    191194        # Construct using the values list
     
    717720        """ Apply the rule to construct values in each set"""
    718721        log.debug("Constructing _SetArray, name="+self.name+", from data="+repr(values))
     722        if self._constructed:
     723            return
     724        self._constructed=True
    719725        if self.virtual:                                #pragma:nocover
    720726           raise TypeError, "It doesn't make sense to create a virtual set array"
     
    852858        """ Apply the rule to construct values in this set """
    853859        log.debug("Constructing _ProductSet, name="+self.name+", from data="+repr(values))
     860        if self._constructed:
     861            return
     862        self._constructed=True
    854863        if self.virtual:
    855864           if len(self.set_tuple) == 0:
  • coopr.pyomo/trunk/coopr/pyomo/base/var.py

    r2129 r2359  
    237237
    238238    def construct(self, data=None):
     239        if self._constructed:
     240            return
     241        self._constructed=True
    239242        #
    240243        # Construct _VarValue() objects for all index values
  • coopr.pyomo/trunk/coopr/pyomo/components/action.py

    r2025 r2359  
    5353        """ Apply the rule to construct values in this set """
    5454        if pyomo.debug("verbose"):      #pragma:nocover
    55            print "Constructing Param, name="+self._name+", from data="+`data`
     55           print "Constructing Action, name="+self._name+", from data="+`data`
     56        if self._constructed:
     57            return
     58        self._constructed=True
    5659        #
    5760        # Update the index set dimension
  • coopr.pyomo/trunk/coopr/pyomo/components/check.py

    r2025 r2359  
    5353        """ Apply the rule to construct values in this set """
    5454        if pyomo.debug("verbose"):      #pragma:nocover
    55            print "Constructing Param, name="+self._name+", from data="+`data`
     55           print "Constructing Check, name="+self._name+", from data="+`data`
     56        if self._constructed:
     57            return
     58        self._constructed=True
    5659        #
    5760        # Update the index set dimension
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/__init__.py

    r2170 r2359  
    1010
    1111##
    12 ## NOTE: the order of these imports defines the order in which presolvers are applied
     12## NOTE: the order of these imports defines the order in which preprocessors are applied
    1313##
    1414
     
    1818#import collect_linear_terms
    1919import compute_canonical_repn
    20 import simple_presolver
     20import simple_preprocessor
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/collect_linear_terms.py

    r2201 r2359  
    7575                     # parameter values. it is currently possible to specify expressions for
    7676                     # parameter values, which is something to warn on and completely chokes
    77                      # the presolve.
     77                     # the preprocess.
    7878                     if isinstance(e.value, expr.Expression):
    7979                        value_as_string = StringIO.StringIO()
     
    157157        return y
    158158
    159     def presolve(self,model):
    160         """
    161         The main routine to perform the presolve
     159    def preprocess(self,model):
     160        """
     161        The main routine to perform the preprocess
    162162        """
    163163        #Vars = model.active_components(Var)
     
    226226                 t = C._linterm[cndx] = self._Collect3(C._data[cndx].body)
    227227              except AttributeError, msg:
    228                  print "***Error encountered in class CollectLinearTerms, method=presolve, encountered when invoking _Collect3 on constraint="+key+"["+str(cndx)+"]"
     228                 print "***Error encountered in class CollectLinearTerms, method=preprocess, encountered when invoking _Collect3 on constraint="+key+"["+str(cndx)+"]"
    229229                 constraint_as_string = StringIO.StringIO()
    230230                 C._data[cndx].body.pprint(ostream=constraint_as_string)                                 
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/compute_canonical_repn.py

    r2214 r2359  
    3535        return 10000
    3636
    37     def presolve(self, model):
     37    def preprocess(self, model):
    3838        """
    39         The main routine to perform the presolve
     39        The main routine to perform the preprocess
    4040        """
    4141        #
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/generic_varlabels.py

    r2201 r2359  
    7070        return tmp
    7171
    72     def presolve(self,model):
     72    def preprocess(self,model):
    7373        """
    74         The main routine to perform the presolve
     74        The main routine to perform the preprocess
    7575        """
    7676        #
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/identify_vars.py

    r2201 r2359  
    8484
    8585
    86     def presolve(self,model):
     86    def preprocess(self,model):
    8787        """
    88         The main routine to perform the presolve
     88        The main routine to perform the preprocess
    8989        """
    9090        Vars = model.active_components(Var)
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/numeric_varlabels.py

    r2201 r2359  
    2828        return 1000
    2929
    30     def presolve(self,model):
     30    def preprocess(self,model):
    3131        """
    32         The main routine to perform the presolve
     32        The main routine to perform the preprocess
    3333        """
    3434        for id in model._var:
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/simple_preprocessor.py

    r2350 r2359  
    1414
    1515
    16 class SimplePresolver(pyutilib.component.core.SingletonPlugin):
     16class SimplePreprocessor(pyutilib.component.core.SingletonPlugin):
    1717    """
    18     This plugin simply applies presolve actions in a fixed order
     18    This plugin simply applies preprocess actions in a fixed order
    1919    and then returns the modified model instance.
    2020    """
     
    2323
    2424    def __init__(self, **kwds):
    25         kwds['name'] = "simple_presolver"
     25        kwds['name'] = "simple_preprocessor"
    2626        pyutilib.component.core.Plugin.__init__(self, **kwds)
    2727        self.actions = pyutilib.component.core.ExtensionPoint(IPyomoPresolveAction)
     
    3838
    3939    def get_actions(self):
    40         """Return a list of presolve actions, with their ranks"""
     40        """Return a list of preprocess actions, with their ranks"""
    4141        self._initialize()
    4242        ans = []
     
    6363            del self.action_rank[action]
    6464
    65     def presolve(self,model):
     65    def preprocess(self,model):
    6666        """
    67         Apply the presolve actions to this instance, and return the revised instance.
     67        Apply the preprocess actions to this instance, and return the revised instance.
    6868        """
    6969        self._initialize()
    7070        for action in self._order_actions():
    71             model = self.actions.service(action).presolve(model)
     71            model = self.actions.service(action).preprocess(model)
    7272        return model
    7373
  • coopr.pyomo/trunk/coopr/pyomo/tests/expr/test_canonical.py

    r2214 r2359  
    1515import pyutilib.th
    1616import pyutilib.services
    17 from coopr.pyomo.presolve.simple_presolver import SimplePresolver
     17from coopr.pyomo.preprocess.simple_preprocessor import SimplePreprocessor
    1818from pyutilib.component.core import PluginGlobals
    1919
     
    4444        TestBase.setUp(self)
    4545        #PluginGlobals.pprint()
    46         self.plugin = SimplePresolver()
     46        self.plugin = SimplePreprocessor()
    4747        self.plugin.deactivate_action("compute_canonical_repn")
    4848
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_preprocess.py

    r1768 r2359  
    2424        model.y = Var(model.A)
    2525        instance=model.create()
    26         instance.presolve()
     26        instance.preprocess()
    2727        self.failUnlessEqual(instance.num_used_variables(),0)
    2828
     
    3535        model.obj = Objective(rule=lambda inst: inst.x[1])
    3636        instance=model.create()
    37         instance.presolve()
     37        instance.preprocess()
    3838        self.failUnlessEqual(instance.num_used_variables(),1)
    3939        self.failUnlessEqual(instance.x[1].label,"x[1]")
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_set.py

    r2128 r2359  
    23722372        except KeyError:
    23732373            pass
     2374        b._constructed=False
    23742375        try:
    23752376            b.construct()
Note: See TracChangeset for help on using the changeset viewer.