Changeset 3268


Ignore:
Timestamp:
Dec 1, 2010 5:08:28 PM (10 years ago)
Author:
jwatson
Message:

Fixing issues with the Piecewise construct when breakpoints and slopes are generated via rules. Works now (on a sample of size 1 - the newly added example5.py) for non-indexed rules, likely broken for indexed breakpoint/slope rules.

Location:
coopr.pyomo/trunk
Files:
1 added
1 edited

Legend:

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

    r3216 r3268  
    680680    model.C2.eval(7.0)
    681681
    682     Breakpoints refer to the value on the domain that separate the
     682    Breakpoints refer to the values on the domain that separate the
    683683    domain into distinct intervals. All intervals are assumed to be
    684684    closed on the lower bound and open on the upper bound, with the
     
    701701    would be modeled as:
    702702
    703     breakpoints = [0,1]
     703    breakpoints = [0, 1]
    704704    slopes = [2, 0, -1]
    705705    offset = 3
     
    828828        Construct the breakpoint and slope expressions
    829829        """
    830         # Construct the break points
     830
     831        if pyomo.debug("verbose"):
     832           print "Constructing Piecewise component, name="+self.name+", from args="+`args`
     833
     834        # NOTE: the _breakvals and _slopevals attributes can be either lists (if the
     835        #       component is not indexed) or list of lists (if the component is indexed).
     836
     837        # Construct the breakpoints
    831838        self._breakvals = list()
    832839        if self._breakpoints is not None:
     
    837844        else:
    838845            # Get breakpoints from a rule
    839             if self._breakpoint_component._ndim == 0:
     846            if (self._breakpoint_component is None) or (self._breakpoint_component._ndim == 0):
    840847                # Rule isn't indexed
    841848                exp = self._breakpoint_rule((self.model,))
     849                if not isinstance(exp, (list, tuple)):
     850                   raise TypeError, "A breakpoint rule is expected to return a list or a tuple - returned type was "+str(type(exp))
    842851                if exp is not None:
    843                     self._breakvals.append(exp)
     852                    self._breakvals = exp
    844853            else:
    845854                # Rule is indexed
     
    866875        else:
    867876            # Get slopes from a rule
    868             if self._slope_component._ndim == 0:
     877            if (self._slope_component is None) or (self._slope_component._ndim == 0):
    869878                # Rule isn't indexed
    870879                exp = self._slope_rule((self.model,))
     880                if not isinstance(exp, (list, tuple)):
     881                   raise TypeError, "A slope rule is expected to return a list or a tuple - returned type was "+str(type(exp))
    871882                if exp is not None:
    872                     self._slopevals.append(exp)
     883                    self._slopevals = exp
    873884            else:
    874885                # Rule is indexed
     
    885896                    if exp is not None:
    886897                        self._slopevals.append(exp)
     898
     899        if pyomo.debug("verbose"):
     900           print "Breakpoints="+str(self._breakvals)
     901           print "Slopes="+str(self._slopevals)
     902
     903        # TBD - need to modify the logic below to deal with indexed components - need to check for each index value!
    887904
    888905        # If N breakpoints are specified, then there can be N-1 or N+1
Note: See TracChangeset for help on using the changeset viewer.