Skip to content

Commit

Permalink
Doc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metab0t committed Mar 2, 2024
1 parent b72fcf0 commit 40c341a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/source/constraint.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ For linear constraints, we can modify the coefficients of the linear part of the
calling the `set_normalized_coefficient` method of the model.

```python
con = model.add_linear_constraint(x + y, poi.ConstraintSense.LessEqual, 1.0)
con = model.add_linear_constraint(x + y, poi.Leq, 1.0)

# modify the right-hand side of the constraint
model.set_normalized_rhs(con, 2.0)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/economic_dispatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Then, we need to add the quadratic objective function to the model. We will use
obj = poi.ExprBuilder()
for t in range(T):
for i in range(N_G):
obj.add(a_cost[i] * P[i, t] * P[i, t] + b_cost[i] * P[i, t] + c_cost[i])
obj += a_cost[i] * P[i, t] * P[i, t] + b_cost[i] * P[i, t] + c_cost[i]
model.set_objective(obj)
```

Expand Down
43 changes: 30 additions & 13 deletions docs/source/expression.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
file_format: mystnb
kernelspec:
name: python3
---
# Expression

## Basic expression
Expand All @@ -8,11 +13,11 @@ PyOptInterface currently supports polynomial expressions with degree up to 2, in

The expression can be expressed by arithmetic operations of variables and constants. For example, we can create a quadratic expression by adding two quadratic expressions, multiplying a linear expression with a constant expression, etc.

```python
```{code-cell}
import pyoptinterface as poi
from pyoptinterface import gurobi
from pyoptinterface import highs
model = gurobi.Model()
model = highs.Model()
x = model.add_variable()
Expand Down Expand Up @@ -40,30 +45,42 @@ $$
\frac{1}{2} \sum_{i=1}^N x_i^2 - \sum_{i=1}^N x_i
$$

```python
expr = poi.ExprBuilder()

```{code-cell}
N = 1000
xs = [model.add_variable() for _ in range(N)]
x = [model.add_variable() for _ in range(N)]
for i in range(N):
expr += x[i] * x[i]
expr -= 2 * x[i]
def fast_expr():
expr = poi.ExprBuilder()
for i in range(N):
expr += 0.5 * x[i] * x[i]
expr -= x[i]
def slow_expr():
expr = 0
for i in range(N):
expr += 0.5 * x[i] * x[i]
expr -= x[i]
expr *= 0.5
```

```{code-cell}
%time fast_expr()
```

```{code-cell}
%time slow_expr()
```

## Pretty print expression
If the names of variables are specified, We can use the `pprint` method to print the expression in a human-readable format:

```python
```{code-cell}
x = model.add_variable(name="x")
y = model.add_variable(name="y")
expr = x * x + 2 * x * y + y * y
model.pprint(expr)
# output: 1*x*x + 2*x*y + 1*y*y
```

## Value of expression
Expand Down

0 comments on commit 40c341a

Please sign in to comment.