source: trunk/coopr/pyomo/base/set_types.py @ 1455

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

Misc edits to make Pyomo models pickle-able.

File size: 5.2 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__ = ['_VirtualSet', '_AnySet', 'RealSet', 'IntegerSet', 'BooleanSet', 'Any', 'Reals', 'PositiveReals', 'NonPositiveReals', 'NegativeReals', 'NonNegativeReals', 'PercentFraction', 'Integers', 'PositiveIntegers', 'NonPositiveIntegers', 'NegativeIntegers', 'NonNegativeIntegers', 'Boolean', 'Binary']
12
13import pyutilib.plugin
14import sets
15from coopr.pyomo.base.plugin import *
16
17
18class _VirtualSet(sets._SetContainer, pyutilib.plugin.Plugin):
19    """
20    A set that does not contain elements, but instead overrides the
21       __contains__ method to define set membership.
22    """
23
24    pyutilib.plugin.implements(IPyomoSet)
25
26    def __init__(self,*args,**kwds):
27        if "name" in kwds:
28            pyutilib.plugin.Plugin.__init__(self,name=kwds["name"])
29        else:
30            pyutilib.plugin.Plugin.__init__(self)
31        self._class_override=False
32        sets._SetContainer.__init__(self,*args,**kwds)
33        self.virtual=True
34
35
36class _AnySet(_VirtualSet):
37    """A virutal set that allows any value"""
38
39    def __init__(self,*args,**kwds):
40        """Constructor"""
41        _VirtualSet.__init__(self,*args,**kwds)
42
43    def __contains__(self, element):
44        if element is None:
45            return False
46        return True
47
48
49class RealSet(_VirtualSet):
50    """A virutal set that represents real values"""
51
52    def __init__(self,*args,**kwds):
53        """Constructor"""
54        if not 'bounds' in kwds:
55            kwds['bounds'] = (None,None)
56        _VirtualSet.__init__(self,*args,**kwds)
57
58   
59    def __contains__(self, element):
60        """Report whether an element is an 'int', 'long' or 'float' value.
61
62        (Called in response to the expression 'element in self'.)
63        """
64        return _VirtualSet.__contains__(self,element) and (type(element) in (float,int,long))
65
66
67class IntegerSet(_VirtualSet):
68    """A set that represents integer values"""
69
70    def __init__(self,*args,**kwds):
71        """Constructor"""
72        if not 'bounds' in kwds:
73            kwds['bounds'] = (None,None)
74        _VirtualSet.__init__(self,*args,**kwds)
75
76    def __contains__(self, element):
77        """Report whether an element is an 'int'.
78
79        (Called in response to the expression 'element in self'.)
80        """
81        return _VirtualSet.__contains__(self,element) and (type(element) in (int,))
82
83
84class BooleanSet(_VirtualSet):
85    """A set that represents boolean values"""
86
87    def __init__(self,*args,**kwds):
88        """Construct the set of booleans, which contains no explicit values"""
89        kwds['bounds'] = (0,1)
90        _VirtualSet.__init__(self,*args,**kwds)
91
92    def __contains__(self, element):
93        """Report whether an element is a boolean.
94
95        (Called in response to the expression 'element in self'.)
96        """
97        return _VirtualSet.__contains__(self,element) and (type(element) in (bool,int)) and (element in (0,1,True,False))
98
99
100#
101# Concrete instances of the standard sets
102#
103Any=_AnySet(name="Any", doc="A set of any data")
104
105Reals=RealSet(name="Reals", doc="A set of real values")
106def validate_PositiveValues(x,model):
107    return x>0
108PositiveReals=RealSet(name="PositiveReals",validate=validate_PositiveValues, doc="A set of positive real values", bounds=(0,None))
109def validate_NonPositiveValues(x,model):
110    return x<=0
111NonPositiveReals=RealSet(name="NonPositiveReals",validate=validate_NonPositiveValues, doc="A set of non-positive real values", bounds=(None,0))
112def validate_NegativeValues(x,model):
113    return x<0
114NegativeReals=RealSet(name="NegativeReals",validate=validate_NegativeValues, doc="A set of negative real values", bounds=(None,0))
115def validate_NonNegativeValues(x,model):
116    return x>=0
117NonNegativeReals=RealSet(name="NonNegativeReals",validate=validate_NonNegativeValues, doc="A set of non-negative real values", bounds=(0,None))
118
119def validate_PercentFraction(x,model):
120    return x>=0 and x<=1.0
121PercentFraction=RealSet(name="PercentFraction",validate=validate_PercentFraction, doc="A set of real values in the interval [0,1]", bounds=(0.0,1.0))
122
123Integers=IntegerSet(name="Integers", doc="A set of integer values")
124PositiveIntegers=IntegerSet(name="PositiveIntegers",validate=validate_PositiveValues, doc="A set of positive integer values", bounds=(1,None))
125NonPositiveIntegers=IntegerSet(name="NonPositiveIntegers",validate=validate_NonPositiveValues, doc="A set of non-positive integer values", bounds=(None,0))
126NegativeIntegers=IntegerSet(name="NegativeIntegers",validate=validate_NegativeValues, doc="A set of negative integer values", bounds=(None,-1))
127NonNegativeIntegers=IntegerSet(name="NonNegativeIntegers",validate=validate_NonNegativeValues, doc="A set of non-negative integer values", bounds=(0,None))
128
129Boolean=BooleanSet(name="Boolean", doc="A set of boolean values")
130Binary=BooleanSet(name="Binary", doc="A set of boolean values")
131
Note: See TracBrowser for help on using the repository browser.