0
# Variables and Constraints
1
2
Comprehensive variable types and constraint system for mathematical programming models. PySCIPOpt supports continuous, integer, and binary variables along with extensive constraint types including linear constraints, special ordered sets, logical constraints, and advanced constraint forms.
3
4
## Capabilities
5
6
### Variable Types and Properties
7
8
Create variables with different types, bounds, and properties for various optimization modeling needs.
9
10
```python { .api }
11
def addVar(self, name='', vtype='C', lb=0.0, ub=None, obj=0.0,
12
pricedVar=False, pricedVarScore=1.0):
13
"""
14
Add a variable to the model.
15
16
Parameters:
17
- name (str): Variable name for identification
18
- vtype (str): Variable type
19
'C': Continuous variable (default)
20
'I': Integer variable
21
'B': Binary variable (automatically sets bounds to [0,1])
22
- lb (float): Lower bound (default: 0.0)
23
- ub (float): Upper bound (default: infinity for continuous/integer, 1 for binary)
24
- obj (float): Objective coefficient (default: 0.0)
25
- pricedVar (bool): Mark as priced variable for column generation
26
- pricedVarScore (float): Priority score for column generation
27
28
Returns:
29
Variable: Variable object that can be used in expressions and constraints
30
"""
31
```
32
33
### Linear Constraints
34
35
Add linear constraints using expressions, inequalities, and equalities.
36
37
```python { .api }
38
def addCons(self, cons, name='', initial=True, separate=True,
39
enforce=True, check=True, propagate=True, local=False,
40
modifiable=False, dynamic=False, removable=False,
41
stickingatnode=False):
42
"""
43
Add a linear constraint to the model.
44
45
Parameters:
46
- cons: Constraint expression (Expr <= rhs, Expr >= lhs, lhs <= Expr <= rhs)
47
- name (str): Constraint name
48
- initial (bool): Include in initial LP relaxation
49
- separate (bool): Subject to cutting plane separation
50
- enforce (bool): Enforce during node processing
51
- check (bool): Check for feasibility
52
- propagate (bool): Subject to constraint propagation
53
- local (bool): Valid only in current subtree
54
- modifiable (bool): Can be modified during solving
55
- dynamic (bool): Can be added during solving
56
- removable (bool): Can be removed during solving
57
- stickingatnode (bool): Stay at node even if removable
58
59
Returns:
60
Constraint: Linear constraint object
61
"""
62
63
def addConss(self, conss, **kwargs):
64
"""
65
Add multiple constraints at once.
66
67
Parameters:
68
- conss (list): List of constraint expressions
69
- **kwargs: Constraint properties (same as addCons)
70
71
Returns:
72
list: List of Constraint objects
73
"""
74
75
def addConsCoeff(self, cons, var, coeff):
76
"""
77
Add coefficient to existing linear constraint.
78
79
Parameters:
80
- cons (Constraint): Existing constraint to modify
81
- var (Variable): Variable to add
82
- coeff (float): Coefficient value
83
"""
84
```
85
86
### Special Ordered Sets (SOS)
87
88
Special ordered set constraints for modeling piecewise linear functions and logical relationships.
89
90
```python { .api }
91
def addConsSOS1(self, vars, weights=None, name="SOS1cons",
92
initial=True, separate=True, enforce=True,
93
check=True, propagate=True, local=False,
94
dynamic=False, removable=False, stickingatnode=False):
95
"""
96
Add SOS1 (Special Ordered Set of type 1) constraint.
97
At most one variable in the set can be non-zero.
98
99
Parameters:
100
- vars (list): List of variables in the SOS1 set
101
- weights (list): Priority weights for variables (default: 1, 2, 3, ...)
102
- name (str): Constraint name
103
- Other parameters: Same as addCons
104
105
Returns:
106
Constraint: SOS1 constraint object
107
"""
108
109
def addConsSOS2(self, vars, weights=None, name="SOS2cons",
110
initial=True, separate=True, enforce=True,
111
check=True, propagate=True, local=False,
112
dynamic=False, removable=False, stickingatnode=False):
113
"""
114
Add SOS2 (Special Ordered Set of type 2) constraint.
115
At most two adjacent variables in the ordered set can be non-zero.
116
117
Parameters:
118
- vars (list): List of variables in the SOS2 set (in order)
119
- weights (list): Priority weights defining order (default: 1, 2, 3, ...)
120
- name (str): Constraint name
121
- Other parameters: Same as addCons
122
123
Returns:
124
Constraint: SOS2 constraint object
125
"""
126
```
127
128
### Logical Constraints
129
130
Boolean logic constraints for modeling logical relationships between binary variables.
131
132
```python { .api }
133
def addConsAnd(self, vars, resvar, name="ANDcons",
134
initial=True, separate=True, enforce=True,
135
check=True, propagate=True, local=False,
136
modifiable=False, dynamic=False, removable=False,
137
stickingatnode=False):
138
"""
139
Add AND constraint: resvar = var1 AND var2 AND ... AND varN
140
141
Parameters:
142
- vars (list): List of binary input variables
143
- resvar (Variable): Binary result variable
144
- name (str): Constraint name
145
- Other parameters: Same as addCons
146
147
Returns:
148
Constraint: AND constraint object
149
"""
150
151
def addConsOr(self, vars, resvar, name="ORcons",
152
initial=True, separate=True, enforce=True,
153
check=True, propagate=True, local=False,
154
modifiable=False, dynamic=False, removable=False,
155
stickingatnode=False):
156
"""
157
Add OR constraint: resvar = var1 OR var2 OR ... OR varN
158
159
Parameters:
160
- vars (list): List of binary input variables
161
- resvar (Variable): Binary result variable
162
- name (str): Constraint name
163
- Other parameters: Same as addCons
164
165
Returns:
166
Constraint: OR constraint object
167
"""
168
169
def addConsXor(self, vars, rhsvar, name="XORcons",
170
initial=True, separate=True, enforce=True,
171
check=True, propagate=True, local=False,
172
modifiable=False, dynamic=False, removable=False,
173
stickingatnode=False):
174
"""
175
Add XOR constraint: rhsvar = var1 XOR var2 XOR ... XOR varN
176
177
Parameters:
178
- vars (list): List of binary variables
179
- rhsvar (Variable): Binary result variable (1 if odd number of vars are 1)
180
- name (str): Constraint name
181
- Other parameters: Same as addCons
182
183
Returns:
184
Constraint: XOR constraint object
185
"""
186
```
187
188
### Cardinality Constraints
189
190
Constraints limiting the number of non-zero variables in a set.
191
192
```python { .api }
193
def addConsCardinality(self, consvars, cardval, indvars=None, weights=None,
194
name="CARDcons", initial=True, separate=True,
195
enforce=True, check=True, propagate=True, local=False,
196
dynamic=False, removable=False, stickingatnode=False):
197
"""
198
Add cardinality constraint limiting number of non-zero variables.
199
200
Parameters:
201
- consvars (list): Variables subject to cardinality constraint
202
- cardval (int): Maximum number of variables that can be non-zero
203
- indvars (list): Binary indicator variables (optional)
204
- weights (list): Priority weights for variables (optional)
205
- name (str): Constraint name
206
- Other parameters: Same as addCons
207
208
Returns:
209
Constraint: Cardinality constraint object
210
"""
211
```
212
213
### Indicator Constraints
214
215
Constraints that are only active when a binary variable takes a specific value.
216
217
```python { .api }
218
def addConsIndicator(self, cons, binvar=None, name="INDcons",
219
initial=True, separate=True, enforce=True,
220
check=True, propagate=True, local=False,
221
dynamic=False, removable=False, stickingatnode=False):
222
"""
223
Add indicator constraint: binvar = 1 => cons holds
224
225
Parameters:
226
- cons: Linear constraint that is activated by indicator
227
- binvar (Variable): Binary indicator variable (created if None)
228
- name (str): Constraint name
229
- Other parameters: Same as addCons
230
231
Returns:
232
Constraint: Indicator constraint object
233
"""
234
```
235
236
### Variable and Constraint Information
237
238
Access properties and information about variables and constraints.
239
240
```python { .api }
241
def getVars(self):
242
"""
243
Get all variables in the model.
244
245
Returns:
246
list: List of all Variable objects
247
"""
248
249
def getConss(self):
250
"""
251
Get all constraints in the model.
252
253
Returns:
254
list: List of all Constraint objects
255
"""
256
257
def getNVars(self):
258
"""
259
Get number of variables in the model.
260
261
Returns:
262
int: Number of variables
263
"""
264
265
def getNConss(self):
266
"""
267
Get number of constraints in the model.
268
269
Returns:
270
int: Number of constraints
271
"""
272
```
273
274
## Usage Examples
275
276
### Variable Types
277
278
```python
279
from pyscipopt import Model
280
281
model = Model("variable_types")
282
283
# Continuous variable with bounds
284
x_cont = model.addVar(name="x_continuous", vtype="C", lb=0.0, ub=10.5)
285
286
# Integer variable
287
x_int = model.addVar(name="x_integer", vtype="I", lb=-5, ub=20)
288
289
# Binary variable (bounds automatically set to [0,1])
290
x_bin = model.addVar(name="x_binary", vtype="B")
291
292
# Variable with objective coefficient
293
x_obj = model.addVar(name="x_with_obj", obj=2.5, lb=0)
294
295
print(f"Created {model.getNVars()} variables")
296
```
297
298
### Linear Constraints
299
300
```python
301
from pyscipopt import Model
302
303
model = Model("linear_constraints")
304
305
# Create variables
306
x = model.addVar(name="x", lb=0)
307
y = model.addVar(name="y", lb=0)
308
z = model.addVar(name="z", lb=0)
309
310
# Inequality constraints
311
model.addCons(2*x + 3*y <= 10, name="resource_limit")
312
model.addCons(x + y >= 2, name="minimum_requirement")
313
314
# Equality constraint
315
model.addCons(x + 2*y + z == 5, name="balance_constraint")
316
317
# Range constraint (double-bounded)
318
model.addCons(1 <= x + y <= 3, name="range_constraint")
319
320
print(f"Created {model.getNConss()} constraints")
321
```
322
323
### Special Ordered Sets
324
325
```python
326
from pyscipopt import Model
327
328
model = Model("sos_example")
329
330
# Variables for piecewise linear function
331
x1 = model.addVar(name="x1", lb=0, ub=1)
332
x2 = model.addVar(name="x2", lb=0, ub=1)
333
x3 = model.addVar(name="x3", lb=0, ub=1)
334
x4 = model.addVar(name="x4", lb=0, ub=1)
335
336
# SOS1: At most one variable can be non-zero
337
model.addConsSOS1([x1, x2, x3, x4], name="sos1_constraint")
338
339
# Variables for piecewise linear with adjacent points
340
y1 = model.addVar(name="y1", lb=0, ub=1)
341
y2 = model.addVar(name="y2", lb=0, ub=1)
342
y3 = model.addVar(name="y3", lb=0, ub=1)
343
344
# SOS2: At most two adjacent variables can be non-zero
345
model.addConsSOS2([y1, y2, y3], weights=[1, 2, 3], name="sos2_constraint")
346
```
347
348
### Logical Constraints
349
350
```python
351
from pyscipopt import Model
352
353
model = Model("logical_constraints")
354
355
# Binary variables
356
a = model.addVar(name="a", vtype="B")
357
b = model.addVar(name="b", vtype="B")
358
c = model.addVar(name="c", vtype="B")
359
result = model.addVar(name="result", vtype="B")
360
361
# AND constraint: result = a AND b AND c
362
model.addConsAnd([a, b, c], result, name="and_constraint")
363
364
# OR constraint: result = a OR b OR c
365
or_result = model.addVar(name="or_result", vtype="B")
366
model.addConsOr([a, b, c], or_result, name="or_constraint")
367
368
# XOR constraint: result = a XOR b XOR c
369
xor_result = model.addVar(name="xor_result", vtype="B")
370
model.addConsXor([a, b, c], xor_result, name="xor_constraint")
371
```
372
373
### Indicator Constraints
374
375
```python
376
from pyscipopt import Model
377
378
model = Model("indicator_example")
379
380
# Variables
381
x = model.addVar(name="x", lb=0, ub=10)
382
y = model.addVar(name="y", lb=0, ub=10)
383
indicator = model.addVar(name="indicator", vtype="B")
384
385
# Indicator constraint: if indicator = 1, then x + y <= 5
386
constraint_expr = x + y <= 5
387
model.addConsIndicator(constraint_expr, indicator, name="indicator_cons")
388
389
# Alternative: let the system create the indicator variable
390
auto_indicator_cons = model.addConsIndicator(x + 2*y >= 3, name="auto_indicator")
391
```
392
393
### Cardinality Constraints
394
395
```python
396
from pyscipopt import Model
397
398
model = Model("cardinality_example")
399
400
# Create variables for portfolio selection
401
stocks = []
402
for i in range(10):
403
stock = model.addVar(name=f"stock_{i}", lb=0, ub=1)
404
stocks.append(stock)
405
406
# Cardinality constraint: select at most 3 stocks
407
model.addConsCardinality(stocks, 3, name="max_3_stocks")
408
409
# With weights to prioritize certain stocks
410
weights = [i+1 for i in range(10)] # Higher weight = higher priority
411
model.addConsCardinality(stocks, 5, weights=weights, name="weighted_selection")
412
```