Changeset 3276


Ignore:
Timestamp:
Dec 3, 2010 4:06:40 PM (10 years ago)
Author:
jwatson
Message:

Some progress toward functional indexed Piecewise components.

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

Legend:

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

    r3274 r3276  
    6767
    6868        self.fixed = False
    69 
    7069        self.status = VarStatus.undefined
    7170
     
    742741
    743742        # We can either get explicit breakpoints and slopes, or rules
    744         # defining them
     743        # defining them.
    745744        self._breakpoints = kwargs.pop('breakpoints', None)
    746745        self._slopes = kwargs.pop('slopes', None)
     
    754753        self._forcesos2 = kwargs.pop('forcesos2', False)
    755754
    756         # Domain variable
     755        # The corresponding domain variable.
    757756        self.variable = kwargs.pop('value', None)
    758757
    759         # y-intercept
     758        # The y-intercept of the entire piecewise construct.
    760759        self.offset = kwargs.pop('offset', 0)
    761760
     
    766765        # Error checking
    767766
    768         # Make sure a domain variable was passed
     767        # Make sure a domain variable was passed.
    769768        if self.variable is None:
    770769            raise TypeError, "Specify a Variable object for the 'value' " \
     
    898897
    899898        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!
    904 
    905         # If N breakpoints are specified, then there can be N-1 or N+1
    906         # slopes
    907         if abs(len(self._slopevals) - len(self._breakvals)) != 1:
    908             msg = "Error constructing Piecewise object '%(name)s'; "          \
    909                   "%(breaks)i breakpoints and %(slopes)i slopes were "        \
    910                   "specified.\n\tGiven %(breaks)i breakpoints, there should " \
    911                   "be %(breaks)i-1 or %(breaks)i+1 slopes."
    912             raise ValueError, msg % {
    913               "breaks" : len(self._breakvals),
    914               "slopes" : len(self._slopevals),
    915               "name"   : self.name
    916             }
    917 
    918         # If they have specified an unbounded problem, add "None" to both
    919         # ends of self._breakvals
    920         if len(self._breakvals) < len(self._slopevals):
    921             self._breakvals.insert(0, None)
    922             self._breakvals.append(None)
     899           if (self._slope_component is None) or (self._slope_component._ndim == 0):
     900              print "Slopes="+str(self._slopevals)
     901           else:
     902              i = 0
     903              for index in self._slope_component._index:
     904                 print "Slope["+str(index)+"]="+str(self._slopevals[i])
     905                 i += 1
     906
     907           if (self._breakpoint_component is None) or (self._breakpoint_component._ndim == 0):
     908              print "Breakpoints="+str(self._slopevals)
     909           else:
     910              i = 0
     911              for index in self._breakpoint_component._index:
     912                 print "Breakpoint["+str(index)+"]="+str(self._breakvals[i])
     913                 i += 1
     914
     915        # If N breakpoints are specified, then there can be N-1 or N+1 slopes.
     916        if (self._breakpoint_component is None) or (self._breakpoint_component._ndim == 0):
     917           if abs(len(self._slopevals) - len(self._breakvals)) != 1:
     918               msg = "Error constructing Piecewise object '%(name)s'; "          \
     919                     "%(breaks)i breakpoints and %(slopes)i slopes were "        \
     920                     "specified.\n\tGiven %(breaks)i breakpoints, there should " \
     921                     "be %(breaks)i-1 or %(breaks)i+1 slopes."
     922               raise ValueError, msg % {
     923                 "breaks" : len(self._breakvals),
     924                 "slopes" : len(self._slopevals),
     925                 "name"   : self.name
     926               }
     927        else:
     928           for i in range(0,len(self._breakvals)):
     929              if abs(len(self._slopevals[i]) - len(self._breakvals[i])) != 1:
     930                  msg = "Error constructing Piecewise object '%(name)s'; "          \
     931                        "%(breaks)i breakpoints and %(slopes)i slopes were "        \
     932                        "specified.\n\tGiven %(breaks)i breakpoints, there should " \
     933                        "be %(breaks)i-1 or %(breaks)i+1 slopes."
     934                  raise ValueError, msg % {
     935                    "breaks" : len(self._breakvals[i]),
     936                    "slopes" : len(self._slopevals[i]),
     937                    "name"   : self.name
     938                  }
     939
     940        # If a user specified an unbounded problem, add "None" to both ends of self._breakvals.
     941        if (self._breakpoint_component is None) or (self._breakpoint_component._ndim == 0):
     942           if len(self._breakvals) < len(self._slopevals):
     943              self._breakvals.insert(0, None)
     944              self._breakvals.append(None)
     945        else:
     946           for i in range(0,len(self._breakvals)):
     947              if len(self._breakvals[i]) < len(self._slopevals[i]):
     948                 self._breakvals[i].insert(0, None)
     949                 self._breakvals[i].append(None)             
    923950
    924951        self._generate_constraints()
Note: See TracChangeset for help on using the changeset viewer.