You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _sources/constraint.md.txt
+38-5Lines changed: 38 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,16 @@ from pyoptinterface import copt
25
25
model = copt.Model()
26
26
```
27
27
28
+
## Constraint Sense
29
+
30
+
The sense of a constraint can be one of the following:
31
+
32
+
- `poi.Eq`: equal
33
+
- `poi.Leq`: less than or equal
34
+
- `poi.Geq`: greater than or equal
35
+
36
+
They are the abbreviations of `poi.ConstraintSense.Equal`, `poi.ConstraintSense.LessEqual` or `poi.ConstraintSense.GreaterEqual` and can be used in the `sense` argument of the constraint creation functions.
37
+
28
38
## Linear Constraint
29
39
It is defined as:
30
40
@@ -42,16 +52,15 @@ It can be added to the model using the `add_linear_constraint` method of the `Mo
42
52
x = model.add_variable(name="x")
43
53
y = model.add_variable(name="y")
44
54
45
-
con = model.add_linear_constraint(2.0*x + 3.0*y, poi.ConstraintSense.LessEqual, 1.0)
55
+
con = model.add_linear_constraint(2.0*x + 3.0*y, poi.Leq, 1.0)
# modify the coefficient of the linear part of the constraint
236
246
model.set_normalized_coefficient(con, x, 2.0)
237
247
```
248
+
249
+
## Create constraint with comparison operator
250
+
251
+
In other modeling languages, we can create a constraint with a comparison operator, like:
252
+
253
+
```python
254
+
model.addConsr(x + y <= 1)
255
+
```
256
+
257
+
This is quite convenient, so PyOptInterface now supports to create constraint with comparison operators `<=`, `==`, `>=` as a shortcut to create a linear or quadratic constraint.
258
+
259
+
```{code-cell}
260
+
model.add_linear_constraint(x + y <= 1)
261
+
model.add_linear_constraint(x <= y)
262
+
model.add_quadratic_constraint(x*x + y*y <= 1)
263
+
```
264
+
265
+
:::{note}
266
+
267
+
Creating constraint with comparison operator may cause performance issue especially the left-hand side and right-hand side of the constraint are complex expressions. PyOptInterface needs to create a new expression by subtracting the right-hand side from the left-hand side, which may be time-consuming.
268
+
269
+
If that becomes the bottleneck of performance, it is recommended to construct the left-hand side expression with `ExprBuilder` and call `add_linear_constraint` or `add_quadratic_constraint` method to create constraints explicitly.
Copy file name to clipboardExpand all lines: _sources/getting_started.md.txt
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -245,7 +245,7 @@ con = model.add_linear_constraint(x1+x2, poi.ConstraintSense.Equal, 1, name="con
245
245
```
246
246
`model.add_linear_constraint` adds a linear constraint to the model.
247
247
- The first argument `x1+x2` is the left-hand side of the constraint.
248
-
- The second argument is the sense of the constraint. It can be `poi.ConstraintSense.Equal`, `poi.ConstraintSense.LessEqual` or `poi.ConstraintSense.GreaterEqual`.
248
+
- The second argument is the sense of the constraint. It can be `poi.ConstraintSense.Equal`, `poi.ConstraintSense.LessEqual` or `poi.ConstraintSense.GreaterEqual` which can also be written as `poi.Eq`, `poi.Leq`, and `poi.Geq`.
249
249
- The third argument is the right-hand side of the constraint. It must be a constant.
250
250
- The fourth argument is optional and can be used to specify the name of the constraint.
0 commit comments