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

Last change on this file since 2005 was 2005, checked in by wehart, 10 years ago

Adding tests to exercise the Pyomo command-line.

Reworked error management a bit.
Updated output of virtual sets.

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