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
+48-23Lines changed: 48 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,31 @@ add a linear constraint to the model
71
71
PyOptInterface provides <project:#pyoptinterface.Eq>, <project:#pyoptinterface.Leq>, and <project:#pyoptinterface.Geq> as alias of <project:#pyoptinterface.ConstraintSense> to represent the sense of the constraint with a shorter name.
72
72
:::
73
73
74
+
The linear constraint can also be created with a comparison operator, like `<=`, `==`, or `>=`:
75
+
76
+
```{code-cell}
77
+
model.add_linear_constraint(2.0*x + 3.0*y <= 1.0)
78
+
model.add_linear_constraint(2.0*x + 3.0*y == 1.0)
79
+
model.add_linear_constraint(2.0*x + 3.0*y >= 1.0)
80
+
```
81
+
82
+
If you want to express a two-sided linear constraint, you can use the `add_linear_constraint` method with a tuple to represent the left-hand side and right-hand side of the constraint like:
The two-sided linear constraint is not implemented for Gurobi because of its [special handling of range constraints](https://docs.gurobi.com/projects/optimizer/en/12.0/reference/c/model.html#c.GRBaddrangeconstr).
97
+
:::
98
+
74
99
## Quadratic Constraint
75
100
Like the linear constraint, it is defined as:
76
101
@@ -103,6 +128,29 @@ add a quadratic constraint to the model
103
128
:return: the handle of the constraint
104
129
```
105
130
131
+
Similarly, the quadratic constraint can also be created with a comparison operator, like `<=`, `==`, or `>=`:
Some solvers like COPT (as of 7.2.9) only supports convex quadratic constraints, which means the quadratic term must be positive semidefinite. If you try to add a non-convex quadratic constraint, an exception will be raised.
142
+
:::
143
+
144
+
The two-sided quadratic constraint can also be created with a tuple to represent the left-hand side and right-hand side of the constraint like:
# modify the coefficient of the linear part of the constraint
246
294
model.set_normalized_coefficient(con, x, 2.0)
247
295
```
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.
Finally, we add the objective function. We want to maximize the altitude at the final time, so we set the objective function to be the negative of the altitude at the final time.
105
112
106
113
```{code-cell}
107
-
model.set_objective(-h[T-1])
114
+
model.set_objective(-h[-1])
108
115
```
109
116
110
117
After solving the problem, we can plot the results.
0 commit comments