Changeset 2653


Ignore:
Timestamp:
Jun 10, 2010 12:03:46 PM (11 years ago)
Author:
prsteel
Message:

Added new abstract base class ConstraintBase?.

ConstraintBase? will serve as a base class for all constraint-like objects.
Currently only Constraint exists; however, this change is in preparation
for SOS1, SOS2, and SOS-n constraint classes, among others.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/constraint.py

    r2651 r2653  
    2424import sys
    2525from set_types import *
     26from component import Component
    2627
    2728
     
    218219
    219220
     221class ConstraintBase(Component):
     222    """
     223    Abstract base class for all constaint types. Keeps track of how many
     224    constaint objects are in existence.
     225    """
     226
     227    alias("ConstraintBase", "Abstract base class for all model constraints")
     228
     229    # The number of explicit constraints declared.
     230    _nExplicitConstraints = 0
     231
     232    def __init__(self, *args, **kwargs):
     233        ctype = kwargs.pop("ctype", None)
     234        if ctype is None:
     235            raise ValueError, "Keyword argument 'ctype' must not be " + \
     236                  "'None'; must specify a class for the component type"
     237        Component.__init__(self, ctype)
     238
     239        tmpname  = kwargs.pop('name', 'unknown')
     240        self.doc = kwargs.pop('doc', None )
     241
     242        # Increment the class count
     243        ConstraintBase._inc_count()
     244
     245    def __del__(self):
     246        """ Remove this constraint from the count """
     247        self._inc_count(-1)
     248
     249    @classmethod
     250    def _inc_count(cls, n=1):
     251        """ Alter the constraint count """
     252        cls._nExplicitConstraints += n
     253
     254    @classmethod
     255    def num_explicit_constraints(cls):
     256        return cls._nExplicitConstraints
     257
     258
    220259class ConstraintData(NumericValue):
    221260
     
    244283
    245284
    246 class Constraint(NumericValue, IndexedComponent):
     285class Constraint(NumericValue, IndexedComponent, ConstraintBase):
    247286    """An object that defines a objective expression"""
    248287
     
    259298           doc:  documentation string for constraint
    260299        """
    261         tkwd = { 'ctype' : Constraint }
     300       
     301        # See if 'ctype' was passed as a keyword argument;
     302        # this allows derived classses to alert Pyomo to
     303        # their existence through super() calls. If not,
     304        # pass Constraint
     305        tkwd = { 'ctype' : kwargs.pop('ctype', Constraint) }
    262306        IndexedComponent.__init__(self, *args, **tkwd)
    263307
     308        # Pass some arguments to ConstraintBase
     309        tkwd['doc'] = kwargs.pop('doc', None)
     310        tkwd['name'] = kwargs.get('name', 'unknown')
     311
     312        # Call constraint base class constructor
     313        ConstraintBase.__init__(self, *args, **tkwd)
     314       
    264315        tmpname  = kwargs.pop('name', 'unknown')
    265316        tmprule  = kwargs.pop('rule', None )
    266317        tmprule  = kwargs.pop('expr', tmprule )
    267         self.doc = kwargs.pop('doc', None )
     318
    268319        self._no_rule_init = kwargs.pop('noruleinit', None )
    269320
Note: See TracChangeset for help on using the changeset viewer.