0
# Domain Sets and Intervals
1
2
Predefined domain sets and interval constructors for variable bounds and parameter domains. Includes real numbers, integers, booleans, and custom intervals for comprehensive domain specification in optimization models.
3
4
## Capabilities
5
6
### Real Number Domains
7
8
Predefined sets for real number domains with various sign constraints for continuous optimization variables.
9
10
```python { .api }
11
Reals: Set
12
"""Set of all real numbers (-∞, +∞)."""
13
14
PositiveReals: Set
15
"""Set of positive real numbers (0, +∞)."""
16
17
NonPositiveReals: Set
18
"""Set of non-positive real numbers (-∞, 0]."""
19
20
NegativeReals: Set
21
"""Set of negative real numbers (-∞, 0)."""
22
23
NonNegativeReals: Set
24
"""Set of non-negative real numbers [0, +∞)."""
25
```
26
27
### Integer Domains
28
29
Predefined sets for integer domains with various sign constraints for discrete optimization variables.
30
31
```python { .api }
32
Integers: Set
33
"""Set of all integers."""
34
35
PositiveIntegers: Set
36
"""Set of positive integers (1, 2, 3, ...)."""
37
38
NonPositiveIntegers: Set
39
"""Set of non-positive integers (..., -2, -1, 0)."""
40
41
NegativeIntegers: Set
42
"""Set of negative integers (..., -3, -2, -1)."""
43
44
NonNegativeIntegers: Set
45
"""Set of non-negative integers (0, 1, 2, 3, ...)."""
46
```
47
48
### Boolean and Binary Domains
49
50
Domains for logical and binary variables in mixed-integer programming problems.
51
52
```python { .api }
53
Boolean: Set
54
"""Boolean domain {0, 1} (currently same as Binary - TODO: Convert to {True, False})."""
55
56
Binary: Set
57
"""Binary domain {0, 1}."""
58
```
59
60
### Special Domains
61
62
Special-purpose domains for specific modeling requirements and edge cases.
63
64
```python { .api }
65
Any: Set
66
"""Domain allowing any value (unconstrained)."""
67
68
AnyWithNone: Set
69
"""Domain allowing any value including None."""
70
71
EmptySet: Set
72
"""Empty set with no valid values."""
73
```
74
75
### Numeric Intervals
76
77
Predefined intervals for common numeric ranges in optimization problems.
78
79
```python { .api }
80
UnitInterval: Set
81
"""Unit interval [0, 1]."""
82
83
PercentFraction: Set
84
"""Percentage fraction interval [0, 1]."""
85
```
86
87
### Interval Constructors
88
89
Functions and classes for creating custom intervals with specified bounds for real and integer domains.
90
91
```python { .api }
92
class RealInterval(RealSet):
93
"""
94
DEPRECATED: Create real number interval.
95
96
.. deprecated:: 5.7
97
Use RangeSet(lower, upper, 0) instead.
98
99
Args:
100
lb (float, optional): Lower bound (None for -∞)
101
ub (float, optional): Upper bound (None for +∞)
102
103
Returns:
104
Set: Real interval [lb, ub]
105
"""
106
107
class IntegerInterval(IntegerSet):
108
"""
109
DEPRECATED: Create integer interval.
110
111
.. deprecated:: 5.7
112
Use RangeSet(lower, upper, 1) instead.
113
114
Args:
115
lb (int, optional): Lower bound (None for -∞)
116
ub (int, optional): Upper bound (None for +∞)
117
118
Returns:
119
Set: Integer interval [lb, ub]
120
"""
121
122
class RangeSet(Set):
123
"""
124
Create custom numeric range sets (modern replacement for interval constructors).
125
126
Args:
127
ranges: Numeric ranges or bounds
128
step: Step size (0 for continuous, 1 for integer)
129
130
Returns:
131
Set: Custom range set
132
"""
133
```
134
135
### Optimization Direction Constants
136
137
Constants for specifying optimization direction in objective functions.
138
139
```python { .api }
140
minimize: int
141
"""Minimization sense constant."""
142
143
maximize: int
144
"""Maximization sense constant."""
145
```
146
147
## Usage Examples
148
149
### Variable Domain Specification
150
151
```python
152
from pyomo.environ import *
153
154
model = ConcreteModel()
155
156
# Real variables with different domains
157
model.x1 = Var(domain=Reals) # Any real number
158
model.x2 = Var(domain=NonNegativeReals) # x >= 0
159
model.x3 = Var(domain=PositiveReals) # x > 0
160
model.x4 = Var(domain=UnitInterval) # 0 <= x <= 1
161
162
# Integer variables
163
model.y1 = Var(domain=Integers) # Any integer
164
model.y2 = Var(domain=NonNegativeIntegers) # y >= 0 integer
165
model.y3 = Var(domain=PositiveIntegers) # y > 0 integer
166
167
# Binary and boolean variables
168
model.z1 = Var(domain=Binary) # {0, 1}
169
model.z2 = Var(domain=Boolean) # {True, False}
170
```
171
172
### Custom Intervals
173
174
```python
175
from pyomo.environ import *
176
177
model = ConcreteModel()
178
179
# Custom real intervals
180
model.x1 = Var(domain=RealInterval(-10, 10)) # [-10, 10]
181
model.x2 = Var(domain=RealInterval(0, None)) # [0, +∞)
182
model.x3 = Var(domain=RealInterval(None, 100)) # (-∞, 100]
183
184
# Custom integer intervals
185
model.y1 = Var(domain=IntegerInterval(1, 100)) # {1, 2, ..., 100}
186
model.y2 = Var(domain=IntegerInterval(-5, 5)) # {-5, -4, ..., 5}
187
```
188
189
### Indexed Variables with Domains
190
191
```python
192
from pyomo.environ import *
193
194
model = ConcreteModel()
195
model.I = Set(initialize=[1, 2, 3, 4, 5])
196
197
# Different domains for different indices
198
def domain_rule(model, i):
199
if i <= 2:
200
return NonNegativeReals
201
elif i <= 4:
202
return Binary
203
else:
204
return Integers
205
206
model.x = Var(model.I, domain=domain_rule)
207
208
# Alternative: specify domain per variable
209
model.y = Var(model.I)
210
for i in model.I:
211
if i <= 2:
212
model.y[i].domain = NonNegativeReals
213
else:
214
model.y[i].domain = Binary
215
```
216
217
### Parameter Domains
218
219
```python
220
from pyomo.environ import *
221
222
model = ConcreteModel()
223
224
# Parameters with domain validation
225
model.cost = Param(domain=NonNegativeReals, initialize=10.5)
226
model.capacity = Param(domain=PositiveIntegers, initialize=100)
227
model.enabled = Param(domain=Boolean, initialize=True)
228
229
# Parameters with custom intervals
230
model.temperature = Param(domain=RealInterval(-273.15, None)) # Above absolute zero
231
model.priority = Param(domain=IntegerInterval(1, 10)) # Priority scale 1-10
232
```
233
234
### Domain Validation Example
235
236
```python
237
from pyomo.environ import *
238
239
model = ConcreteModel()
240
241
# Create variable with domain constraint
242
model.x = Var(domain=UnitInterval)
243
244
# This will work
245
model.x.set_value(0.5)
246
247
# This would raise an error due to domain violation
248
try:
249
model.x.set_value(1.5) # Outside [0,1]
250
except ValueError as e:
251
print(f"Domain violation: {e}")
252
253
# Check if value is in domain
254
print(f"0.5 in UnitInterval: {0.5 in UnitInterval}")
255
print(f"1.5 in UnitInterval: {1.5 in UnitInterval}")
256
```
257
258
### Optimization Sense Usage
259
260
```python
261
from pyomo.environ import *
262
263
model = ConcreteModel()
264
model.x = Var(domain=NonNegativeReals)
265
model.y = Var(domain=NonNegativeReals)
266
267
# Minimization objective
268
model.min_obj = Objective(
269
expr=model.x + model.y,
270
sense=minimize
271
)
272
273
# Maximization objective (deactivate first objective)
274
model.min_obj.deactivate()
275
model.max_obj = Objective(
276
expr=model.x + model.y,
277
sense=maximize
278
)
279
```
280
281
### Domain Checking and Validation
282
283
```python
284
from pyomo.environ import *
285
286
# Check domain membership
287
values_to_check = [-1, 0, 0.5, 1, 2]
288
289
for val in values_to_check:
290
print(f"Value {val}:")
291
print(f" In NonNegativeReals: {val in NonNegativeReals}")
292
print(f" In UnitInterval: {val in UnitInterval}")
293
print(f" In PositiveReals: {val in PositiveReals}")
294
print(f" In Binary: {val in Binary}")
295
```