source: trunk/coopr/pyomo/base/rangeset.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 11 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 2.7 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__ = ['RangeSet'] 
12
13#import pyutilib.plugin.core
14import sets
15import expr
16from numvalue import value
17from coopr.pyomo.base.plugin import *
18
19class RangeSetValidator(object):
20
21    def __init__(self, start, end, step):
22        self.start=start
23        self.end=end
24        self.step=step
25
26    def __call__(self,val,model):
27       if val < self.start:
28          return False
29       if val > self.end:
30          return False
31       if (val-self.start)%self.step != 0:
32          return False
33       return True
34
35class RangeSet(sets._SetContainer):
36    """A set that represents a list of integer values"""
37
38    def __init__(self,*args,**kwds):
39        """Construct a list of integers"""
40        tmp=()
41        sets._SetContainer.__init__(self,*tmp,**kwds)
42        self.ordered=True
43        if len(args) == 1:
44           self._start=1
45           self._end=args[0]
46           self._step=1
47        elif len(args) == 2:
48           self._start=args[0]
49           self._end=args[1]
50           self._step=1
51        else:
52           self._start=args[0]
53           self._end=args[1]
54           self._step=args[2]
55
56    def _construct(self, model=None, values=None):
57        if isinstance(self._start,expr.Expression):
58           self._start_val = self._start()
59        else:
60           self._start_val = value(self._start)
61
62        if isinstance(self._end,expr.Expression):
63           self._end_val = self._end()
64        else:
65           self._end_val = value(self._end)
66
67        if isinstance(self._step,expr.Expression):
68           self._step_val = self._step()
69        else:
70           self._step_val = value(self._step)
71
72        if self.validate is None:
73           self.validate=RangeSetValidator(self._start_val, self._end_val, self._step_val)
74
75        #
76        # Generate set, and track the bounds
77        #
78        first=self._start_val
79        last=self._start_val
80        for val in range(self._start_val,self._end_val+1,self._step_val):
81          if self._verify(val,False):
82             last=val
83             self.add(val)
84        self._bounds=(first,last)
85
86ComponentRegistration("RangeSet", RangeSet, "A sequence of numeric values.  RangeSet(start,end,step) is a sequence starting a value 'start', and increasing in values by 'step' until a value greater than of equal to 'end' is reached.")
Note: See TracBrowser for help on using the repository browser.